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

The errorMessages are explicitly given!

HINT: var denotes a lexical var inside a function°, it only becomes global* if used outside.

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

updates

°) mostly like my in Perl except for the hoisting effect

*) in this case it behaves mostly like our in Perl

Replies are listed 'Best First'.
Re^4: Accessing javascript variable from perl
by frazap (Monk) on Jun 18, 2019 at 07:18 UTC
    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.

      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

      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