Examples - WWW::Mechanize::Firefox example programs.
This is a documentation only module showing the examples that are included in the WWW::Mechanize::Firefox distribution.
This file was auto-generated via the gen_examples_pod.pl program that is also included in the examples directory.
gen_examples_pod.pl
The following is a list of the 7 example programs that are included in the WWW::Mechanize::Firefox distribution.
"Example: open-local-file.pl" Open a local file in Firefox
"Example: open-url.pl" Open an URL in Firefox
"Example: screenshot.pl" Take a screenshot of a website
"Example: dump-links.pl" Dump links on a webpage
"Example: manipulate-javascript.pl" Make changes to Javascript values in a webpage
"Example: javascript.pl" Execute Javascript in the webpage context
"Example: urlbar.pl" Listen to changes in the location bar
use strict; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->get_local('datei.html'); <>;
Download this example: http://cpansearch.perl.org/src/CORION/WWW-Mechanize-Firefox-0.25/examples/open-local-file.pl
use strict; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->get('http://www.perlworkshop.de'); <>;
Download this example: http://cpansearch.perl.org/src/CORION/WWW-Mechanize-Firefox-0.25/examples/open-url.pl
#!perl -w use strict; use WWW::Mechanize::Firefox; use Getopt::Long; use Pod::Usage; GetOptions( 'mozrepl|m:s' => \my $mozrepl, 'outfile|o:s' => \my $outfile, ) or pod2usage(); $outfile ||= 'screenshot.png'; my @args; if (! @ARGV) { push @args, tab => 'current'; }; my $mech = WWW::Mechanize::Firefox->new( launch => 'firefox', @args ); if (@ARGV) { $mech->get($ARGV[0]); }; my $png = $mech->content_as_png(); open my $out, '>', $outfile or die "Couldn't create '$outfile': $!"; binmode $out; print {$out} $png; =head1 NAME screenshot.pl - take a screenshot of a webpage =head1 SYNOPSIS screenshot.pl [options] [url] Options: --outfile name of output file --mozrepl connection string to Firefox =head1 OPTIONS =over 4 =item B<--outfile> Name of the output file. The image will always be written in PNG format. =item B<--mozrepl> Connection information for the mozrepl instance to use. =back =head1 DESCRIPTION B<This program> will take a screenshot of the given URL (including plugins) and write it to the given file or the file C<screenshot.png>. =cut
Download this example: http://cpansearch.perl.org/src/CORION/WWW-Mechanize-Firefox-0.25/examples/screenshot.pl
use strict; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->get_local('links.html'); $mech->highlight_node( $mech->selector('a.download')); print $_->{href}, " - ", $_->{innerHTML}, "\n" for $mech->selector('a.download'); <>; =head1 NAME dump-links.pl - Dump links on a webpage =head1 SYNOPSIS dump-links.pl =head1 DESCRIPTION This program demonstrates how to read elements out of the Firefox DOM and how to get at text within nodes. It also demonstrates how you can modify elements in a webpage. =cut
Download this example: http://cpansearch.perl.org/src/CORION/WWW-Mechanize-Firefox-0.25/examples/dump-links.pl
use strict; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->get_local('javascript.html'); my ($val,$type) = $mech->eval_in_page(<<'JS'); secret JS if ($type ne 'string') { die "Unbekannter Ergebnistyp: $type"; }; print "Das Kennwort ist $val"; $mech->value('pass',$val); <>; =head1 NAME manipulate-javascript.pl - demonstrate how to manipulate Javascript in a page =head1 SYNOPSIS manipulate-javascript.pl =head1 DESCRIPTION This program demonstrates that you have write access to Javascript variables in Firefox and in webpages displayed through Firefox. =cut
Download this example: http://cpansearch.perl.org/src/CORION/WWW-Mechanize-Firefox-0.25/examples/manipulate-javascript.pl
use strict; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->get_local('links.html'); $mech->eval_in_page(<<'JS'); alert('Hallo Frankfurt.pm'); JS <>; =head1 NAME javascript.pl - execute Javascript in a page =head1 SYNOPSIS javascript.pl =head1 DESCRIPTION B<This program> demonstrates how to execute simple Javascript in a page. =cut
Download this example: http://cpansearch.perl.org/src/CORION/WWW-Mechanize-Firefox-0.25/examples/javascript.pl
use strict; use lib 'C:/Projekte/MozRepl-RemoteObject/lib'; use WWW::Mechanize::Firefox; use Time::HiRes; my $mech = WWW::Mechanize::Firefox->new( #log => ['debug'], ); $mech->get('http://www.cpan.org'); my $mk_listener = $mech->repl->declare(<<'JS'); function (myListener,source) { myListener.source = source; const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START; const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP; var callbacks = ['onStateChange', 'onLocationChange', "onProgressChange", "onStatusChange", "onSecurityChange", ]; for (var h in callbacks) { var e = callbacks[h]; if (! myListener[e]) { myListener[e] = function(){} }; }; myListener.QueryInterface = function(aIID) { if (aIID.equals(Components.interfaces.nsIWebProgressListener) || aIID.equals(Components.interfaces.nsISupportsWeakReference) || aIID.equals(Components.interfaces.nsISupports)) return this; throw Components.results.NS_NOINTERFACE; }; return myListener } JS =begin JSDoc "onStateChange": handlers[ function(aWebProgress, aRequest, aFlag, aStatus) { // If you use myListener for more than one tab/window, use // aWebProgress.DOMWindow to obtain the tab/window which triggers the state change if(aFlag & STATE_START) { // This fires when the load event is initiated onLoadStart(aWebProgress,aRequest,aStatus); } if(aFlag & STATE_STOP) { // This fires when the load finishes onLoadStop(aWebProgress,aRequest,aStatus); } }, "onLocationChange": function(aProgress, aRequest, aURI) { // This fires when the location bar changes; i.e load event is confirmed // or when the user switches tabs. If you use myListener for more than one tab/window, // use aProgress.DOMWindow to obtain the tab/window which triggered the change. }, // For definitions of the remaining functions see related documentation "onProgressChange": function(aWebProgress, aRequest, curSelf, maxSelf, curTot, maxTot) { }, "onStatusChange": function(aWebProgress, aRequest, aStatus, aMessage) { }, "onSecurityChange": function(aWebProgress, aRequest, aState) { }, }; =cut sub onStateChange { my ($progress,$request,$flag,$status) = @_; print "@_\n"; } sub onLocationChange { my ($progress,$request,$uri) = @_; print "Location :", $uri->{spec},"\n"; } my $NOTIFY_STATE_DOCUMENT = $mech->repl->expr('Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT'); sub event_listener { my ($source,%handlers) = @_; my ($obj) = $mech->repl->expr('new Object'); for my $key (keys %handlers) { $obj->{$key} = $handlers{$key}; }; my $lsn = $mk_listener->($obj,$source); $lsn->__release_action('self.source.removeEventListener(self)'); $source->addProgressListener($lsn,$NOTIFY_STATE_DOCUMENT); $lsn; }; my $browser = $mech->repl->expr('window.getBrowser()'); my $eventlistener = event_listener( $browser, onLocationChange => \&onLocationChange, ); while (1) { $mech->repl->poll(); sleep 1; };
Download this example: http://cpansearch.perl.org/src/CORION/WWW-Mechanize-Firefox-0.25/examples/urlbar.pl
Max Maischein corion@cpan.org
corion@cpan.org
Contributed examples contain the original author's name.
Copyright 2010, Max Maischein.
All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
To install WWW::Mechanize::Firefox, copy and paste the appropriate command in to your terminal.
cpanm
cpanm WWW::Mechanize::Firefox
CPAN shell
perl -MCPAN -e shell install WWW::Mechanize::Firefox
For more information on module installation, please visit the detailed CPAN module installation guide.