in reply to Re^6: fetch HTML page, do javascript, read output from alert/msgbox
in thread fetch HTML page, do javascript, read output from alert/msgbox

sub eval_in_page_with_redefined_alert { my ($self,$str,$redefined_alert) = @_; my $eval_in_sandbox = $self->repl->declare(<<'JS'); function (w,str,myalert) { var unsafeWin = w.wrappedJSObject; var safeWin = XPCNativeWrapper(unsafeWin); var sandbox = Components.utils.Sandbox(safeWin); sandbox.window = safeWin; sandbox.window.alert = myalert; // <-- sandbox.alert = myalert; // <-- sandbox.document = sandbox.window.document; sandbox.__proto__ = unsafeWin; var res = Components.utils.evalInSandbox(str, sandbox); return [res,typeof(res)]; }; JS my $window = $self->tab->{linkedBrowser}->{contentWindow}; my $uri = $self->uri; return @{ $eval_in_sandbox->("$uri",$window,$redefined_alert ) }; };

and call it as

eval_in_page_with_redefined_alert($mech,$str, sub { print "Caught aler +t(): @_" });

If that doesn't work for you, something likely is wrong with my Javascript. You'll then have to look at the Mozilla FireFox Javascript documentation to figure out how things should be done instead.

Replies are listed 'Best First'.
Re^8: fetch HTML page, do javascript, read output from alert/msgbox
by TheFlyingCorpse (Novice) on Nov 11, 2009 at 14:54 UTC
    MozRepl::RemoteObject: [Exception... "Illegal value" nsresult: "0x800 +70057 (NS_ERROR _ILLEGAL_VALUE)" location: "JS frame :: chrome://mozrepl/content/repl +.js -> file:/// C:/Documents%20and%20Settings/Administrator/Programdata/Mozilla/Firefo +x/Profiles/kaq7 hscq.default/mozrepl.tmp.js :: anonymous :: line 5" data: no] at C:\D +ocuments and Se ttings\Administrator\Skrivebord\PRINTLOOKER\firefox.pl line 40

    I get this error when running this script:

    #Start use WWW::Mechanize::FireFox #URL $url = 'http://host/url/site'; #New instance my $mech = WWW::Mechanize::FireFox->new(); #Exitkode my $EXITCODE = 0; #Get content of URL $mech->get($url); $str = 'javascript:InstallPrinter()'; eval_in_page_with_redefined_alert($mech,$str, sub { print "Caught aler +t(): @_" }); #$eval_in_page=>('javascript:InstallPrinter()'); #$mech->get('javascript:InstallPrinter()'); sub eval_in_page_with_redefined_alert { my ($self,$str,$redefined_alert) = @_; my $eval_in_sandbox = $self->repl->declare(<<'JS'); function (w,str,myalert) { var unsafeWin = w.wrappedJSObject; var safeWin = XPCNativeWrapper(unsafeWin); var sandbox = Components.utils.Sandbox(safeWin); sandbox.window = safeWin; sandbox.window.alert = myalert; // <-- sandbox.alert = myalert; // <-- sandbox.document = sandbox.window.document; sandbox.__proto__ = unsafeWin; var res = Components.utils.evalInSandbox(str, sandbox); return [res,typeof(res)]; }; JS my $window = $self->tab->{linkedBrowser}->{contentWindow}; my $uri = $self->uri; return @{ $eval_in_sandbox->("$uri",$window,$redefined_alert ) }; };

      The following code works for me. Note that the interface is currently geared towards only capturing Javascript events (that is, the first passed argument) instead of properly capturing all arguments passed to the function. This will likely change in the next release of MozRepl::RemoteObject.

      #!perl -w use strict; use Test::More; use WWW::Mechanize::FireFox; my $mech = eval { WWW::Mechanize::FireFox->new( autodie => 0, #log => [qw[debug]] )}; if (! $mech) { my $err = $@; plan skip_all => "Couldn't connect to MozRepl: $@"; exit } else { plan tests => 2; }; isa_ok $mech, 'WWW::Mechanize::FireFox'; my $browser = $mech->tab->{linkedBrowser}; $mech->get_local('49-mech-get-file.html'); sub eval_in_page_with_redefined_alert { my ($self,$str,$redefined_alert) = @_; my $eval_in_sandbox = $self->repl->declare(<<'JS'); function (w,str,myalert) { var unsafeWin = w.wrappedJSObject; var safeWin = XPCNativeWrapper(unsafeWin); var sandbox = Components.utils.Sandbox(safeWin); sandbox.window = safeWin; sandbox.window.alert = myalert; // <-- sandbox.alert = myalert; // <-- sandbox.document = sandbox.window.document; sandbox.__proto__ = unsafeWin; var res = Components.utils.evalInSandbox(str, sandbox); return [res,typeof(res)]; }; JS my $window = $self->tab->{linkedBrowser}->{contentWindow}; my $uri = $self->uri; return @{ $eval_in_sandbox->($window, $str, $redefined_alert ) }; }; my @alerts; eval_in_page_with_redefined_alert($mech,"window.alert('Hello')", sub { + my @params = @_; push @alerts, \@params }); $mech->repl->poll; is_deeply \@alerts, [['Hello']], "We can capture alert() boxes if we w +ant to";

      Update: With MozRepl::RemoteObject v0.08 and WWW::Mechanize::FireFox v0.09, you can now specify additional variables for the environment in which things get evaluated.

        AH, maybe thats why I still dont appear to "catch" the alert/msgbox, since there may be more arguments and such going on in the background.

        Thank you so much for your help though, you really got me quite much further then if you wouldnt have assisted me :)