Advertisement
S_J_H

ccbrowser1.0.0

Jul 11th, 2020
2,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.67 KB | None | 0 0
  1. --CCbrowser 1.0.0 by SamH
  2.  
  3.  
  4. local tArgs = {...}
  5. lines = {}
  6. centered = false
  7. page = {}
  8. pcolor = {}
  9. links = {"None"}
  10. local pTerm = term.current()
  11. local mx,my = term.getSize()
  12. local win = window.create(pTerm,1,1,mx,my)
  13. local webViewer = window.create(win,1,2,mx-1,my)
  14. title = "404"
  15.  
  16. function updateTop(title, pterm)
  17.     pterm.setCursorPos(1,1)
  18.  
  19.     pterm.setBackgroundColor(colors.gray)
  20.     pterm.setTextColor(colors.white)
  21.     pterm.clear()
  22.  
  23.     if title then
  24.         print("Page : "..title)
  25.     else
  26.         print("Page : "..gpage)
  27.     end
  28. end
  29.  
  30. function reload(file)
  31.     --gpage = "http://theoldnet.com/"
  32.     --gpage = "http://google.com/"
  33.     --gpage = "http://microsoft.com/"
  34.     --gpage = "http://apple.com/"
  35.     gpage = "http://81.110.170.100/cc/"
  36.     --gpage = "http://81.110.170.100/" 
  37.  
  38.     -- Determines if the file exists, and can be edited on this computer
  39.     page = {}
  40.     lines = {}
  41.     if tArgs == nil then
  42.         sPath = nil
  43.     else
  44.         sPath = tArgs[1]
  45.     end
  46.  
  47.     if (gpage == nil and sPath == nil) then
  48.         webViewer.clear()
  49.         webViewer.write("No file, or website to display")
  50.     elseif tArgs == nil or #tArgs == 0 then
  51.         sPath = nil
  52.         if file == nil or file == "" then
  53.             tArgs = nil
  54.             url = http.get(gpage, headers)
  55.         else
  56.             url = http.get(file, headers)
  57.         end
  58.     elseif #tArgs >= 1 then
  59.         tArgs = nil
  60.         sPath = shell.resolve(tArgs[1])
  61.     end
  62.  
  63.     if sPath then
  64.         local bReadOnly = fs.isReadOnly(sPath)
  65.         if fs.exists(sPath) and fs.isDir(sPath) then
  66.             print("Cannot edit a directory.")
  67.             return
  68.         end
  69.         h = fs.open(sPath, "r")
  70.     end
  71.     --title = ""
  72.  
  73.     if h or url then
  74.         if sPath then
  75.             for line in h.readLine do
  76.                 lines[ #lines + 1 ] = line
  77.             end
  78.  
  79.             h.close()
  80.         end
  81.         if not sPath then
  82.             for line in url.readLine do
  83.                 lines[ #lines + 1 ] = line
  84.             end    
  85.         end
  86.         for i=1,#lines do
  87.             p = string.find(lines[i], "<p>")
  88.             P = string.find(lines[i], "<P>")
  89.  
  90.             a = string.find(lines[i], '<a href="')
  91.  
  92.             FinputT = string.find(lines[i], '<input type="text"')
  93.             FinputS = string.find(lines[i], '<input type="submit"')
  94.             Flable = string.find(lines[i], '<label')
  95.  
  96.             hed = string.find(lines[i], "<h1>")
  97.             hrr = string.find(lines[i], "<hr>")
  98.             t = string.find(lines[i], "<title>")
  99.             f = string.find(lines[i], "<font")
  100.             lis = string.find(lines[i], "<li")
  101.  
  102.             if t then
  103.                 ti = string.gsub(lines[i], "<title>", "")
  104.                 --tit = string.gsub(ti, "le>", "")
  105.                 title = string.sub(ti, 2, -9)
  106.             elseif f then
  107.                 fo = string.gsub(lines[i], "<", "")
  108.                 fon = string.gsub(fo, ">", "")
  109.                 font = string.sub(fon, 7, -6)
  110.                 color = string.match(font, 'color="(.+)"')
  111.                 sizeq = string.match(font, 'size="(.)"')
  112.                 size = string.match(font, 'size=(.)')
  113.                 if sizeq or size then
  114.                     if color then
  115.                         cf = string.gsub(font, 'color="'..color..'"', "")
  116.                     else
  117.                         cf = font
  118.                     end
  119.  
  120.                     if sizeq then
  121.                         df = string.gsub(cf, 'size="'..sizeq..'"', "")
  122.                     end
  123.  
  124.                     if not sizeq then
  125.                         df = string.gsub(cf, 'size='..size..'', "")
  126.                     end
  127.                    
  128.                     if color then
  129.                         page[#page + 1] = " <Cf>:"..color..":</Cf>".. df   
  130.                     else
  131.                         page[#page + 1] = df
  132.                     end            
  133.                 elseif color then
  134.                     if not size then
  135.                         page[#page + 1] = " <Cf>:"..color..":</Cf> ".. string.gsub(font, 'color="'..color..'"', "")
  136.                     end
  137.                 else
  138.                     if not size then
  139.                         page[#page + 1] = font
  140.                     end
  141.                 end
  142.             elseif FinputT then
  143.                 inpu = string.gsub(lines[i], "<", "")
  144.                 input = string.gsub(inpu, ">", "")
  145.                 inputT = string.sub(input, 1, -7)
  146.                 inputTval = string.match(lines[i], 'value="(.+)"')
  147.                 if inputTval then
  148.                     page[#page + 1] = "<I>:text:</I> v:"..inputTval
  149.                 else
  150.                     page[#page + 1] = "<I>:text:</I> v: "
  151.                 end
  152.             elseif FinputS then
  153.                 Sinpu = string.gsub(lines[i], "<", "")
  154.                 Sinput = string.gsub(Sinpu, ">", "")
  155.                 SinputT = string.sub(Sinput, 1, -7)
  156.                 SinputSval = string.match(lines[i], 'value="(.+)"')
  157.                 page[#page + 1] = "<I>:submit:</I> v:"..SinputSval
  158.             elseif Flable then
  159.                 Flab = string.gsub(lines[i], "<", "")
  160.                 Flabl = string.gsub(Flab, ">", "")
  161.                 Flabley = string.sub(Flabl, 9, -9)
  162.                 lfor = string.match(lines[i], 'for="(.+)"')
  163.                 if lfor then
  164.                     page[#page + 1] = string.gsub(Flabley, 'for="'..lfor..'"', "")
  165.                 else
  166.                     page[#page + 1] = Flabley  
  167.                 end
  168.             elseif a then
  169.                 link = string.match(lines[i], '<a href="(.+)">')
  170.                 links[i] = link
  171.                 dlink = string.match(lines[i], '<a href="'..link..'">(.+)</a>')
  172.                 if dlink then
  173.                     page[#page + 1] = " » "..dlink.." «"
  174.                 else
  175.                     page[#page + 1] = " » "..link.." «"
  176.                 end
  177.  
  178.             elseif p then
  179.                 prt = string.gsub(lines[i], "<", "")
  180.                 err = string.gsub(prt, "p>", "")
  181.                 ern = string.sub(err, 2, -2)
  182.                 page[#page + 1] = ern
  183.             elseif P then
  184.                 prt = string.gsub(lines[i], "<", "")
  185.                 err = string.gsub(prt, "P>", "")
  186.                 ern = string.sub(err, 2, -2)
  187.                 page[#page + 1] = ern
  188.             elseif hed then
  189.                 he = string.gsub(lines[i], "<", "")
  190.                 hea = string.gsub(he, "<", "")
  191.                 head = string.sub(hea, 6, -5)
  192.                 page[#page + 1] = head
  193.             elseif hrr then
  194.                 hr = string.gsub(lines[i], "<", "")
  195.                 hro = string.gsub(hr, "<", "")
  196.                 hrozon = string.sub(hro, 6, -5)
  197.                 page[#page + 1] = "hr"
  198.             elseif lis then
  199.                 li = string.gsub(lines[i], "<", "")
  200.                 lis = string.gsub(li, "<", "")
  201.                 list = string.sub(lis, 7, -5)
  202.                 page[#page + 1] = " * "..list
  203.             end
  204.         end
  205.     else
  206.         term.redirect(webViewer)
  207.  
  208.         term.setBackgroundColor(colors.white)
  209.         term.setTextColor(colors.red)
  210.         term.clear()
  211.         error("404: not found")
  212.  
  213.         sleep(0.5)
  214.         local evt, arg1, arg2 = os.pullEvent()
  215.         term.redirect(pTerm)
  216.  
  217.         webViewer.setBackgroundColor(colors.black)
  218.         webViewer.setTextColor(colors.white)
  219.         webViewer.clear()
  220.  
  221.         term.setBackgroundColor(colors.black)
  222.         term.setTextColor(colors.white)
  223.         term.clear()
  224.         term.setCursorPos(1,1)
  225.         shell.run("clear")
  226.         return
  227.     end
  228. end
  229.  
  230. function drawPage(viewer, pterm, currY)
  231.  
  232.     mx,my = term.getSize()
  233.     updateTop(title, pterm)
  234.  
  235.     term.redirect(viewer)
  236.  
  237.     term.setBackgroundColor(colors.white)
  238.     term.clear()
  239.     nextColor = "black"
  240.  
  241.     for k, v in pairs(page) do
  242.         c =  string.match(v, '<Cf>:(.+):</Cf>')
  243.         if v == "hr" then
  244.             term.setBackgroundColor(colors.white)
  245.             term.setTextColor(colors.black)
  246.             term.setCursorPos(1,currY)
  247.             for i=1,mx do
  248.                 term.write("-")
  249.             end
  250.         elseif string.find(v, "<I>:text:</I>")then
  251.             term.setBackgroundColor(colors.gray)
  252.             term.setTextColor(colors.white)
  253.             term.setCursorPos(1,currY)
  254.             for i=2,mx/2 do
  255.                 term.setCursorPos(i,currY)
  256.                 term.write(" ")
  257.             end
  258.             term.setCursorPos(2,currY)
  259.             term.write(string.gsub(v, '<I>:text:</I> v:', " "))
  260.             term.setTextColor(colors.black)
  261.         elseif string.find(v, "<I>:submit:</I>")then
  262.             currY = currY + 1
  263.             term.setBackgroundColor(colors.gray)
  264.             term.setTextColor(colors.white)
  265.             term.setCursorPos(1,currY)
  266.             l = string.match(v, 'v:(.+)')
  267.             for i=2,#l do
  268.                 term.setCursorPos(i,currY)
  269.                 term.write(" ")
  270.             end
  271.             term.setCursorPos(2,currY)
  272.             term.write(string.gsub(v, '<I>:submit:</I> v:', ""))
  273.             term.setTextColor(colors.black)
  274.         else
  275.             if c then
  276.                 if colors[c] then
  277.                     term.setCursorPos(1,currY)
  278.                     term.setTextColor(colors[c])
  279.                     term.write(string.gsub(v, " <Cf>:"..c..":</Cf> ", ""))
  280.                 else
  281.                     term.setCursorPos(1,currY)
  282.                     term.setTextColor(colors.black)
  283.                     term.write(v)
  284.                 end
  285.             else
  286.                 term.setCursorPos(1,currY)
  287.                 term.setBackgroundColor(colors.white)
  288.                 term.setTextColor(colors.black)
  289.                 term.write(v)
  290.             end
  291.         end
  292.         currY = currY + 1
  293.     end
  294.     term.redirect(pterm)
  295. end
  296.  
  297. function main()
  298.     term.clear()
  299.     print("We also have progr amarguments so you can load a html file!")
  300.     print("Enter URL , or leave blank for demo site (How to use guide)")
  301.     url = read()
  302.  
  303.     term.redirect(win)
  304.     reload(url)
  305.     local currY = 1                    
  306.     gview = "page"
  307.  
  308.     while h or url do
  309.         if gview == "page" then
  310.             updateTop(title, win)
  311.             drawPage(webViewer, pTerm, currY)
  312.             local evt, arg1, arg2 = os.pullEvent()
  313.  
  314.             if evt=="mouse_scroll" then
  315.                 if arg1==1 then
  316.                     if currY+1 == my-#page then
  317.                         currY = my-#page
  318.                     else
  319.                         currY=math.min(currY-1,my)
  320.                     end
  321.                 elseif arg1==-1 then
  322.                     if currY == 1 then
  323.                         currY = 1
  324.                     else
  325.                         currY=math.max(currY+1,0-my)
  326.                     end
  327.                 end
  328.  
  329.             elseif evt=="key" then         
  330.                 if arg1==keys.down then
  331.                     if currY+1 == my-#page then
  332.                         currY = my-#page
  333.                     else
  334.                         currY=math.min(currY-1,my)
  335.                     end
  336.                 elseif arg1==keys.up then
  337.                     if currY == 1 then
  338.                         currY = 1
  339.                     else
  340.                         currY=math.max(currY+1,0-my)
  341.                     end
  342.                 elseif arg1==keys.r then
  343.                     reload()
  344.                     updateTop(title, win)
  345.                     drawPage(webViewer, win, currY)
  346.                 elseif arg1==keys.leftCtrl then
  347.                     gview = "off"
  348.                 elseif arg1== keys.a then
  349.                     gveiw = "off"                  
  350.                 end
  351.             end
  352.         elseif gview == "off" then
  353.             return
  354.             --gview = "page"
  355.         end
  356.     end
  357. end
  358.  
  359. help.setPath( help.path() .. ":/web/help/html.txt" )
  360.  
  361. headers = {
  362.   [ "User-Agent" ] = "CCbrowser/1.0.0(CraftOS)CCwebKit/1.0.0(KHTML, like Gecko) Version/13.1 CCbrowser/1.0.0", -- Overrides the default User-Agent.
  363. }
  364.  
  365. no_error, message = pcall(main)
  366. term.redirect(pTerm)
  367. term.setBackgroundColor(colors.black)
  368. term.setTextColor(colors.white)
  369. term.clear()
  370. term.setCursorPos(1,1)
  371. if not no_error then
  372.     print(message)
  373. else
  374.     print("Goodbye!")
  375. end
  376.  
  377.         -- elseif arg1==keys.pageDown then
  378.         --  currY=math.max(currY+my-1,my-1)
  379.         --  drawPage(webViewer, pTerm, currY)
  380.         -- elseif arg1==keys.pageUp then
  381.         --  currY=math.min(currY-my-1,1)
  382.         --  drawPage(webViewer, pTerm, currY)
  383.         -- elseif arg1==keys.home then
  384.         --  currY=1
  385.         --  drawPage(webViewer, pTerm, currY)
  386.         -- elseif arg1==keys["end"] then
  387.         --  currY=my+1
  388.         --  drawPage(webViewer, pTerm, currY)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement