Login Register






[Lua] simple web browser 1.41 filter_list
Author
Message
[Lua] simple web browser 1.41 #1
1.41

Code:
-------------------------------------------------------------------------------- -- -- -- Simple Web Browser -- -- -- -------------------------------------------------------------------------------- -- -- -- You're allowed to edit and/or give away this code for free as long as you -- -- give credits. -- -- -- -------------------------------------------------------------------------------- -- -- -- simple web browser · coded by noize · 2013 -- -- -- -------------------------------------------------------------------------------- require("iuplua") require("iupluaole") require("iupluacontrols") require("luacom") local http = require("socket.http") local ltn12 = require("ltn12") local tmp = "tempSourceFile.data" -- temporary file to store page sources function readf(file) local f = io.open(file, "r") -- define the handle/open the file if not f then return nil end -- file does not exist local data = f:read("*a") -- read the file content f:close() -- close the file handle return data -- return the file content end function writef(file, text) local f = io.open(file, "w") -- open the file f:write(text) -- write to file f:close() -- close the file handle end local tree = iup.tree {} local filebox = iup.text {border = "YES", expand = "HORIZONTAL"} function save() local sourceData = readf(tmp) -- read the temporary source file if sourceData then -- if the file exists local popup = {} popup.text = {} popup.ok = {} popup.cancel = {} for i = 1,2 do popup.text[i] = iup.text {border = "YES", expand = "YES"} popup.cancel[i] = iup.button { title = "Cancel", size = "EIGHTHxEIGHTH", action = function() return iup.CLOSE end } end popup.ok[1] = iup.button { title = "OK", size = "EIGHTHxEIGHTH", action = function() tree.name = popup.text[1].value return iup.CLOSE end } popup.ok[2] = iup.button { title = "OK", size = "EIGHTHxEIGHTH", action = function() return iup.CLOSE end } local renbox = iup.dialog { iup.vbox { popup.text[1], iup.hbox {popup.ok[1],popup.cancel[1]} }; defaultenter = popup.ok[1], defaultesc = popup.cancel[1], title = "Rename", size = "180", startfocus = popup.text[1] } local newbox = iup.dialog { iup.vbox { popup.text[2], iup.hbox {popup.ok[2],popup.cancel[2]} }; defaultenter = popup.ok[2], defaultesc = popup.cancel[2], title = "New directory", size = "180", startfocus = popup.text[2] } function renbox:k_any(key) -- when any key is pressed if popup.text[1].value == tree.name and key ~= 13 and key ~= 32 and key ~= 139 then -- if the value of the box is the same as the name of the node and the pressed key is not ENTER or SPACE or DEL popup.text[1].value = "" -- the value of the box is overridden end end -- right click menu local addbranch = iup.item {title = "New directory"} local renamenode = iup.item {title = "Rename"} local deletenode = iup.item {title = "Delete"} local menu = iup.menu {addbranch, renamenode, deletenode} function tree:rightclick_cb(id) -- on right click tree.value = id -- select node menu:popup(iup.MOUSEPOS,iup.MOUSEPOS) -- popup where the mouse is end function exeleaf() -- rename popup.text[1].value = tree.name -- put the current filename in the textbox renbox:popup(iup.CENTER, iup.CENTER) -- popup the rename box iup.SetFocus(tree) -- focus back on the tree end function tree:executeleaf_cb(id) -- on double click on leaf filebox.value = tree.name exeleaf() end function removenode() local choice = iup.Alarm("Warning", "Do you want to delete " .. tree.name .. "?", "No", "Yes", "Cancel") if choice == 2 then os.remove(tree.name) tree.delnode = "MARKED" end end function addbranch:action() -- new directory local dir = {} dir.name = "New directory" function newbox:k_any(key) -- when any key is pressed if popup.text[2].value == dir.name and key ~= 13 and key ~= 32 and key ~= 139 then -- if the value of the box is the same as the name of the node and the pressed key is not ENTER or SPACE or DEL popup.text[2].value = "" -- the value of the box is overridden end end -- if exists dir.name then -- dir.i = 1 -- while dir_exists "New directory (" .. dir.i .. ")" do -- dir.i = dir.i + 1 -- end -- dir.name = "New directory (" .. dir.i .. ")" -- end popup.text[2].value = dir.name newbox:popup() dir.name = popup.text[2].value -- while exists dir.name then -- error messagebox -- newbox:popup() -- dir.name = popup.text[2].value -- end -- mkdir dir.name -- if exists dir.name then tree.addbranch = dir.name -- else -- iup.Message("Error","Failed to create the directory\nThis is usually caused by either an invalid directory name or by privilege lacks") end function renamenode:action() tree:executeleaf_cb(tree.value) end function deletenode:action() removenode() end function saved() writef(filedlg.value,sourceData) iup.Message("File saved","Source was successfully saved to " .. filebox.value) end dlg = iup.dialog {iup.vbox {tree, filebox, iup.fill{}}; title = "Save source", size = "390x190"} dlg:showxy(iup.CENTER,iup.CENTER) tree.name = "File system" local drive = os.getenv("systemdrive") if drive then for dir in (io.popen([[dir "]] .. drive .. [[\" /b /ad]]):lines()) do tree.addbranch = dir end else for dir in (io.popen([[ls -a /]]):lines()) do tree.addbranch = dir end end tree.value = "0" -- get files in current directory filebox.value = tree.name function tree:k_any(key) -- when any key is pressed if key == 13 then -- if it is the ENTER key exeleaf() -- popup the rename box elseif key == 139 then -- if it is the DEL key removenode() -- delete the file or directory elseif key == 141 or key == 593 then return dlg:destroy() end if key ~= 130 and key ~= 136 then filebox.value = tree.name end end if (iup.MainLoopLevel()==0) then iup.MainLoop() end else iup.Message("Error","No temporary source file found\nPlease, download a page first") end end function clearTemp() os.remove(tmp) local f = io.open(tmp,"r") if not f then f:close() iup.Message("File removed","Temp file was successfully deleted") else iup.Message("Error","Failed to remove the temp file") end end local ctrl = {} for i = 1, 11, 1 do ctrl[i] = iup.olecontrol{"Shell.Explorer"} ctrl[i]:CreateLuaCOM() ctrl[i].designmode = "NO" end local gui = {} gui.bar = {} gui.button = {} gui.tab = {} gui.label = {} gui.label.source = iup.label {expand = "NO", title= " http://", size="30x10"} for i = 1, 11, 1 do gui.bar[i] = iup.text {expand = "HORIZONTAL", tip = "URL box"} gui.button[i] = iup.button { title = "Go!", tip = "Load", size = "45x11", action = function() ctrl[i].com:Navigate(gui.bar[i].value) end } if i < 10 then gui.tab[i] = iup.vbox { iup.hbox {gui.bar[i], gui.button[i]}, ctrl[i], tabtitle = "Tab " .. i, } elseif i == 10 then gui.tab[i] = iup.vbox { iup.hbox { gui.bar[10], iup.button { title = "Go!", tip = "Load the page", size = "45x11", action = function() for j = 1, 10, 1 do ctrl[j].com:Navigate(gui.bar[i].value) end end } }, iup.label { expand = "NO", size = "0x19", title = " The navigation on this page overwhelms all the others." }, ctrl[i], tabtitle = "CTRL tab" } elseif i == 11 then gui.tab[i] = iup.vbox { iup.hbox { gui.label.source,gui.bar[i], iup.button { title = "Go!", tip = "Download the page", size = "45x11", action = function() httpget(gui.bar[i].value) ctrl[i].com:Navigate(gui.bar[i].value) end } }, iup.hbox { iup.button { expand = "HORIZONTAL", size = "x16", title = "Save source to file", action = function() save() end} }, iup.hbox { iup.button { expand = "HORIZONTAL", size = "x16", title = "Delete temporary source file", action = function() clearTemp() end} }, ctrl[i], tabtitle = "Source tab" } end gui.bar.i = gui.bar[i] function gui.bar.i:k_any(key) if key == 13 or key == 10 then ctrl[i].com:Navigate(gui.bar[i].value) if i == 11 then httpget(gui.bar[i].value) end end end end local tabs_arr = {expand = "YES"} for i = 1, 11 do table.insert(tabs_arr,gui.tab[i]) end local tabs = iup.tabs (tabs_arr) function httpget() if gui.bar[11].value ~= "" then http.request { url = "http://" .. gui.bar[11].value, sink = ltn12.sink.file(io.open(tmp,"w")) } if readf(tmp) == nil or readf(tmp) == "" then iup.Message("Target: " .. gui.bar[11].value,"No data downloaded") else os.execute(tmp) end else iup.Message("Error","No URL to download") end end ctrl[1].com:Navigate("www.google.com") dlg = iup.dialog {tabs; title="Simple Web Browser 1.3", size="440x180", background="255 255 255"} function dlg:k_any(key) -- when any key is pressed if key == 593 then -- if it's CTRL + Q os.exit() -- quit end end dlg:show() iup.SetFocus(gui.bar[1]) iup.MainLoop()

Unstable version, hoping to post a 1.5 stable version soon.



V1.3 Alpha
find the old versions here

I just looked back at the source code I had used for the older versions and I thought, like, "this is shit". So I handed it all back again, still some things I'd like to get better, but this is yet far better than the older versions.

changelog:
- About tab removed
- minor edits in the GUI
- great edits in the source code, far DRYer now
- page browsing (like in other tabs) is now also available in the source tab
- great changes in the save function in the source (it now takes alone about half of the whole code)

Now, the source tab is not ready for a release, so, this will be the alpha version of the new version of swb. That's as well what needs more feedback, so, if you could tell me if in the save dialog you find a tree of your main drive (in Windows) or of your "/" directory (in Unix-like systems), it would be appreciated. If you're using (or you have easy access to) any less common OS, I would really like to know about the output of the tree over there.

Please, report any bug or suggestion regarding any other tab. Reporting bugs/suggestions about the source tab is not forbidden as well, it's just that I'll most likely be already aware about it.

Not sure about why I took up such a thing like this way to save files while I could have used OS-provided dialogs. This program will be most likely remarkable for the save() function only.

Code:
-------------------------------------------------------------------------------- -- -- -- Simple Web Browser -- -- -- -------------------------------------------------------------------------------- -- -- -- You're allowed to edit and/or give away this code for free as long as you -- -- give credits. -- -- -- -------------------------------------------------------------------------------- -- -- -- simple web browser · coded by noize · 2013 -- -- -- -------------------------------------------------------------------------------- require("iuplua") require("iupluaole") require("iupluacontrols") require("luacom") local http = require("socket.http") local ltn12 = require("ltn12") local tmp = "tempSourceFile.data" -- temporary file to store page sources function readf(file) local f = io.open(file, "r") -- define the handle/open the file if not f then return nil end -- file does not exist local data = f:read("*a") -- read the file content f:close() -- close the file handle return data -- return the file content end function writef(file, text) local f = io.open(file, "w") -- open the file f:write(text) -- write to file f:close() -- close the file handle end local tree = iup.tree {} local filebox = iup.text {border = "YES", expand = "HORIZONTAL"} function save() local sourceData = readf(tmp) -- read the temporary source file if sourceData then -- if the file exists local popup = {} popup.text = {} popup.ok = {} popup.cancel = {} for i = 1,2 do popup.text[i] = iup.text {border = "YES", expand = "YES"} popup.cancel[i] = iup.button { title = "Cancel", size = "EIGHTHxEIGHTH", action = function() return iup.CLOSE end } end popup.ok[1] = iup.button { title = "OK", size = "EIGHTHxEIGHTH", action = function() tree.name = popup.text[1].value return iup.CLOSE end } popup.ok[2] = iup.button { title = "OK", size = "EIGHTHxEIGHTH", action = function() return iup.CLOSE end } local renbox = iup.dialog { iup.vbox { popup.text[1], iup.hbox {popup.ok[1],popup.cancel[1]} }; defaultenter = popup.ok[1], defaultesc = popup.cancel[1], title = "Rename", size = "180", startfocus = popup.text[1] } local newbox = iup.dialog { iup.vbox { popup.text[2], iup.hbox {popup.ok[2],popup.cancel[2]} }; defaultenter = popup.ok[2], defaultesc = popup.cancel[2], title = "New directory", size = "180", startfocus = popup.text[2] } function renbox:k_any(key) -- when any key is pressed if popup.text[1].value == tree.name and key ~= 13 and key ~= 32 and key ~= 139 then -- if the value of the box is the same as the name of the node and the pressed key is not ENTER or SPACE or DEL popup.text[1].value = "" -- the value of the box is overridden end end -- right click menu local addbranch = iup.item {title = "New directory"} local renamenode = iup.item {title = "Rename"} local deletenode = iup.item {title = "Delete"} local menu = iup.menu {addbranch, renamenode, deletenode} function tree:rightclick_cb(id) -- on right click tree.value = id -- select node menu:popup(iup.MOUSEPOS,iup.MOUSEPOS) -- popup where the mouse is end function exeleaf() -- rename popup.text[1].value = tree.name -- put the current filename in the textbox renbox:popup(iup.CENTER, iup.CENTER) -- popup the rename box iup.SetFocus(tree) -- focus back on the tree end function tree:executeleaf_cb(id) -- on double click on leaf filebox.value = tree.name exeleaf() end function removenode() local choice = iup.Alarm("Warning", "Do you want to delete " .. tree.name .. "?", "No", "Yes", "Cancel") if choice == 2 then os.remove(tree.name) tree.delnode = "MARKED" end end function addbranch:action() -- new directory local dir = {} dir.name = "New directory" function newbox:k_any(key) -- when any key is pressed if popup.text[2].value == dir.name and key ~= 13 and key ~= 32 and key ~= 139 then -- if the value of the box is the same as the name of the node and the pressed key is not ENTER or SPACE or DEL popup.text[2].value = "" -- the value of the box is overridden end end -- if exists dir.name then -- dir.i = 1 -- while dir_exists "New directory (" .. dir.i .. ")" do -- dir.i = dir.i + 1 -- end -- dir.name = "New directory (" .. dir.i .. ")" -- end popup.text[2].value = dir.name newbox:popup() dir.name = popup.text[2].value -- while exists dir.name then -- error messagebox -- newbox:popup() -- dir.name = popup.text[2].value -- end -- mkdir dir.name -- if exists dir.name then tree.addbranch = dir.name -- else -- iup.Message("Error","Failed to create the directory\nThis is usually caused by either an invalid directory name or by privilege lacks") end function renamenode:action() tree:executeleaf_cb(tree.value) end function deletenode:action() removenode() end function saved() writef(filedlg.value,sourceData) iup.Message("File saved","Source was successfully saved to " .. filebox.value) end dlg = iup.dialog {iup.vbox {tree, filebox, iup.fill{}}; title = "Save source", size = "390x190"} dlg:showxy(iup.CENTER,iup.CENTER) tree.name = "File system" local drive = os.getenv("systemdrive") if drive then for dir in (io.popen([[dir "]] .. drive .. [[\" /b /ad]]):lines()) do tree.addbranch = dir end else for dir in (io.popen([[ls -a /]]):lines()) do tree.addbranch = dir end end tree.value = "0" -- get files in current directory filebox.value = tree.name function tree:k_any(key) -- when any key is pressed if key == 13 then -- if it is the ENTER key exeleaf() -- popup the rename box elseif key == 139 then -- if it is the DEL key removenode() -- delete the file or directory elseif key == 141 or key == 593 then return dlg:destroy() end if key ~= 130 and key ~= 136 then filebox.value = tree.name end end if (iup.MainLoopLevel()==0) then iup.MainLoop() end else iup.Message("Error","No temporary source file found\nPlease, download a page first") end end function clearTemp() os.remove(tmp) local f = io.open(tmp,"r") if not f then f:close() iup.Message("File removed","Temp file was successfully deleted") else iup.Message("Error","Failed to remove the temp file") end end local ctrl = {} for i = 1, 11, 1 do ctrl[i] = iup.olecontrol{"Shell.Explorer"} ctrl[i]:CreateLuaCOM() ctrl[i].designmode = "NO" end local gui = {} gui.bar = {} gui.button = {} gui.tab = {} gui.label = {} gui.label.source = iup.label {expand = "NO", title= " http://", size="30x10"} for i = 1, 11, 1 do gui.bar[i] = iup.text {expand = "HORIZONTAL", tip = "URL box"} gui.button[i] = iup.button { title = "Go!", tip = "Load", size = "45x11", action = function() ctrl[i].com:Navigate(gui.bar[i].value) end } if i < 10 then gui.tab[i] = iup.vbox { iup.hbox {gui.bar[i], gui.button[i]}, ctrl[i], tabtitle = "Tab " .. i, } elseif i == 10 then gui.tab[i] = iup.vbox { iup.hbox { gui.bar[10], iup.button { title = "Go!", tip = "Load the page", size = "45x11", action = function() for j = 1, 10, 1 do ctrl[j].com:Navigate(gui.bar[i].value) end end } }, iup.label { expand = "NO", size = "0x19", title = " The navigation on this page overwhelms all the others." }, ctrl[i], tabtitle = "CTRL tab" } elseif i == 11 then gui.tab[i] = iup.vbox { iup.hbox { gui.label.source,gui.bar[i], iup.button { title = "Go!", tip = "Download the page", size = "45x11", action = function() httpget(gui.bar[i].value) ctrl[i].com:Navigate(gui.bar[i].value) end } }, iup.hbox { iup.button { expand = "HORIZONTAL", size = "x16", title = "Save source to file", action = function() save() end} }, iup.hbox { iup.button { expand = "HORIZONTAL", size = "x16", title = "Delete temporary source file", action = function() clearTemp() end} }, ctrl[i], tabtitle = "Source tab" } end gui.bar.i = gui.bar[i] function gui.bar.i:k_any(key) if key == 13 then ctrl[i].com:Navigate(gui.bar[i].value) if i == 11 then httpget(gui.bar[i].value) end end end end local tabs = iup.tabs { gui.tab[1], gui.tab[2], gui.tab[3], gui.tab[4], gui.tab[5], gui.tab[6], gui.tab[7], gui.tab[8], gui.tab[9], gui.tab[10], gui.tab[11], expand="YES" } function httpget() if gui.bar[11].value ~= "" then http.request { url = "http://" .. gui.bar[11].value, sink = ltn12.sink.file(io.open(tmp,"w")) } if readf(tmp) == nil or readf(tmp) == "" then iup.Message("Target: " .. gui.bar[11].value,"No data downloaded") else os.execute(tmp) end else iup.Message("Error","No URL to download") end end ctrl[1].com:Navigate("www.google.com") dlg = iup.dialog {tabs; title="Simple Web Browser 1.3", size="440x180", background="255 255 255"} function dlg:k_any(key) -- when any key is pressed if key == 593 then -- if it's CTRL + Q os.exit() -- quit end end dlg:show() iup.SetFocus(gui.bar[1]) iup.MainLoop()

I'm not able to upload any screenshot/demo-video at the moment, but I will. In the while, if you don't have Lua installed, you can have a look at the demonstration videos of the old versions. It's not the same, but for many aspects of the GUI of the main tabs it's nearly the same.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: [Lua] simple web browser 1.3 #2
update: version 1.41 out now.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: [Lua] simple web browser 1.3 #3
update: version 1.41 out now.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: [Lua] simple web browser 1.41 #4
Very cool. Ill look more into it later.
[Image: 2YpkRjy.png] Follow me on Twitter @jessehorne27

Reply