Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = 2.2
- srvEnabled = true --If you don't want srv, just turn this to false.
- fastSetup = true --Only setup pageLoader once, if you want it to be slow everytime, just turn this to false.
- safeMode = true --Disables fs and io, Not finished yet.
- linksEnabled = true
- browserID = "lightning"
- requestTimeout = 3 --If you use bitnet, this will need to be set higher.
- --Changelog (Starting in v1.6)
- --2.2 Fixed loading animation
- --2.1 Made a loading animation
- --1.9 Made is use wireless modems if wired modems are found.
- --1.8 Edited the URL bar.
- --1.7 Converted apiGet and downloadPage to work better, using requestTimeout.
- --1.6 Changed the pageloader to use requestTimeout, also will work better.
- if srvEnabled then
- if not fs.exists("/srv") then
- shell.run("pastebin","get","xwSFL865","/srv")
- end
- os.loadAPI("/srv")
- end
- browserFenv = {}
- --if safeMode then
- --
- --else
- --
- --end
- debug = false
- local scrx,scry = term.getSize()
- local cx,cy = math.floor(scrx/2), math.floor(scry/2)
- local fline = string.rep(" ",scrx)
- for k, v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- modem = peripheral.wrap(v)
- rednet.open(v)
- end
- end
- for k, v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- if peripheral.call(v,"isWireless") then
- rednet.close()
- modem = peripheral.wrap(v)
- rednet.open(v)
- end
- end
- end
- function goto(addr)
- selected = false
- site = addr
- typed = ""
- tPos = 1
- os.queueEvent("key",41)
- nPage = true
- history[chist+1] = site
- chist = chist+1
- if history[chist+1] then
- for i = chist+1, #history do
- history[i] = nil
- end
- end
- bar()
- end
- function downloadPage(urlToDownload, fileName)
- local curposx, curposy = term.getCursorPos()
- utd = urlToDownload
- sp = string.find(utd,"/")
- if sp then
- if #utd > sp then
- domain = string.sub(utd,1,sp-1)
- pageOS = string.sub(utd,sp+1,#utd)
- else
- domain = string.sub(utd,1,sp-1)
- pageOS = "index"
- end
- else
- domain = utd
- pageOS = "index"
- end
- rednet.broadcast(pageOS,domain)
- evnt = {rednet.receive(3)}
- RTID = os.startTimer(requestTimeout)
- LSTID = os.startTimer(0.2)
- lps = #"Loading" +1
- write("Loading")
- loadloops = 0
- while true do
- e,side,chan,rchan,msg,dist = os.pullEvent()
- if e == "timer" and side == RTID then
- print("Could not connect to site.")
- return false
- elseif e == "timer" and side == LSTID then
- term.setCursorPos(-1+curposx+lps+loadloops,curposy)
- if loadloops ~= 3 then
- write(".")
- end
- loadloops = loadloops +1
- if loadloops == 4 then
- term.setCursorPos(curposx,curposy)
- write("Loading ")
- loadloops = 0
- end
- LSTID = os.startTimer(0.2)
- elseif e == "modem_message" and chan == os.getComputerID() then
- if msg["nRecipient"] == os.getComputerID() then
- f = fs.open(fileName,"w")
- f.write(msg["message"])
- f.close()
- term.setCursorPos(curposx,curposy)
- write(string.rep(" "),#"Loading...")
- term.setCursorPos(curposx,curposy)
- return true
- end
- end
- end
- end
- function apiGet(page,args)
- local curposx, curposy = term.getCursorPos()
- if args then
- utd = page.."?"..args
- else
- utd = page
- end
- sp = string.find(utd,"/")
- if sp then
- if #utd > sp then
- domain = string.sub(utd,1,sp-1)
- pageOS = string.sub(utd,sp+1,#utd)
- else
- domain = string.sub(utd,1,sp-1)
- pageOS = "index"
- end
- else
- domain = utd
- pageOS = "index"
- end
- rednet.broadcast(pageOS,domain)
- RTID = os.startTimer(requestTimeout)
- LSTID = os.startTimer(0.2)
- lps = #"Loading" +1
- write("Loading")
- loadloops = 0
- while true do
- e,side,chan,rchan,msg,dist = os.pullEvent()
- if e == "timer" and side == RTID then
- print("Could not connect to site.")
- return false
- elseif e == "timer" and side == LSTID then
- term.setCursorPos(-1+curposx+lps+loadloops,curposy)
- if loadloops ~= 3 then
- write(".")
- end
- loadloops = loadloops +1
- if loadloops == 4 then
- term.setCursorPos(curposx,curposy)
- write("Loading ")
- loadloops = 0
- end
- LSTID = os.startTimer(0.2)
- elseif e == "modem_message" and chan == os.getComputerID() then
- if msg["nRecipient"] == os.getComputerID() then
- term.setCursorPos(curposx,curposy)
- write(string.rep(" "),#"Loading...")
- term.setCursorPos(curposx,curposy)
- return msg["message"]
- end
- end
- end
- end
- term.clear()
- local dE = true
- local cur = term.current()
- local wPage = window.create(cur, 1, 3, scrx, scry-2,true)
- local wBar= window.create(cur,7,1,scrx-6, 1, true)
- term.redirect(wPage)
- term.setBackgroundColor(colors.white)
- term.clear()
- history = {
- "main",
- }
- chist = 1
- site = "main"
- local oPullRaw = os.pullEventRaw
- local oPull = os.pullEvent
- os.pullEvent = function(filter)
- pEData = {oPullRaw(filter)}
- if pEData[1] ~= "mouse_click" then
- return unpack(pEData)
- else
- local e,b,cx,cy =unpack(pEData)
- cy = cy-2
- return e,b,cx,cy
- end
- end
- function cWrite(text,line)
- if not line then
- local curx,cury = term.getCursorPos()
- line = cury
- else
- line = tonumber(line)
- end
- local scrx,scry = term.getSize()
- local cx,cy = math.floor(scrx/2), math.floor(scry/2)
- local tx = cx - #text/2
- term.setCursorPos(tx,line)
- write(text)
- end
- function cPrint(text,line)
- if not line then
- local curx,cury = term.getCursorPos()
- line = cury
- else
- line = tonumber(line)
- end
- local scrx,scry = term.getSize()
- local cx,cy = math.floor(scrx/2), math.floor(scry/2)
- local tx = cx - #text/2
- term.setCursorPos(tx,line)
- write(text.."\n")
- end
- pageLoad = {
- "plv = 2.2",
- "scrx,scry = term.getSize()",
- "cx,cy = math.floor(scrx/2),math.floor(scry/2)",
- "local add1 = \"function page()\"",
- "add2 = \"\"",
- "",
- "at2 = {",
- "\"end\",",
- "\"function cExit()\",",
- "\" rPage = true\",",
- "\" while rPage do\",",
- "\" ef,p1f = os.pullEvent()\",",
- "\" if ef == 'key' or ef == 'terminate' then \",",
- "\" if p1f == 41 or ef == 'terminate' then \",",
- "\" rPage = false\",",
- "\" return true\",",
- "\" end\",",
- "\" end\",",
- "\" end\",",
- "\"end\",",
- "\"parallel.waitForAny(page, cExit)\",",
- "}",
- "",
- "for i = 1, #at2 do",
- " add2 = add2..\"\\n\"..at2[i]",
- " sleep(0)",
- "end",
- "function page()",
- " dE = false",
- " --term.redirect(wPage)",
- " term.setBackgroundColor(colors.white)",
- " term.setTextColor(colors.black)",
- " term.clear()",
- " term.setCursorPos(1,1)",
- " if site == \"main\" then",
- " term.setCursorPos(1,cx-1)",
- " cPrint(\"Lightning Browser\")",
- " cPrint(\"Main page\")",
- " elseif site == \"\" or site == nil then",
- " term.setCursorPos(1,1)",
- " cPrint(\"This page does not exist!\")",
- " else",
- " local curposx, curposy = term.getCursorPos()",
- " fDomain = false",
- " sp = string.find(site,\"/\")",
- " if sp then",
- " if #site > sp then",
- " domain = string.sub(site,1,sp-1)",
- " pageOS = string.sub(site,sp+1,#site)",
- " else",
- " domain = string.sub(site,1,sp-1)",
- " pageOS = \"index\"",
- " end",
- " else",
- " domain = site",
- " pageOS = \"index\"",
- " end",
- " rednet.broadcast(pageOS,domain)",
- " evnt = nil",
- " pagef = nil",
- " pg = nil",
- " RTID = os.startTimer(requestTimeout)",
- " LSTID = os.startTimer(0.2)",
- " lps = #\"Loading\" +1",
- " write(\"Loading\")",
- " loadloops = 0",
- " while true do",
- " e,side,chan,rchan,msg,dist = os.pullEvent()",
- " if e == 'timer' and side == RTID then",
- " print(\"Could not connect to site.\")",
- " break",
- " elseif e == \"timer\" and side == LSTID then",
- " term.setCursorPos(-1+curposx+lps+loadloops,curposy)",
- " if loadloops ~= 3 then",
- " write(\".\")",
- " end",
- " loadloops = loadloops +1",
- " if loadloops == 4 then",
- " term.setCursorPos(curposx,curposy)",
- " write(\"Loading \")",
- " loadloops = 0",
- " end",
- " LSTID = os.startTimer(0.2)",
- " elseif e == \"modem_message\" and chan == os.getComputerID() then",
- " if msg[\"nRecipient\"] == os.getComputerID() then",
- " pg = add1..\"\\n\"..msg[\"message\"]..\"\\n\"..add2",
- " if debug == true then",
- " f = fs.open('pageLog','w')",
- " f.write(pg)",
- " f.close()",
- " end",
- " function exitPage()",
- " exitE = nil",
- " while not exitE do",
- " exitEvent,exitParam = os.pullEvent()",
- " if exitEvent == \"key\" and exitParam == 41 then",
- " exitE = true",
- " return true",
- " elseif exitEvent == 'terminate' then",
- " if oPull and oPullRaw then",
- " os.pullEvent = oPull",
- " os.pullEventRaw = oPullRaw",
- " end",
- " os.queueEvent('key',41)",
- " browserID = nil",
- " linksEnabled = nil",
- " debug = nil",
- " run = nil",
- " break",
- " end",
- " end",
- " end",
- " term.setCursorPos(curposx,curposy)",
- " write(string.rep(\" \",#\"Loading...\"))",
- " term.setCursorPos(curposx,curposy)",
- " pagef = loadstring(pg)",
- " setfenv(pagef, getfenv())",
- " parallel.waitForAny(exitPage, function() pcall(pagef) end)",
- " pagef = nil",
- " exitPage = nil",
- " break",
- " end",
- " end",
- " end",
- " end",
- "end",
- "page()",
- }
- forceSetup = false
- if fs.exists("/.pageloader") then
- f = fs.open("/.pageloader","r")
- plvd = loadstring(f.readLine())
- f.close()
- plvd()
- if plv == 2.2 then
- else
- forceSetup = true
- end
- end
- if not fs.exists("/.pageloader") then
- if not fastSetup then
- nPageLoad = ""
- print("Setting up the page loader, this may take awhile")
- for i = 1, #pageLoad do
- nPageLoad = nPageLoad.."\n"..pageLoad[i]
- sleep(0)
- end
- else
- f = fs.open("/.pageloader","w")
- nPageLoad = ""
- print("Setting up the page loader, this may take awhile")
- for i = 1, #pageLoad do
- f.writeLine(pageLoad[i])
- nPageLoad = nPageLoad.."\n"..pageLoad[i]
- sleep(0)
- end
- f.close()
- end
- elseif fs.exists("/.pageloader") then
- if not forceSetup then
- if fastSetup then
- nPageLoad = ""
- f = fs.open("/.pageloader","r")
- nPageLoad = f.readAll()
- f.close()
- else
- nPageLoad = ""
- print("Setting up the page loader, this may take awhile")
- for i = 1, #pageLoad do
- nPageLoad = nPageLoad.."\n"..pageLoad[i]
- sleep(0)
- end
- end
- else
- if not fastSetup then
- nPageLoad = ""
- print("Setting up the page loader, this may take awhile")
- for i = 1, #pageLoad do
- nPageLoad = nPageLoad.."\n"..pageLoad[i]
- sleep(0)
- end
- else
- f = fs.open("/.pageloader","w")
- nPageLoad = ""
- print("Setting up the page loader, this may take awhile")
- for i = 1, #pageLoad do
- f.writeLine(pageLoad[i])
- nPageLoad = nPageLoad.."\n"..pageLoad[i]
- sleep(0)
- end
- f.close()
- end
- end
- end
- pageLoad = nPageLoad
- function PL()
- pageLoader = loadstring(pageLoad)
- setfenv(pageLoader, getfenv())
- pageLoader()
- end
- function pageSys()
- nPage = false
- site = "main"
- PL()
- run = 1
- while run do
- TID = os.startTimer(0.1)
- local e,p1,p2,p3,p4,p5 = oPullRaw()
- if e == "terminate" then
- run = false
- return false
- elseif e == "timer" and p1 == TID then
- TID = os.startTimer(0.1)
- end
- if nPage then
- nPage = false
- PL()
- end
- end
- end
- function cBar()
- tPos = 1
- typed = ""
- while true do
- e,mb, clickx, clicky = oPull()
- if e == "key" and mb == 29 then
- if not selected then
- typed = ""
- selected = true
- bar()
- end
- elseif e == "terminate" then
- os.pullEvent = oPull
- os.pullEventRaw = oPullRaw
- os.queueEvent("key",41)
- browserID = nil
- linksEnabled = nil
- debug = nil
- run = nil
- break
- elseif e == "key" and mb == 28 then
- if selected then
- selected = false
- site = typed
- typed = ""
- tPos = 1
- os.queueEvent("key",41)
- nPage = true
- history[chist+1] = site
- chist = chist+1
- if history[chist+1] then
- for i = chist+1, #history do
- history[i] = nil
- end
- end
- bar()
- end
- elseif e == "key" and mb == 14 then
- if selected then
- if tPos > 1 then
- typed = string.sub(typed,0,tPos-2)..string.sub(typed,tPos,#typed)
- tPos = tPos -1
- bar()
- end
- end
- elseif e == "key" and mb == keys.left then
- if selected then
- if tPos > 1 then
- tPos = tPos -1
- end
- end
- elseif e == "key" and mb == keys.right then
- if selected then
- if tPos < #typed+1 then
- tPos = tPos +1
- end
- end
- elseif e == "char" then
- if selected then
- typed = string.sub(typed,0,tPos-1)..mb..string.sub(typed,tPos,#typed)
- tPos = tPos+1
- bar()
- end
- elseif e == "mouse_click" then
- if clickx >= 7 and clicky == 1 then
- if not selected then
- if site ~= "main" then
- typed = site
- else
- typed = ""
- end
- tPos = #typed+1
- selected = true
- bar()
- end
- elseif clickx >= 1 and clickx <= 2 and clicky == 1 then
- if chist > 1 and history[chist-1] then
- chist = chist-1
- selected = false
- site = history[chist]
- typed = ""
- tPos = 1
- os.queueEvent("key",41)
- nPage = true
- bar()
- end
- elseif clickx >= 4 and clickx <= 5 and clicky == 1 then
- if chist < #history and history[chist+1] then
- chist = chist+1
- selected = false
- site = history[chist]
- typed = ""
- tPos = 1
- os.queueEvent("key",41)
- nPage = true
- bar()
- end
- end
- end
- end
- end
- function bar()
- term.redirect(cur)
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.lightGray)
- write("<-|-> ")
- term.setBackgroundColor(colors.lightGray)
- print(string.rep(" ",scrx-#"<-|-> "))
- term.setCursorPos(7,1)
- term.setTextColor(colors.black)
- write(string.sub(site,1,scrx-6))
- term.setTextColor(colors.lightGray)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(1,2)
- print(string.rep("_",scrx))
- term.redirect(wBar)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- if selected then
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- end
- if not selected then
- write(site)
- else
- write(typed)
- end
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- term.redirect(wPage)
- end
- if term.isColor() then
- site = ""
- typed = ""
- selected = false
- bar()
- parallel.waitForAny(pageSys,cBar)
- else
- print("You need an advanced computer for this program!")
- end
- os.pullEvent = oPull
- os.pullEventRaw = oPullRaw
Advertisement
Add Comment
Please, Sign In to add comment