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

Hello, I have a little project where I need to verify the status of some object that gets installed on the clients computers using JavaScript(not inhouse) I've managed to get the code running using PERL with the module Win32::IE::Mechanize with the code pasted below, but I cannot get the output of the command because that is returned to the browser as a javascript:alert("xxx").
use Win32::IE::Mechanize; my $mech = Win32::IE::Mechanize->new( visible => 0 ); $url = 'http://url/to/webpage'; $mech->get( $url ); $mech->success or die $mech->response->status_line; $mech->get('javascript:Install()'); #DO something to hide the resulting msgBox/javascript:alert
What I am however unable todo, is catch/trap/hide the dialogue returned at the end of the JavaScript which is embedded in the webpage, it calls a function where it writes it to a msgBox. I am not able to hide this and I really want todo this. I've tried putting the IE::Mechanize agent in quiet mode, ignoring errors etc, but it always comes back to tell me using message boxes. Is it possible to trap these? (I am not bound to use Internet Explorer/Windows XP, but was the easiest to kick off this project with)

Replies are listed 'Best First'.
Re: fetch HTML page, do javascript, read output from alert/msgbox
by Corion (Patriarch) on Nov 10, 2009 at 22:00 UTC

    You "just" need to inject your own Javascript code into the fetched URL, and in that Javascript code redefine window.alert() to be the function you want.

    If you're not tied to using Internet Explorer, you can easily do that with WWW::Mechanize::FireFox, but I'm biased, because I'm the author of that module :).

      I oh, so I can inject a code that redefines alert/msgbox?
      Got any examples or documents that tells me how I can incorporate that? ;-)
      I am checking out your WWW::Mechanize::FireFox module btw :-)

        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 :).