in reply to Re^2: fetch HTML page, do javascript, read output from alert/msgbox
in thread fetch HTML page, do javascript, read output from alert/msgbox
In theory, the (Javascript) code that I use in WWW::Mechanize::FireFox::eval_in_page can redefine all functions:
sub eval_in_page { my ($self,$str) = @_; 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,sub {print "Alert: @_" +}}; };
Note that the callbacks from MozRepl::RemoteObject are not blocking and not delivered in real time to you, so you can only capture the results of the alert() call but cannot take any action there in Perl. For taking an immediate action, you have to write some Javascript.
I plan a nicer interface for ->eval_in_page that allows you to (re)define properties of the window object, but as currently, WWW::Mechanize::FireFox is under heavy development, I can't promise any timeframe :).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: fetch HTML page, do javascript, read output from alert/msgbox
by TheFlyingCorpse (Novice) on Nov 11, 2009 at 10:55 UTC | |
by Corion (Patriarch) on Nov 11, 2009 at 12:05 UTC | |
by TheFlyingCorpse (Novice) on Nov 11, 2009 at 14:37 UTC | |
by Corion (Patriarch) on Nov 11, 2009 at 14:44 UTC | |
by TheFlyingCorpse (Novice) on Nov 11, 2009 at 14:54 UTC | |
|