Created
December 5, 2014 14:08
-
-
Save nanis/894fcd5739674523b29d to your computer and use it in GitHub Desktop.
Internet Explorer screenshot using Perl and Win32::OLE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; use warnings; | |
use feature 'say'; | |
use Const::Fast; | |
use Imager; | |
use Imager::Screenshot qw( screenshot ); | |
use Win32::GuiTest qw( SetForegroundWindow ); | |
use Win32::OLE qw(EVENTS valof); | |
$Win32::OLE::Warn = 3; | |
const my $TYPE => 'bmp'; | |
const my $READYSTATE_COMPLETE => 4; | |
my ($URL) = @ARGV; | |
die "Need URL\n" unless defined $URL; | |
my $browser = Win32::OLE->new( | |
"InternetExplorer.Application", sub { $_[0]->Quit } | |
); | |
Win32::OLE->WithEvents($browser, \&Event, 'DWebBrowserEvents2'); | |
$browser->{Visible} = 1; | |
$browser->Navigate2($URL); | |
Win32::OLE->MessageLoop; | |
Win32::OLE->SpinMessageLoop; | |
$browser->Quit; | |
sleep 3; | |
sub Event { | |
my ($browser, $event, @argv) = @_; | |
if ($event eq 'DocumentComplete') { | |
sleep 1; | |
$browser->{ReadyState} == $READYSTATE_COMPLETE | |
or return; | |
my $hwnd = $browser->{HWND}; | |
SetForegroundWindow $hwnd; | |
my $img = screenshot(hwnd => $hwnd, decor => 1) | |
or die Imager->errstr; | |
my $url = valof( $argv[1] ); | |
$url =~ s{^https?://}{}; | |
$url =~ s{[^A-Za-z0-9_-]}{-}g; | |
$img->write(file => "$url.$TYPE", type => $TYPE) | |
or die $img->errstr; | |
Win32::OLE->QuitMessageLoop; | |
} | |
elsif ($event eq 'StatusTextChange') { | |
say $browser->{StatusText}; | |
} | |
else { | |
say $event; | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment