| 1 | package HTTP::Cookies::ChromeDevTools; | ||||||||||||||||||||
| 2 | 69 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 69 | ▶ | use Carp qw[croak]; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 69 | our $VERSION = '0.80'; | |||||||||||||||||||
| 6 | 69 | our @CARP_NOT; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 69 | ▶ | use Moo 2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 69 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 69 | ▶ | use feature 'signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 69 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 69 | extends 'HTTP::Cookies'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | HTTP::Cookies::ChromeDevTools - retrieve cookies from a live Chrome instance | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | use HTTP::Cookies::ChromeDevTools; | ||||||||||||||||||||
| 22 | my $cookie_jar = HTTP::Cookies::ChromeDevTools->new(); | ||||||||||||||||||||
| 23 | # use just like HTTP::Cookies | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | This package overrides the load() and save() methods of HTTP::Cookies | ||||||||||||||||||||
| 28 | so it can work with a live Chrome instance. | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head1 Reusing an existing connection | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | If you already have an existing connection to Chrome | ||||||||||||||||||||
| 33 | that you want to reuse, just pass the L<Chrome::DevToolsProtocol> | ||||||||||||||||||||
| 34 | instance to the cookie jar constructor in the C<driver> parameter: | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | my $cookie_jar = HTTP::Cookies::ChromeDevTools->new( | ||||||||||||||||||||
| 37 | driver => $driver | ||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | has 'transport' => ( | ||||||||||||||||||||
| 43 | is => 'lazy', | ||||||||||||||||||||
| 44 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 45 | $ENV{ WWW_MECHANIZE_CHROME_TRANSPORT } | ||||||||||||||||||||
| 46 | 0 | }, | |||||||||||||||||||
| 47 | 69 | ); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | has 'driver' => ( | ||||||||||||||||||||
| 50 | is => 'lazy', | ||||||||||||||||||||
| 51 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 52 | 0 | my( $self ) = @_; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # Connect to it | ||||||||||||||||||||
| 55 | Chrome::DevToolsProtocol->new( | ||||||||||||||||||||
| 56 | 'port' => $self->{ port }, | ||||||||||||||||||||
| 57 | host => $self->{ host }, | ||||||||||||||||||||
| 58 | auto_close => 0, | ||||||||||||||||||||
| 59 | 0 | ▶ | error_handler => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 60 | #warn ref$_[0]; | ||||||||||||||||||||
| 61 | #warn "<<@CARP_NOT>>"; | ||||||||||||||||||||
| 62 | #warn ((caller($_))[0,1,2]) | ||||||||||||||||||||
| 63 | # for 1..4; | ||||||||||||||||||||
| 64 | 0 | local @CARP_NOT = (@CARP_NOT, ref $_[0],'Try::Tiny'); | |||||||||||||||||||
| 65 | # Reraise the error | ||||||||||||||||||||
| 66 | 0 | croak $_[1] | |||||||||||||||||||
| 67 | }, | ||||||||||||||||||||
| 68 | transport => $self->transport, | ||||||||||||||||||||
| 69 | log => $self->{ log }, | ||||||||||||||||||||
| 70 | ) | ||||||||||||||||||||
| 71 | 0 | }, | |||||||||||||||||||
| 72 | 69 | ); | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 69 | has '_loading' => ( | |||||||||||||||||||
| 75 | is => 'rw', | ||||||||||||||||||||
| 76 | default => 0, | ||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =head2 C<< ->load( $file, %options ) >> | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | $jar->load( undef, driver => $driver ); | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | Loads the cookies from the Chrome instance. Passing in a filename to load | ||||||||||||||||||||
| 84 | cookies from is currently unsupported. | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | ▶ | sub load($self, $file=undef, %options) { | ||||||||||||||||||
|
Subroutine
load: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 89 | 0 | ▶ | my $driver = $options{ driver } || $self->driver; | ||||||||||||||||||
|
Condition0%0/3
$options{'driver'} || $self->driver
MC/DC0%0/2
$options{'driver'} || $self->driver
$options{'driver'}
$self->driver
Truth table0%0/3
A || B
A= $options{'driver'}B= $self->driver
| |||||||||||||||||||||
| 90 | # Alternative: Storage.getCookies | ||||||||||||||||||||
| 91 | 0 | my $cookies = $driver->send_message('Network.getAllCookies')->get(); | |||||||||||||||||||
| 92 | 0 | $cookies = $cookies->{cookies}; | |||||||||||||||||||
| 93 | 0 | $self->clear(); | |||||||||||||||||||
| 94 | 0 | local $self->{_loading} = 1; | |||||||||||||||||||
| 95 | 0 | for my $c (@$cookies) { | |||||||||||||||||||
| 96 | 0 | ▶ | if( $c->{expires} == -1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | $c->{expires} = 1; # we need to do it this way or HTTP::Cookies will discard our cookie immediately :( | |||||||||||||||||||
| 98 | }; | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | $self->set_cookie( | ||||||||||||||||||||
| 101 | 1, | ||||||||||||||||||||
| 102 | $c->{name}, | ||||||||||||||||||||
| 103 | $c->{value}, | ||||||||||||||||||||
| 104 | $c->{path}, | ||||||||||||||||||||
| 105 | $c->{domain}, | ||||||||||||||||||||
| 106 | undef, # Chrome doesn't support port numbers?! | ||||||||||||||||||||
| 107 | undef, | ||||||||||||||||||||
| 108 | $c->{secure}, | ||||||||||||||||||||
| 109 | $c->{expires}, | ||||||||||||||||||||
| 110 | #$c->{session}, | ||||||||||||||||||||
| 111 | undef, | ||||||||||||||||||||
| 112 | 0 | ); | |||||||||||||||||||
| 113 | }; | ||||||||||||||||||||
| 114 | 0 | ▶ | if( $file ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | my $jar = HTTP::Cookies->new( file => $file ); | |||||||||||||||||||
| 116 | 0 | $self->load_jar( $jar ); | |||||||||||||||||||
| 117 | }; | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =head2 C<< ->load_jar( $jar, %options ) >> | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | $jar->load( $jar, replace => 1; | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Imports the cookies from another cookie jar into Chrome. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | B<replace> will clear out the cookie jar before loading the fresh cookies. | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | ▶ | sub load_jar($self, $jar, %options) { | ||||||||||||||||||
|
Subroutine
load_jar: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 131 | 0 | ▶ | my $driver = $options{ driver } || $self->driver; | ||||||||||||||||||
|
Condition0%0/3
$options{'driver'} || $self->driver
MC/DC0%0/2
$options{'driver'} || $self->driver
$options{'driver'}
$self->driver
Truth table0%0/3
A || B
A= $options{'driver'}B= $self->driver
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | ▶ | if( $options{ replace }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | 0 | $driver->send_message('Network.clearBrowserCookies')->get(); | |||||||||||||||||||
| 135 | 0 | $self->clear(); | |||||||||||||||||||
| 136 | }; | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | local $self->{_loading} = 0; | |||||||||||||||||||
| 139 | 0 | ▶ | $jar->scan( sub(@c) { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 140 | 0 | my $c = {}; | |||||||||||||||||||
| 141 | 0 | @{$c}{qw(version name value path domain port path_spec secure expires discard hash)} = @c; | |||||||||||||||||||
| 142 | $self->set_cookie( | ||||||||||||||||||||
| 143 | 1, | ||||||||||||||||||||
| 144 | $c->{name}, | ||||||||||||||||||||
| 145 | $c->{value}, | ||||||||||||||||||||
| 146 | $c->{path}, | ||||||||||||||||||||
| 147 | $c->{domain}, | ||||||||||||||||||||
| 148 | undef, # Chrome doesn't support port numbers | ||||||||||||||||||||
| 149 | undef, | ||||||||||||||||||||
| 150 | $c->{secure}, | ||||||||||||||||||||
| 151 | $c->{expires}, | ||||||||||||||||||||
| 152 | undef, | ||||||||||||||||||||
| 153 | 0 | ); | |||||||||||||||||||
| 154 | 0 | }); | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | ▶ | sub set_cookie($self, $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard) { | ||||||||||||||||||
|
Subroutine
set_cookie: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 158 | # We've just read from Chrome, so just update our local variables | ||||||||||||||||||||
| 159 | #$self->debug_set_cookie($version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard); | ||||||||||||||||||||
| 160 | 0 | $self->SUPER::set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard ); | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | ▶ | if( ! $self->_loading ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | # Update Chrome | ||||||||||||||||||||
| 164 | 0 | $maxage += time(); | |||||||||||||||||||
| 165 | 0 | $self | |||||||||||||||||||
| 166 | ->set_cookie_in_chrome($version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard) | ||||||||||||||||||||
| 167 | ->get(); | ||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | ▶ | sub set_cookie_in_chrome($self, $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard) { | ||||||||||||||||||
|
Subroutine
set_cookie_in_chrome: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 172 | # Update Chrome | ||||||||||||||||||||
| 173 | 0 | my $driver = $self->driver; | |||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 0 | $maxage += time(); | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | ▶ | $driver->send_message('Network.setCookie', | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | name => $key, | ||||||||||||||||||||
| 179 | value => $val, | ||||||||||||||||||||
| 180 | path => $path, | ||||||||||||||||||||
| 181 | domain => $domain, | ||||||||||||||||||||
| 182 | httpOnly => JSON::false, | ||||||||||||||||||||
| 183 | expires => $maxage, | ||||||||||||||||||||
| 184 | secure => ($secure ? JSON::true : JSON::false), | ||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||
| 186 | }; | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | ▶ | sub save { | ||||||||||||||||||
|
Subroutine
save: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 189 | 0 | croak 'save is not yet implemented' | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 69 | 1; | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||