in reply to Re^4: Executing JavaScript function using WWW::Mechanize::Firefox
in thread Executing JavaScript function using WWW::Mechanize::Firefox

Not sure I understand what kind of a clue this is. Can you elaborate?

Its telling you there is no alert function because "this" is not a Window so there is no function "alert" -- even javascript has "namespaces"

See the docs and source code of https://metacpan.org/pod/WWW::Mechanize::Firefox#mech-eval_in_page-str-env-document and you'll understand.

  • Comment on Re^5: Executing JavaScript function using WWW::Mechanize::Firefox

Replies are listed 'Best First'.
Re^6: Executing JavaScript function using WWW::Mechanize::Firefox
by Anonymous Monk on Feb 17, 2016 at 03:11 UTC
    Actually I had been to that website before, but the thing is that I don't understand. It says "This allows access to variables and functions declared "globally" on the web page." I understand that somehow I am not in the right "context". But if I understand you correctly, then you are saying there is no way to get there?

      But if I understand you correctly, then you are saying there is no way to get there?

      No, I am saying you have to learn more about mozrepl/firefox/javascript and create your own one of these

      sub eval_in_page { my ($self,$str,$env,$doc,$window) = @_; $env ||= {}; my $js_env = {}; $doc ||= $self->document; # do a manual transfer of keys, to circumvent our stupid # transformation routine: if (keys %$env) { $js_env = $self->repl->declare(<<'JS')->(); function () { return new Object } JS for my $k (keys %$env) { $js_env->{$k} = $env->{$k}; }; }; my $eval_in_sandbox = $self->repl->declare(<<'JS', 'list'); function (w,d,str,env,caller,line) { var unsafeWin = w.wrappedJSObject; var safeWin = XPCNativeWrapper(unsafeWin); var sandbox = Components.utils.Sandbox(safeWin); sandbox.window = safeWin; sandbox.document = d; // Transfer the environment for (var e in env) { sandbox[e] = env[e] sandbox.window[e] = env[e] } sandbox.__proto__ = unsafeWin; var res = Components.utils.evalInSandbox(str, sandbox, "1.8",c +aller,line); return [res,typeof(res)]; }; JS $window ||= $self->tab->{linkedBrowser}->{contentWindow}; # Report errors from scope of caller # This feels weirdly backwards here, but oh well: #local @CARP_NOT = (ref $self->repl); # we trust this my ($caller,$line) = (caller)[1,2]; $eval_in_sandbox->($window,$doc,$str,$js_env,$caller,$line); };

        Okay, you clearly know more than I do, but you apparently enjoy giving cryptic answers.

        For all others that might have a similar problem that I had: Using an older version of Firefox fixed it.