Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^4: Accessing javascript variable from perl

by frazap (Monk)
on Jun 18, 2019 at 07:18 UTC ( [id://11101505]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Accessing javascript variable from perl
in thread Accessing javascript variable from perl

With the file jello.jshello.js
var leg = { aa: "Hello from javascript" } function to_json() { return JSON.stringify(leg); }
And the file perl.pl
use strict; use warnings; use WWW::Mechanize::PhantomJS; my $js = WWW::Mechanize::PhantomJS->new(launch_exe=> 'C:/prog/phantomj +s/bin/phantomjs.exe'); $js->get_local("hello.js"); #$js->update_html('<script src="hello.js" type="text/javascript"></scr +ipt>'); #print $js->content; #my ($val, $type) = $js->eval_in_page('JSON.stringify(window.leg);'); my ($val, $type) = $js->eval_in_page('to_json()'); print "val: $val, type: $type\n";
I can't have any meaningfull result from calling eval_in_page either on the leg var directly or via the function.

Can you show me how to have this working ?

Thanks

Update without phantomjs
use strict; use warnings; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($ERROR); use WWW::Mechanize::Chrome; my $js = WWW::Mechanize::Chrome->new(report_js_errors=>1); $js->get_local("hello.js"); #$js->update_html('<script src="hello.js" type="text/javascript"></scr +ipt>'); #print $js->content; #my ($val, $type) = $js->eval_in_page('JSON.stringify(window.leg);'); my ($val, $type) = $js->eval_in_page('to_json()'); print "val: $val, type: $type\n";

This gives undef for $val whatever I'm calling: the to_json() function or JSON.stringify directly.

Replies are listed 'Best First'.
Re^5: Accessing javascript variable from perl
by LanX (Saint) on Jun 18, 2019 at 08:15 UTC
    hello != jello

    > I can't have any meaningfull result ...

    Which error message do you get?

    I don't have phantomjs installed, so please provide a reproducible example.

    UPDATE

    I ran your JS code in the browser's console and it works for me:

    > var leg = { aa: "Hello from javascript" } function to_json() { return JSON.stringify(leg); } < undefined > JSON.stringify(leg); < "{\"aa\":\"Hello from javascript\"}" > to_json() < "{\"aa\":\"Hello from javascript\"}"

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^5: Accessing javascript variable from perl
by LanX (Saint) on Jun 18, 2019 at 11:15 UTC
    regarding your update:

    Neither do I have WWW::Mechanize::Chrome installed.

    My suggestions are

    • to make sure your JS is really executed (add an alert to show)
    • and this before you try eval_in_page (you could play around with sleep or a similar wait mechanism)
    HTH!

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      HTH

      Yes, it did: adding an alert("hello from js"); in the js file show me that a (the only ?) way to have it executed, was to load an existing html file. Using $js->modify_html('<script src="hello.js" type="text/javascript"></script>')on a blank page did not run the js file.

      So, I have to load this

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +"> <title>test</title> <script src="hello.js"></script> </head> <body> </body
      Then, the following worked:
      my $js = WWW::Mechanize::PhantomJS->new(launch_exe=> 'C:/prog/phantomj +s/bin/phantomjs.exe'); #or #my $js = WWW::Mechanize::Chrome->new(report_js_errors=>1); $js->get_local("hello.html"); my ($val, $type) = $js->eval_in_page('JSON.stringify(window.leg);'); #or #my ($val, $type) = $js->eval_in_page('JSON.stringify(leg);'); #or #my ($val, $type) = $js->eval_in_page('to_json()'); print "val: ", Dumper($val), "type: $type\n";

      Thanks

      frazap

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11101505]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-28 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found