Prolem examining JavaScript with

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi all,

I have the feeling I must have missed something in the documentation of
JavaScript::SpiderMonkey. It seems to execute simple JavaScript fine.
However, when generic browser elements like "window" or "document" are
referenced, an error occurs.

ReferenceError: document is not defined at line 0: (null)

I can work around this by creating such objects explicitly using the
object_by_path() method:

   use JavaScript::SpiderMonkey;
   my $js = new JavaScript::SpiderMonkey;
   my $document = $js->object_by_path("document");
   $js->function_set("write", sub {print "JS> @_\n"}, $document);
   $js->eval(qq{x=5; y=3; document.write(x+y);}) or die "js error: $@\n";

Although this works, it does not seem practical to me. Is there any
framework handling creation of objects existing in browsers?

The following js code also executes fine and prints "axc" as expecyed:
   x=new String("abc"); y = x.replace(/b/, "x"); document.write(y);

In a browser, however, I could do something like this, which in
SpiderMonkey would require implementing a self-written replace method
for the object "window.location.href".
   x=window.location.href.replace(/x/, "y");

I do not control the JavaScript I am going to inspect with SpiderMonkey.
So this workaround (implementing methods for used objects in perl) seems
unfeasible. It would be great if SpiderMonkey would know
window.location.href exists *and* really can be used as a string with
all its methods.

Any suggestions?

Re: Prolem examining JavaScript with

Quoted text here. Click to load it

That's because JavaScript::SpiderMonkey only interfaces with the
spidermonkey js interpreter. Which does not implement a browser model,
just the javascript core.

Quoted text here. Click to load it

Not as far as I know. You're probably much better off using
Mozilla::Mechanize, IE::Mechanize or WWW::Selenium, which run the
javascript code in an actual browser.

Quoted text here. Click to load it

Yes, that's part of the javscript core / ecmascript specification.

Quoted text here. Click to load it

I'm not sure what you'd expect that to do in SpiderMonkey, since there
is no window.

Quoted text here. Click to load it

Changing the location of a window in a browser does quite a bit more
than just changing the properties of a string, so again, I'm not sure
what you actually want to achieve.

Joost Diepenmaat | blog: | work: /

Re: Prolem examining JavaScript with

Quoted text here. Click to load it

Heh, I was right. If you're interested in an extra hand, drop me a line.

John - Hacking & Hiking in Mexico

Perl help in exchange for a gift:

Site Timeline