[jruby-user] DOM manipulation
Rodrigo Botafogo <[email protected]> Fri, 8 May 2015 14:49:34 -0300
| Newsgroups | gmane.comp.lang.jruby.user |
|---|---|
| Message-ID | <CAAKc=GCZNocg7dDBKUEqtwLb12gSChq89Yr+H+p6S-a-Ei+9XA@mail.gmail.com> |
--001a113ec6d2eac1d4051595a7a1
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Hello...
I=C2=B4m trying to manipulate the DOM directly through JRubyFX, but I=C2=B4=
m not
getting it right. Here is my code (which is basically what I found on
JRubyFX wiki):
require 'jrubyfx'
class HelloWorldApp < JRubyFX::Application
def start(stage)
browser =3D WebView.new
web_engine =3D browser.getEngine()
# get the window
window =3D web_engine.executeScript("window")
# get the HTMLDocument
document =3D window.eval("document")
// Loads a very simple HTML file that just shows the words 'hello world=
'
f =3D Java::JavaIo.File.new("simple.html")
fil =3D f.toURI().toURL().toString()
web_engine.load(fil)
web_engine.setJavaScriptEnabled(true)
elmts =3D document.body.getElementsByTagName("*")
p elmts // #<Java::ComSunWebkitDom::NodeListImpl:0x1cf5189>
p elmts.getLength() // 0: no elements
elm =3D document.getElementsByClassName("body")
p elm.getLength() // 0: no body
para =3D document.createElement("p")
node =3D document.createTextNode("This is new")
para.appendChild(node)
elmts =3D document.body.getElementsByTagName("*")
p elmts // #<Java::ComSunWebkitDom::NodeListImpl:0x11d2018>
p elmts.getLength() // still prints 0
with(stage, title: "Hello World!", width: 800, height: 600) do
layout_scene do
browser // opens a browser windows with a string
end
end
stage.show # Tip: most of the time, () can be removed from method calls
end
end
HelloWorldApp.launch
Any hint is appreciated!
Thanks,
--=20
Rodrigo Botafogo
--001a113ec6d2eac1d4051595a7a1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello...<div><br></div><div>I=C2=B4m trying to manipulate =
the DOM directly through JRubyFX, but I=C2=B4m not getting it right.=C2=A0 =
Here is my code (which is basically what I found on JRubyFX wiki):</div><di=
v><br></div><div><br></div><div><br></div><div><div>require 'jrubyfx=
9;</div><div><br></div><div>class HelloWorldApp < JRubyFX::Application</=
div><div><br></div><div>=C2=A0 def start(stage)</div><div><br></div><div>=
=C2=A0 =C2=A0 browser =3D WebView.new</div><div>=C2=A0 =C2=A0 web_engine =
=3D browser.getEngine()</div><div>=C2=A0 =C2=A0 # get the window</div><div>=
=C2=A0 =C2=A0 window =3D web_engine.executeScript("window")</div>=
<div>=C2=A0 =C2=A0 # get the HTMLDocument</div><div>=C2=A0 =C2=A0 document =
=3D window.eval("document")</div><div><br></div><div>=C2=A0 =C2=
=A0 // Loads a very simple HTML file that just shows the words 'hello w=
orld'</div><div>=C2=A0 =C2=A0 f =3D Java::JavaIo.File.new("simple.=
html")<br></div><div><br></div><div>=C2=A0 =C2=A0 fil =3D f.toURI().to=
URL().toString()</div><div>=C2=A0 =C2=A0 web_engine.load(fil)</div><div>=C2=
=A0 =C2=A0 web_engine.setJavaScriptEnabled(true)</div><div><br></div><div>=
=C2=A0 =C2=A0 elmts =3D document.body.getElementsByTagName("*")</=
div><div>=C2=A0 =C2=A0 p elmts //=C2=A0#<Java::ComSunWebkitDom::NodeList=
Impl:0x1cf5189></div><div>=C2=A0 =C2=A0 p elmts.getLength() // 0: no ele=
ments</div><div><br></div><div>=C2=A0 =C2=A0 elm =3D document.getElementsBy=
ClassName("body")</div><div>=C2=A0 =C2=A0 p elm.getLength() // 0:=
no body</div><div><br></div><div>=C2=A0 =C2=A0 para =3D document.createEle=
ment("p")</div><div>=C2=A0 =C2=A0 node =3D document.createTextNod=
e("This is new")</div><div>=C2=A0 =C2=A0 para.appendChild(node)</=
div><div><br></div><div>=C2=A0 =C2=A0 elmts =3D document.body.getElementsBy=
TagName("*")</div><div>=C2=A0 =C2=A0 p elmts //=C2=A0#<Java::C=
omSunWebkitDom::NodeListImpl:0x11d2018></div><div>=C2=A0 =C2=A0 p elmts.=
getLength() =C2=A0// still prints 0</div><div><br></div><div>=C2=A0 =C2=A0 =
with(stage, title: "Hello World!", width: 800, height: 600) do</d=
iv><div>=C2=A0 =C2=A0 =C2=A0 layout_scene do</div><div>=C2=A0 =C2=A0 =C2=A0=
=C2=A0 browser =C2=A0// opens a browser windows with a string</div><div>=
=C2=A0 =C2=A0 =C2=A0 end</div><div>=C2=A0 =C2=A0 end</div><div><br></div><d=
iv>=C2=A0 =C2=A0 stage.show # Tip: most of the time, () can be removed from=
method calls</div><div><br></div><div>=C2=A0 end</div><div><br></div><div>=
end</div><div><br></div><div>HelloWorldApp.launch</div><div><br></div><div>=
<br></div><div>Any hint is appreciated!</div><div><br></div><div><br></div>=
<div>Thanks,</div><div><br></div>-- <br><div class=3D"gmail_signature"><div=
dir=3D"ltr">Rodrigo Botafogo<div><br><br><div><br></div></div></div></div>
</div></div>
--001a113ec6d2eac1d4051595a7a1--