This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
hego555's profile picture

Web Browser in CC

Started by hego555, 19 September 2012 - 07:18 AM
hego555 #1
Posted 19 September 2012 - 09:18 AM
You guys think it is possible to make a small web browser in Lua?

Like text based basically…

Ideas/Comments welcome!
Lyqyd #2
Posted 19 September 2012 - 03:45 PM
Yes, it is definitely possible. It's going to be fairly time-consuming, though. Even if you just make a browser that can handle HTML 4.0, no JavaScript etc., you're looking at having to put together a full parser for HTML (unless you can find one and adapt it–unlikely) that can create text on the screen that bears some resemblance to the actual web page.

This is not any easy one, but it most certainly is possible.
Cranium #3
Posted 19 September 2012 - 04:56 PM
I know some people have made a miniature web browser already in CC. It does not use the HTTP API, but you can probably make something with the HTTP if you wanted.
Noodle #4
Posted 19 September 2012 - 10:06 PM
Lua is good for this stuff! Get the xml reader and it'll operate just like it. It'll take the tags out of them and css won't be much of use, but surely you could make one.
Spoilerfunction
parseargs (s)
local arg = {}
gsub(s, "(%w+)=(["'])(.-)%2", function (w, _, a)
%arg[w] = a
end)
return arg
end

function collect (s)
local stack = {n=0}
local top = {n=0}
tinsert(stack, top)
local ni,c,label,args, empty
local i, j = 1, 1
while 1 do
ni,j,c,label,args, empty = strfind(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
if not ni then break end
local text = strsub(s, i, ni-1)
if not strfind(text, "^%s*$") then
tinsert(top, text)
end
if empty == "/" then – empty element tag
tinsert(top, {n=0, label=label, args=parseargs(args), empty=1})
elseif c == "" then – start tag
top = {n=0, label=label, args=parseargs(args)}
tinsert(stack, top) – new level
else – end tag
local toclose = tremove(stack) – remove top
top = stack[stack.n]
if stack.n < 1 then
error("nothing to close with "..label)
end
if toclose.label ~= label then
error("trying to close "..toclose.label.." with "..label)
end
tinsert(top, toclose)
end
i = j+1
end
local text = strsub(s, i)
if not strfind(text, "^%s*$") then
tinsert(stack[stack.n], text)
end
if stack.n > 1 then
error("unclosed "..stack[stack.n].label)
end
return stack[1]
end


– example

x = collect[[
<methodCall kind="xuxu">
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>41</i4></value>
</param>
</params>
</methodCall>

]]

From the LuaXml place
hego555 #5
Posted 19 September 2012 - 11:44 PM
I was thinking creating a new semi-language for the web browser, so the websites and computers can interact and send and receive commands or really anything!
Cranium #6
Posted 19 September 2012 - 11:46 PM
If you feel like creating that language, yes. If you want to create a new "rednet standard protocol", then by all means, do so. That is the magic of Lua, it can work with ANYTHING.
Cranium #7
Posted 19 September 2012 - 11:52 PM
Just make sure you include THIS.
hego555 #8
Posted 20 September 2012 - 12:08 AM
If you feel like creating that language, yes. If you want to create a new "rednet standard protocol", then by all means, do so. That is the magic of Lua, it can work with ANYTHING.

Only problem I can think of is styling!!!

See the problem is, I cant really think of a way to lets say move text to the right side of the screen, or the middle, or both!

Like this

——————————————————————
LEFT SIDE MIDDLE RIGHT SIDE
——————————————————————
Cranium #9
Posted 20 September 2012 - 12:19 AM
*Ahem*
I'll just leave this shameless self plug here….. :)/>/>
hego555 #10
Posted 20 September 2012 - 03:13 AM
Hmm, I will install that on the server!!!

All is falling into place, but I frankly dont want dependencies!
aw well!
Jan #11
Posted 20 September 2012 - 10:12 AM
Just make sure you include THIS.
That' s not a bad idea:
When you have industrialcraft (which has coffee now), you can warm your coffee via rednet! HAYO!
Noodle #12
Posted 20 September 2012 - 11:14 PM
Styling is easy with tags..
x, y = term.getSize()
If tag == "<center>" then
  (x/2) - (string.len(INNERTEXT)/2)
  print(INNERTEXT)
end