Alyssa

Browser

Feb 16th, 2015
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.30 KB | None | 0 0
  1. version = 2.2
  2. srvEnabled = true --If you don't want srv, just turn this to false.
  3. fastSetup = true --Only setup pageLoader once, if you want it to be slow everytime, just turn this to false.
  4. safeMode = true --Disables fs and io, Not finished yet.
  5. linksEnabled = true
  6. browserID = "lightning"
  7. requestTimeout = 3 --If you use bitnet, this will need to be set higher.
  8. --Changelog (Starting in v1.6)
  9. --2.2 Fixed loading animation
  10. --2.1 Made a loading animation
  11. --1.9 Made is use wireless modems if wired modems are found.
  12. --1.8 Edited the URL bar.
  13. --1.7 Converted apiGet and downloadPage to work better, using requestTimeout.
  14. --1.6 Changed the pageloader to use requestTimeout, also will work better.
  15. if srvEnabled then
  16.     if not fs.exists("/srv") then
  17.         shell.run("pastebin","get","xwSFL865","/srv")
  18.     end
  19.     os.loadAPI("/srv")
  20. end
  21. browserFenv = {}
  22. --if safeMode then
  23. --
  24. --else
  25. --
  26. --end
  27. debug = false
  28. local scrx,scry = term.getSize()
  29. local cx,cy = math.floor(scrx/2), math.floor(scry/2)
  30. local fline = string.rep(" ",scrx)
  31. for k, v in pairs(peripheral.getNames()) do
  32.     if peripheral.getType(v) == "modem" then
  33.         modem = peripheral.wrap(v)
  34.         rednet.open(v)
  35.     end
  36. end
  37.  
  38. for k, v in pairs(peripheral.getNames()) do
  39.     if peripheral.getType(v) == "modem" then
  40.         if peripheral.call(v,"isWireless") then
  41.             rednet.close()
  42.             modem = peripheral.wrap(v)
  43.             rednet.open(v)
  44.         end
  45.     end
  46. end
  47.  
  48. function goto(addr)
  49.     selected = false
  50.     site = addr
  51.     typed = ""
  52.     tPos = 1
  53.     os.queueEvent("key",41)
  54.     nPage = true
  55.     history[chist+1] = site
  56.     chist = chist+1
  57.     if history[chist+1] then
  58.         for i = chist+1, #history do
  59.             history[i] = nil               
  60.         end
  61.     end
  62.     bar()
  63. end
  64.  
  65. function downloadPage(urlToDownload, fileName)
  66.     local curposx, curposy = term.getCursorPos()
  67.     utd = urlToDownload
  68.     sp = string.find(utd,"/")
  69.     if sp then
  70.         if #utd > sp then
  71.             domain = string.sub(utd,1,sp-1)
  72.             pageOS = string.sub(utd,sp+1,#utd)
  73.         else
  74.             domain = string.sub(utd,1,sp-1)
  75.             pageOS = "index"
  76.         end
  77.     else
  78.         domain = utd
  79.         pageOS = "index"
  80.     end
  81.     rednet.broadcast(pageOS,domain)
  82.     evnt = {rednet.receive(3)}
  83.     RTID = os.startTimer(requestTimeout)
  84.     LSTID = os.startTimer(0.2)
  85.     lps = #"Loading" +1
  86.     write("Loading")
  87.     loadloops = 0
  88.     while true do
  89.         e,side,chan,rchan,msg,dist = os.pullEvent()
  90.         if e == "timer" and side == RTID then
  91.             print("Could not connect to site.")
  92.             return false
  93.         elseif e == "timer" and side == LSTID then
  94.             term.setCursorPos(-1+curposx+lps+loadloops,curposy)
  95.             if loadloops ~= 3 then
  96.                 write(".")
  97.             end
  98.             loadloops = loadloops +1
  99.             if loadloops == 4 then
  100.                 term.setCursorPos(curposx,curposy)
  101.                 write("Loading   ")
  102.                 loadloops = 0
  103.             end
  104.             LSTID = os.startTimer(0.2)
  105.         elseif e == "modem_message" and chan == os.getComputerID() then
  106.             if msg["nRecipient"] == os.getComputerID() then
  107.                 f = fs.open(fileName,"w")
  108.                 f.write(msg["message"])
  109.                 f.close()
  110.                 term.setCursorPos(curposx,curposy)
  111.                 write(string.rep(" "),#"Loading...")
  112.                 term.setCursorPos(curposx,curposy)
  113.                 return true
  114.             end
  115.         end
  116.     end
  117. end
  118.  
  119. function apiGet(page,args)
  120.     local curposx, curposy = term.getCursorPos()
  121.     if args then
  122.         utd = page.."?"..args
  123.     else
  124.         utd = page
  125.     end
  126.     sp = string.find(utd,"/")
  127.     if sp then
  128.         if #utd > sp then
  129.             domain = string.sub(utd,1,sp-1)
  130.             pageOS = string.sub(utd,sp+1,#utd)
  131.         else
  132.             domain = string.sub(utd,1,sp-1)
  133.             pageOS = "index"
  134.         end
  135.     else
  136.         domain = utd
  137.         pageOS = "index"
  138.     end
  139.     rednet.broadcast(pageOS,domain)
  140.     RTID = os.startTimer(requestTimeout)
  141.     LSTID = os.startTimer(0.2)
  142.     lps = #"Loading" +1
  143.     write("Loading")
  144.     loadloops = 0
  145.     while true do
  146.         e,side,chan,rchan,msg,dist = os.pullEvent()
  147.         if e == "timer" and side == RTID then
  148.             print("Could not connect to site.")
  149.             return false
  150.         elseif e == "timer" and side == LSTID then
  151.             term.setCursorPos(-1+curposx+lps+loadloops,curposy)
  152.             if loadloops ~= 3 then
  153.                 write(".")
  154.             end
  155.             loadloops = loadloops +1
  156.             if loadloops == 4 then
  157.                 term.setCursorPos(curposx,curposy)
  158.                 write("Loading   ")
  159.                 loadloops = 0
  160.             end
  161.             LSTID = os.startTimer(0.2)
  162.         elseif e == "modem_message" and chan == os.getComputerID() then
  163.             if msg["nRecipient"] == os.getComputerID() then
  164.                 term.setCursorPos(curposx,curposy)
  165.                 write(string.rep(" "),#"Loading...")
  166.                 term.setCursorPos(curposx,curposy)
  167.                 return msg["message"]
  168.             end
  169.         end
  170.     end
  171. end
  172.  
  173. term.clear()
  174. local dE = true
  175. local cur = term.current()
  176. local wPage = window.create(cur, 1, 3, scrx, scry-2,true)
  177. local wBar= window.create(cur,7,1,scrx-6, 1, true)
  178. term.redirect(wPage)
  179. term.setBackgroundColor(colors.white)
  180. term.clear()
  181. history = {
  182. "main",
  183. }
  184. chist = 1
  185. site = "main"
  186.  
  187. local oPullRaw = os.pullEventRaw
  188. local oPull = os.pullEvent
  189. os.pullEvent = function(filter)
  190.     pEData = {oPullRaw(filter)}
  191.     if pEData[1] ~= "mouse_click" then
  192.         return unpack(pEData)
  193.     else
  194.         local e,b,cx,cy =unpack(pEData)
  195.         cy = cy-2
  196.         return e,b,cx,cy
  197.     end
  198. end
  199.  
  200. function cWrite(text,line)
  201.     if not line then
  202.         local curx,cury = term.getCursorPos()
  203.         line = cury
  204.     else
  205.         line = tonumber(line)
  206.     end
  207.     local scrx,scry = term.getSize()
  208.     local cx,cy = math.floor(scrx/2), math.floor(scry/2)
  209.     local tx = cx - #text/2
  210.     term.setCursorPos(tx,line)
  211.     write(text)
  212. end
  213.  
  214. function cPrint(text,line)
  215.     if not line then
  216.         local curx,cury = term.getCursorPos()
  217.         line = cury
  218.     else
  219.         line = tonumber(line)
  220.     end
  221.     local scrx,scry = term.getSize()
  222.     local cx,cy = math.floor(scrx/2), math.floor(scry/2)
  223.     local tx = cx - #text/2
  224.     term.setCursorPos(tx,line)
  225.     write(text.."\n")
  226. end
  227.  
  228. pageLoad = {
  229. "plv = 2.2",
  230. "scrx,scry = term.getSize()",
  231. "cx,cy = math.floor(scrx/2),math.floor(scry/2)",
  232. "local add1 = \"function page()\"",
  233. "add2 = \"\"",
  234. "",
  235. "at2 = {",
  236. "\"end\",",
  237. "\"function cExit()\",",
  238. "\" rPage = true\",",
  239. "\" while rPage do\",",
  240. "\"     ef,p1f = os.pullEvent()\",",
  241. "\"     if ef == 'key' or ef == 'terminate' then \",",
  242. "\"         if p1f == 41 or ef == 'terminate' then \",",
  243. "\"             rPage = false\",",
  244. "\"             return true\",",
  245. "\"         end\",",
  246. "\"     end\",",
  247. "\" end\",",
  248. "\"end\",",
  249. "\"parallel.waitForAny(page, cExit)\",",
  250. "}",
  251. "",
  252. "for i = 1, #at2 do",
  253. "   add2 = add2..\"\\n\"..at2[i]",
  254. "   sleep(0)",
  255. "end",
  256. "function page()",
  257. "   dE = false",
  258. "   --term.redirect(wPage)",
  259. "   term.setBackgroundColor(colors.white)",
  260. "   term.setTextColor(colors.black)",
  261. "   term.clear()",
  262. "   term.setCursorPos(1,1)",
  263. "   if site == \"main\" then",
  264. "       term.setCursorPos(1,cx-1)",
  265. "       cPrint(\"Lightning Browser\")",
  266. "       cPrint(\"Main page\")",
  267. "   elseif site == \"\" or site == nil then",
  268. "       term.setCursorPos(1,1)",
  269. "       cPrint(\"This page does not exist!\")",
  270. "   else",
  271. "       local curposx, curposy = term.getCursorPos()",
  272. "       fDomain = false",
  273. "       sp = string.find(site,\"/\")",
  274. "       if sp then",
  275. "           if #site > sp then",
  276. "               domain = string.sub(site,1,sp-1)",
  277. "               pageOS = string.sub(site,sp+1,#site)",
  278. "           else",
  279. "               domain = string.sub(site,1,sp-1)",
  280. "               pageOS = \"index\"",
  281. "           end",
  282. "       else",
  283. "           domain = site",
  284. "           pageOS = \"index\"",
  285. "       end",
  286. "       rednet.broadcast(pageOS,domain)",
  287. "       evnt = nil",
  288. "       pagef = nil",
  289. "       pg = nil",
  290. "       RTID = os.startTimer(requestTimeout)",
  291. "       LSTID = os.startTimer(0.2)",
  292. "       lps = #\"Loading\" +1",
  293. "       write(\"Loading\")",
  294. "       loadloops = 0",
  295. "       while true do",
  296. "           e,side,chan,rchan,msg,dist = os.pullEvent()",
  297. "           if e == 'timer' and side == RTID then",
  298. "               print(\"Could not connect to site.\")",
  299. "               break",
  300. "       elseif e == \"timer\" and side == LSTID then",
  301. "           term.setCursorPos(-1+curposx+lps+loadloops,curposy)",
  302. "           if loadloops ~= 3 then",
  303. "               write(\".\")",
  304. "           end",
  305. "           loadloops = loadloops +1",
  306. "           if loadloops == 4 then",
  307. "               term.setCursorPos(curposx,curposy)",
  308. "               write(\"Loading   \")",
  309. "               loadloops = 0",
  310. "           end",
  311. "           LSTID = os.startTimer(0.2)",
  312. "           elseif e == \"modem_message\" and chan == os.getComputerID() then",
  313. "               if msg[\"nRecipient\"] == os.getComputerID() then",
  314. "                   pg = add1..\"\\n\"..msg[\"message\"]..\"\\n\"..add2",
  315. "                   if debug == true then",
  316. "                       f = fs.open('pageLog','w')",
  317. "                       f.write(pg)",
  318. "                       f.close()",
  319. "                   end",
  320. "                   function exitPage()",
  321. "                       exitE = nil",
  322. "                       while not exitE do",
  323. "                           exitEvent,exitParam = os.pullEvent()",
  324. "                           if exitEvent == \"key\" and exitParam == 41 then",
  325. "                               exitE = true",
  326. "                               return true",
  327. "                           elseif exitEvent == 'terminate' then",
  328. "                               if oPull and oPullRaw then",
  329. "                                   os.pullEvent = oPull",
  330. "                                   os.pullEventRaw = oPullRaw",
  331. "                               end",
  332. "                               os.queueEvent('key',41)",
  333. "                               browserID = nil",
  334. "                               linksEnabled = nil",
  335. "                               debug = nil",
  336. "                               run = nil",
  337. "                               break",
  338. "                           end",
  339. "                       end",
  340. "                   end",
  341. "                   term.setCursorPos(curposx,curposy)",
  342. "                   write(string.rep(\" \",#\"Loading...\"))",
  343. "                   term.setCursorPos(curposx,curposy)",
  344. "                   pagef = loadstring(pg)",
  345. "                   setfenv(pagef, getfenv())",
  346. "                   parallel.waitForAny(exitPage, function() pcall(pagef) end)",
  347. "                   pagef = nil",
  348. "                   exitPage = nil",
  349. "                   break",
  350. "               end",
  351. "           end",
  352. "       end",
  353. "   end",
  354. "end",
  355. "page()",
  356. }
  357. forceSetup = false
  358. if fs.exists("/.pageloader") then
  359.     f = fs.open("/.pageloader","r")
  360.     plvd = loadstring(f.readLine())
  361.     f.close()
  362.     plvd()
  363.     if plv == 2.2 then
  364.     else
  365.         forceSetup = true
  366.     end
  367. end
  368. if not fs.exists("/.pageloader") then
  369.     if not fastSetup then
  370.         nPageLoad = ""
  371.         print("Setting up the page loader, this may take awhile")
  372.         for i = 1, #pageLoad do
  373.             nPageLoad = nPageLoad.."\n"..pageLoad[i]
  374.             sleep(0)
  375.         end
  376.     else
  377.         f = fs.open("/.pageloader","w")
  378.         nPageLoad = ""
  379.         print("Setting up the page loader, this may take awhile")
  380.         for i = 1, #pageLoad do
  381.             f.writeLine(pageLoad[i])
  382.             nPageLoad = nPageLoad.."\n"..pageLoad[i]
  383.             sleep(0)
  384.         end
  385.         f.close()
  386.     end
  387. elseif fs.exists("/.pageloader") then
  388.     if not forceSetup then
  389.         if fastSetup then
  390.             nPageLoad = ""
  391.             f = fs.open("/.pageloader","r")
  392.             nPageLoad = f.readAll()
  393.             f.close()
  394.         else
  395.             nPageLoad = ""
  396.             print("Setting up the page loader, this may take awhile")
  397.             for i = 1, #pageLoad do
  398.                 nPageLoad = nPageLoad.."\n"..pageLoad[i]
  399.                 sleep(0)
  400.             end
  401.         end
  402.     else
  403.         if not fastSetup then
  404.             nPageLoad = ""
  405.             print("Setting up the page loader, this may take awhile")
  406.             for i = 1, #pageLoad do
  407.                 nPageLoad = nPageLoad.."\n"..pageLoad[i]
  408.                 sleep(0)
  409.             end
  410.         else
  411.             f = fs.open("/.pageloader","w")
  412.             nPageLoad = ""
  413.             print("Setting up the page loader, this may take awhile")
  414.             for i = 1, #pageLoad do
  415.                 f.writeLine(pageLoad[i])
  416.                 nPageLoad = nPageLoad.."\n"..pageLoad[i]
  417.                 sleep(0)
  418.             end
  419.             f.close()
  420.         end
  421.     end
  422. end
  423. pageLoad = nPageLoad
  424.  
  425. function PL()
  426.     pageLoader = loadstring(pageLoad)
  427.     setfenv(pageLoader, getfenv())
  428.     pageLoader()
  429. end
  430.  
  431. function pageSys()
  432.     nPage = false
  433.     site = "main"
  434.     PL()
  435.     run = 1
  436.     while run do
  437.         TID = os.startTimer(0.1)
  438.         local e,p1,p2,p3,p4,p5 = oPullRaw()
  439.         if e == "terminate" then
  440.             run = false
  441.             return false
  442.         elseif e == "timer" and p1 == TID then
  443.             TID = os.startTimer(0.1)
  444.         end
  445.         if nPage then
  446.             nPage = false
  447.             PL()
  448.         end
  449.     end
  450. end
  451.  
  452. function cBar()
  453.     tPos = 1
  454.     typed = ""
  455.     while true do
  456.         e,mb, clickx, clicky = oPull()
  457.         if e == "key" and mb == 29 then
  458.             if not selected then
  459.                 typed = ""
  460.                 selected = true
  461.                 bar()
  462.             end
  463.         elseif e == "terminate" then
  464.             os.pullEvent = oPull
  465.             os.pullEventRaw = oPullRaw
  466.             os.queueEvent("key",41)
  467.             browserID = nil
  468.             linksEnabled = nil
  469.             debug = nil
  470.             run = nil
  471.             break
  472.         elseif e == "key" and mb == 28 then
  473.             if selected then
  474.                 selected = false
  475.                 site = typed
  476.                 typed = ""
  477.                 tPos = 1
  478.                 os.queueEvent("key",41)
  479.                 nPage = true
  480.                 history[chist+1] = site
  481.                 chist = chist+1
  482.                 if history[chist+1] then
  483.                     for i = chist+1, #history do
  484.                         history[i] = nil               
  485.                     end
  486.                 end
  487.                 bar()
  488.             end
  489.         elseif e == "key" and mb == 14 then
  490.             if selected then
  491.                 if tPos > 1 then
  492.                     typed = string.sub(typed,0,tPos-2)..string.sub(typed,tPos,#typed)
  493.                     tPos = tPos -1
  494.                     bar()
  495.                 end
  496.             end
  497.         elseif e == "key" and mb == keys.left then
  498.             if selected then
  499.                 if tPos > 1 then
  500.                     tPos = tPos -1
  501.                 end
  502.             end
  503.         elseif e == "key" and mb == keys.right then
  504.             if selected then
  505.                 if tPos < #typed+1 then
  506.                     tPos = tPos +1
  507.                 end
  508.             end
  509.         elseif e == "char" then
  510.             if selected then
  511.                 typed = string.sub(typed,0,tPos-1)..mb..string.sub(typed,tPos,#typed)
  512.                 tPos = tPos+1
  513.                 bar()
  514.             end
  515.         elseif e == "mouse_click" then
  516.             if clickx >= 7 and clicky == 1 then
  517.                 if not selected then
  518.                     if site ~= "main" then
  519.                         typed = site
  520.                     else
  521.                         typed = ""
  522.                     end
  523.                     tPos = #typed+1
  524.                     selected = true
  525.                     bar()
  526.                 end
  527.             elseif clickx >= 1 and clickx <= 2 and clicky == 1 then
  528.                 if chist > 1 and history[chist-1] then
  529.                     chist = chist-1
  530.                     selected = false
  531.                     site = history[chist]
  532.                     typed = ""
  533.                     tPos = 1
  534.                     os.queueEvent("key",41)
  535.                     nPage = true
  536.                     bar()
  537.                 end
  538.             elseif clickx >= 4 and clickx <= 5 and clicky == 1 then
  539.                 if chist < #history and history[chist+1] then
  540.                     chist = chist+1
  541.                     selected = false
  542.                     site = history[chist]
  543.                     typed = ""
  544.                     tPos = 1
  545.                     os.queueEvent("key",41)
  546.                     nPage = true
  547.                     bar()
  548.                 end
  549.             end
  550.         end
  551.     end
  552. end
  553.  
  554. function bar()
  555.     term.redirect(cur)
  556.     term.setCursorPos(1,1)
  557.     term.setBackgroundColor(colors.white)
  558.     term.setTextColor(colors.lightGray)
  559.     write("<-|-> ")
  560.     term.setBackgroundColor(colors.lightGray)
  561.     print(string.rep(" ",scrx-#"<-|-> "))
  562.     term.setCursorPos(7,1)
  563.     term.setTextColor(colors.black)
  564.     write(string.sub(site,1,scrx-6))
  565.     term.setTextColor(colors.lightGray)
  566.     term.setBackgroundColor(colors.white)
  567.     term.setCursorPos(1,2)
  568.     print(string.rep("_",scrx))
  569.     term.redirect(wBar)
  570.     term.setBackgroundColor(colors.lightGray)
  571.     term.setTextColor(colors.black)
  572.     term.clear()
  573.     term.setCursorPos(1,1)
  574.     if selected then
  575.         term.setBackgroundColor(colors.blue)
  576.         term.setTextColor(colors.white)
  577.     end
  578.     if not selected then
  579.         write(site)
  580.     else
  581.         write(typed)
  582.     end
  583.     term.setBackgroundColor(colors.lightGray)
  584.     term.setTextColor(colors.black)
  585.     term.redirect(wPage)
  586. end
  587.  
  588. if term.isColor() then
  589.     site = ""
  590.     typed = ""
  591.     selected = false
  592.     bar()
  593.     parallel.waitForAny(pageSys,cBar)
  594. else
  595.     print("You need an advanced computer for this program!")
  596. end
  597. os.pullEvent = oPull
  598. os.pullEventRaw = oPullRaw
Advertisement
Add Comment
Please, Sign In to add comment