Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks, I am using the following snippet to execute a JavaScript function using MozRepl. In reality, I am trying to do it from an actual website, but for demo purposes, this appears to be sufficient, because even in this case I get the error message "MozRepl::RemoteObject: TypeError: myFunction is not a function at test.pl line 16." What am I doing wrong?
use v5.10; use WWW::Mechanize::Firefox; use strict; use warnings; my $mech = WWW::Mechanize::Firefox->new(); $mech->update_html(<<HTML); <script type="text/javascript"> function myFunction() { alert("Hello World!"); } </script> HTML sleep 2; $mech->eval_in_page( 'myFunction();' );

Replies are listed 'Best First'.
Re: Executing JavaScript function using WWW::Mechanize::Firefox
by Anonymous Monk on Feb 17, 2016 at 01:55 UTC
    Try this  ....eval_in_page( $js ); where
    my $js = <<'__JS__'; (function(){ function myFunction() { alert("Hello World!"); } myFunction(); })(); __JS__

      Thanks, but that yields the error "MozRepl::RemoteObject: TypeError: 'alert' called on an object that does not implement interface Window. at test_js.pl line 23."

      Also, my problem is that in my use case, I don't write the JavaScript myself -- I am trying to have Firefox use a website that has a couple of functions which I want to call.

        Thanks, but that yields the error "MozRepl::RemoteObject: TypeError: 'alert' called on an object that does not implement interface Window. at test_js.pl line 23."

        That is an important message and clue about the environment you're facing

        Also, my problem is that in my use case, I don't write the JavaScript myself -- I am trying to have Firefox use a website that has a couple of functions which I want to call.

        Can you get firefox to do that without involving perl ?

        Because browsers kind of go out of their way to forbid such tricks because of security issues