WWW-Mechanize-Chrome-Webshot

 view release on metacpan or  search on metacpan

lib/WWW/Mechanize/Chrome/Webshot.pm  view on Meta::CPAN

	"WWW::Mechanize::Chrome" : {
		"headless" : "1",
		"launch_arg" : [
			</* this will change as per the 'resolution' setting above */>
			"--window-size=600x800",
			"--password-store=basic", </* do not ask me for stupid chrome account password */>
		</*	"--remote-debugging-port=9223", */>
		</*	"--enable-logging", */>
			"--disable-gpu",
		</*	"--no-sandbox", NO LONGER VALID */>
			"--ignore-certificate-errors",
			"--disable-background-networking",
			"--disable-client-side-phishing-detection",
			"--disable-component-update",
			"--disable-hang-monitor",
			"--disable-save-password-bubble",
			"--disable-default-apps",
			"--disable-infobars",
			"--disable-popup-blocking"
		]
	}
}
EOCS

sub	new {
	my $class = $_[0];
	my $params = $_[1]; # a hash of params, see below

	my $parent = ( caller(1) )[3] || "N/A";
	my $whoami = ( caller(0) )[3];

	my $self = {
		'_private' => {
			'confighash' => undef,
			'configfile' => '', # this should never be undef
			'debug' => {
				'verbosity' => 0,
				'cleanup' => 1,
			},
			'log' => {
				'logger-object' => undef,
				'logfile' => undef
			},
			'settle-time' => 5,
			'resolution' => '1000,1002', # not x but , !!!
			'stop-on-error' => 0,
			'remove-dom-elements' => [],
			# this is a global exif metadata to be set
			# each time we shoot() UNLESSS shoot()'s
			# params have their own, in which case this is not used:
			'exif' => undef,
			# this may be instantiated lazily,
			# when needed as mech is expensive to load
			# or it can be passed on to us via the constructor
			'WWW::Mechanize::Chrome-object' => undef,
			# these are the final mech params,
			# from confighash and user-specified $params
			'WWW::Mechanize::Chrome-params' => undef,
		}
	};
	bless $self => $class;

	# this will read configuration and create confighash,
	# make logger, verbosity,
	# instantiate any objects we need here etc.
	if( $self->init($params) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, call to init() has failed.\n"; return undef }

	# Now we have a logger
	my $log = $self->log();

	# do module-specific init like instantiating the mech, unless params has 'launch-mech-on-demand'=>1
	if( $self->init_module_specific($params) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to init_module_specific() has failed."); return undef }

	my $verbosity = $self->verbosity;
	if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : done, success (verbosity is set to ".$self->verbosity." and cleanup to ".$self->cleanup.").") }

	return $self;
}

# blank the browser
sub	blank_browser { $_[0]->mech_obj->get('about:blank'); $_[0]->mech_obj->sleep(1); }

# hits the page, waits for some settle-time, removes some elements from the DOM,
# if asked, that obstruct the shot, e.g. banners and dumps the browser's window
# to a PNG or PDF file (whose filename must be supplied as input param).
# NOTE: PDF output does not seem to contain the whole browser content!
# it has a preset size.
# optional 'remove-dom-elements' overwrites the self if any
# optional 'exif' parameter (as hash) specifies tag/value pairs to be inserted as
# metadata into the final output image
# { k => v, ... }
# exif tagnames have restrictions: no unicode, no spaces, not ':'
# returns 0 on failure
# returns 1 on success
sub	shoot {
	my $self = $_[0];
	my $params = $_[1]; # a hashref of 'output-filename', 'url' etc.
	my $parent = ( caller(1) )[3] || "N/A";
	my $whoami = ( caller(0) )[3];
	my $verbos = $self->verbosity;
	my $log = $self->log;

	# this is required
	my $outfile = exists($params->{'output-filename'}) ? $params->{'output-filename'} : undef;
	if( ! defined $outfile ){ $log->error("$whoami (via $parent), line ".__LINE__." : error, 'output-filename' parameter was not specified."); return 0 }

	my $outformat;
	if( exists($params->{'output-format'}) && defined($params->{'output-format'}) ){
		if( $params->{'output-format'} =~ /^(png|pdf)$/ ){ $outformat = $1 }
		else { $log->error("$whoami (via $parent), line ".__LINE__." : error, parameter 'output-format' was specified as '".$params->{'output-format'}."' but one of 'png' or 'pdf' was expected."); return 0 }
	} else {
		# try and deduce the format from the file extension
		if( $outfile =~ /\.(pdf|png)$/i ){ $outformat = $1 }
		else { $log->error("$whoami (via $parent), line ".__LINE__." : error, parameter 'output-format' was not specified and failed to deduce the output format from the specified output filename ($outfile). Either specify 'output-format' as one of 'png' o...
	}

	# this is optional, because mech already may be in current page
	# if you will use URI for local file (file://...) then
	# USE ABSOLUTE FILEPATH
	my $URL = exists($params->{'url'}) ? $params->{'url'} : undef;



( run in 2.283 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )