Advertisement
Guest User

screenshot.pl

a guest
Oct 14th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.59 KB | None | 0 0
  1. use strict; use warnings;
  2. use Gtk3; use Glib::IO;
  3. use Glib::Object::Introspection;
  4. Glib::Object::Introspection->setup(qw'basename WebKit2 version 4.0 package WebKit2');
  5. use Getopt::Std 'getopts';
  6.  
  7. 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');
  8. getopts 'u:o:w:h:t:a:fd', \%c;
  9.  
  10. print <<\doc and exit 0 unless $c{u};
  11. usage:
  12.   perl screenshot.pl -u https://www.whatismybrowser.com -o out.png -w 1280 -h 960
  13.  
  14. switches:
  15.   -u : url
  16.   -o : out file
  17.   -w : viewport width
  18.   -h : viewport height
  19.   -f : grab full document
  20.   -t : wait for dynamic content (=50ms)
  21.   -a : user agent
  22.   -d : debug (display dev tools)
  23. doc
  24.  
  25. $\ = "\n";
  26.  
  27. Gtk3->init;
  28. my $w = $c{d} ? Gtk3::Window->new : Gtk3::OffscreenWindow->new;
  29. my $ctx = WebKit2::WebContext->new_ephemeral;
  30. my $v = WebKit2::WebView->new_with_context($ctx);
  31. $w->add($v);
  32. for ($v->get_settings) {
  33.     $_->set('enable-developer-extras', Gtk3::true) if $c{d};
  34.     $_->set('user-agent', $c{a});
  35.     $_->set('enable-write-console-messages-to-stdout', Gtk3::true);
  36. }
  37.  
  38. my ($timer, $cancellable);
  39. $v->signal_connect('load-changed', sub {
  40.     print 'state: '.$_[1];
  41.     if ($_[1] eq 'finished') {
  42.         $timer = Glib::Timeout->add($c{t}, sub {
  43.             $v->get_snapshot(
  44.                 $c{f} ? 'full-document' : 'visible',
  45.                 'transparent-background',
  46.                 ($cancellable = Glib::IO::Cancellable->new),
  47.                 sub {
  48.                     $v->get_snapshot_finish($_[1])->write_to_png($c{o});
  49.                     print '> '.$c{o};
  50.                     $w->destroy
  51.                 }
  52.             );
  53.         });
  54.     }
  55. });
  56. $v->signal_connect('resource-load-started', sub { $_[1]->signal_connect('failed', sub { print $_[0]->get_uri.' failed' }) });
  57. $v->signal_connect('decide-policy', sub { print 'policy: '.$_[2]; $_[1]->use; Gtk3::true });
  58. $v->signal_connect('permission-request', sub { print 'permission: '.$_[1]; $_[1]->allow; Gtk3::true });
  59. $v->signal_connect('show-notification', sub { print 'notif: '.$_[1]->get_title; $_[1]->close; Gtk3::true });
  60. $v->signal_connect('script-dialog', sub { print $_[1]->get_dialog_type.': '.substr($_[1]->get_message,0,250); Gtk3::true });
  61. $v->signal_connect('close', sub { print 'closing webview'; $_[0]->destroy });
  62. $v->signal_connect('insecure-content-detected', sub { print "insecure content: $_[2]"; Gtk3::true });
  63. $w->signal_connect('destroy', sub { Gtk3->main_quit });
  64. $v->get_inspector->show if $c{d};
  65. $w->set_default_size(@c{qw/w h/});
  66. $w->show_all;
  67.  
  68. print '< '.(my $url = $c{u});
  69. $v->load_uri($url);
  70.  
  71. Gtk3->main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement