in reply to SpiderMonkey and JS

the js object must know about the page structure in order to be able to traverse the DOM and apply the getElementById to it. there should be a method of the SpiderMonkey object which does just that. By reading the docs I see the only way to load a particular page would be for you to  document.location.href = that_page and eval that with the SpiderMonkey object,and only after that's finished , and will hopefully make a HTTP request(you should check that with ngrep for example) , will you be able to do anything with the DOM.

As an alternative and more DIY option I suggest you write a Firefox extension(XUL) which communicates through AJAX with a Perl script and automate your task that way , you'll be able to have complete control over what you're doing and instead of having to deal with the potential bugs of SpiderMonkey or it's counterpart CPAN module you'll only have to deal with Firefox bugs(which is pretty stable IMHO).

Replies are listed 'Best First'.
Re^2: SpiderMonkey and JS
by Anonymous Monk on May 01, 2009 at 19:32 UTC
    well...I think I'm still missing something...
    use warnings; use strict; use LWP::UserAgent; use HTML::Parser; use JavaScript::SpiderMonkey; use Data::Dumper; my $base = 'http://www.myspace.com'; my $js = JavaScript::SpiderMonkey->new(); $js->init(); $js->function_set("SMSTokenValue", sub { print "@_\n"; }); $js->property_by_path("document.getElementById"); { my $ua = new LWP::UserAgent(); my $req = new HTTP::Request ('GET', $base); my $res = $ua->request($req); if (!($res->is_success)){ warn "Warning:".$res->message."\n"; } else { print "Successful\n"; $js->property_by_path("document.location.href"); my $rc = $js->eval(q! document.location.href = append("http://", "www.myspace.com"); document.getElementById = append("ctl00_ctl00_cpMain_cpMain_LoginB +ox_SMSVerifiedCookieToken"); SMSTokenValue("Token: ", document.getElementById); function append(first, second) { return first + ' = ' + second; } !); $js->destroy(); } }
    The value comes back as undefined...Am I missing something else?
        $js->property_by_path("document.location.href");

      here you're not actually setting aything , please read here how property_by_path is properly used.

      document.location.href = append("http://", "www.myspace.com"); document.getElementById = append("ctl00_ctl00_cpMain_cpMain_LoginB +ox_SMSVerifiedCookieToken");

      if you would have used property_by_path correctly above there would've been no need for setting location.href again inside javascript . however if you decide to do it in js you need to keep in mind that it won't take effect immediately and you must somehow wait until the page is loaded.

      P.S. see this tutorial by Limbic~Region where he describes how WWW::Selenium is used in a similar situation.