in reply to Re^3: 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?

Manual operation of the website works just fine! And since ReplMoz is inside the browser, I should not be caught by the browser doing anything fishy, right?

Here is what the actual link in the website I am facing looks like:

<a href='#' onclick='goToPage(2);return false;'>Next &gt;</a>

And yes, I know, I can click that link in a different way, but the thing is that I want to be able to tell it to which page it should go, i.e. I want to call goToPage with a parameter of my choosing.

Replies are listed 'Best First'.
Re^5: Executing JavaScript function using WWW::Mechanize::Firefox
by Anonymous Monk on Feb 17, 2016 at 03:03 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); };