Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict; use warnings;
- use Gtk3; use Glib::IO;
- use Glib::Object::Introspection;
- Glib::Object::Introspection->setup(qw'basename WebKit2 version 4.0 package WebKit2');
- use Getopt::Std 'getopts';
- my %c = (qw'w 1280 h 960 o out.png t 50 a', 'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0');
- getopts 'u:o:w:h:t:a:fd', \%c;
- print <<\doc and exit 0 unless $c{u};
- usage:
- perl screenshot.pl -u https://www.whatismybrowser.com -o out.png -w 1280 -h 960
- switches:
- -u : url
- -o : out file
- -w : viewport width
- -h : viewport height
- -f : grab full document
- -t : wait for dynamic content (=50ms)
- -a : user agent
- -d : debug (display dev tools)
- doc
- $\ = "\n";
- Gtk3->init;
- my $w = $c{d} ? Gtk3::Window->new : Gtk3::OffscreenWindow->new;
- my $ctx = WebKit2::WebContext->new_ephemeral;
- my $v = WebKit2::WebView->new_with_context($ctx);
- $w->add($v);
- for ($v->get_settings) {
- $_->set('enable-developer-extras', Gtk3::true) if $c{d};
- $_->set('user-agent', $c{a});
- $_->set('enable-write-console-messages-to-stdout', Gtk3::true);
- }
- my ($timer, $cancellable);
- $v->signal_connect('load-changed', sub {
- print 'state: '.$_[1];
- if ($_[1] eq 'finished') {
- $timer = Glib::Timeout->add($c{t}, sub {
- $v->get_snapshot(
- $c{f} ? 'full-document' : 'visible',
- 'transparent-background',
- ($cancellable = Glib::IO::Cancellable->new),
- sub {
- $v->get_snapshot_finish($_[1])->write_to_png($c{o});
- print '> '.$c{o};
- $w->destroy
- }
- );
- });
- }
- });
- $v->signal_connect('resource-load-started', sub { $_[1]->signal_connect('failed', sub { print $_[0]->get_uri.' failed' }) });
- $v->signal_connect('decide-policy', sub { print 'policy: '.$_[2]; $_[1]->use; Gtk3::true });
- $v->signal_connect('permission-request', sub { print 'permission: '.$_[1]; $_[1]->allow; Gtk3::true });
- $v->signal_connect('show-notification', sub { print 'notif: '.$_[1]->get_title; $_[1]->close; Gtk3::true });
- $v->signal_connect('script-dialog', sub { print $_[1]->get_dialog_type.': '.substr($_[1]->get_message,0,250); Gtk3::true });
- $v->signal_connect('close', sub { print 'closing webview'; $_[0]->destroy });
- $v->signal_connect('insecure-content-detected', sub { print "insecure content: $_[2]"; Gtk3::true });
- $w->signal_connect('destroy', sub { Gtk3->main_quit });
- $v->get_inspector->show if $c{d};
- $w->set_default_size(@c{qw/w h/});
- $w->show_all;
- print '< '.(my $url = $c{u});
- $v->load_uri($url);
- Gtk3->main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement