| 1 | package AnyEvent::Chromi; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use AnyEvent::Socket; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use AnyEvent::Handle; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | use Protocol::WebSocket::Handshake::Client; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use Protocol::WebSocket::Handshake::Server; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use Protocol::WebSocket::Frame; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 1 | ▶ | use JSON::XS; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use URI::Escape; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use Log::Any qw($log); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 1 | our $VERSION = '1.01'; | |||||||||||
| 17 | |||||||||||||
| 18 | sub new | ||||||||||||
| 19 | 5 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 20 | 5 | my ($class, %args) = @_; | |||||||||||
| 21 | 5 | my $self = {}; | |||||||||||
| 22 | 5 | bless $self, $class; | |||||||||||
| 23 | |||||||||||||
| 24 | 5 | ▶ | $self->{mode} = $args{mode} // 'server'; | ||||||||||
|
Condition100%2/2
$args{"mode"} // "server"
MC/DC100%1/1
$args{"mode"} // "server"
$args{"mode"}
Truth table100%2/2
A
A= $args{"mode"}
| |||||||||||||
| 25 | 5 | ▶ | $self->{port} = $args{port} // 7441; | ||||||||||
|
Condition100%2/2
$args{"port"} // 7441
MC/DC100%1/1
$args{"port"} // 7441
$args{"port"}
Truth table100%2/2
A
A= $args{"port"}
| |||||||||||||
| 26 | 5 | ▶ | $self->{on_connect} = $args{on_connect} if defined $args{on_connect}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 5 | ▶ | if($self->{mode} eq 'client') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 2 | $self->_start_client(); | |||||||||||
| 29 | } | ||||||||||||
| 30 | else { | ||||||||||||
| 31 | 3 | $self->_start_server(); | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | 5 | return $self; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | sub call | ||||||||||||
| 38 | 0 | ▶ | { | ||||||||||
|
Subroutine
call: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 39 | 0 | my ($self, $method, $args, $cb) = @_; | |||||||||||
| 40 | 0 | ▶ | if(not $self->is_connected) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 0 | $log->warning("can't call $method: not connected"); | |||||||||||
| 42 | 0 | return; | |||||||||||
| 43 | } | ||||||||||||
| 44 | 0 | my $id = int(rand(100000000)); | |||||||||||
| 45 | 0 | my $msg = "chromi $id $method"; | |||||||||||
| 46 | 0 | ▶ | if($args) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 0 | $msg .= " " . uri_escape(encode_json($args)); | |||||||||||
| 48 | } | ||||||||||||
| 49 | 0 | my $frame = Protocol::WebSocket::Frame->new($msg); | |||||||||||
| 50 | 0 | ▶ | if($cb) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 0 | $self->{callbacks}{$id} = $cb; | |||||||||||
| 52 | } | ||||||||||||
| 53 | 0 | $self->{handle}->push_write($frame->to_bytes); | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | sub is_connected | ||||||||||||
| 57 | 0 | ▶ | { | ||||||||||
|
Subroutine
is_connected: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 58 | 0 | my ($self) = @_; | |||||||||||
| 59 | 0 | return $self->{connected}; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | sub _setup_connection | ||||||||||||
| 63 | 0 | ▶ | { | ||||||||||
|
Subroutine
_setup_connection: not called CC=3 SCAR=24.8
| |||||||||||||
| 64 | 0 | my ($self, $fh) = @_; | |||||||||||
| 65 | |||||||||||||
| 66 | 0 | ▶ | my $ws_handshake = $self->{mode} eq 'client' ? Protocol::WebSocket::Handshake::Client->new(url => 'ws://localhost') : | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | Protocol::WebSocket::Handshake::Server->new; | ||||||||||||
| 68 | 0 | my $ws_frame = Protocol::WebSocket::Frame->new; | |||||||||||
| 69 | |||||||||||||
| 70 | 0 | $self->{handle} = AnyEvent::Handle->new(fh => $fh); | |||||||||||
| 71 | |||||||||||||
| 72 | $self->{handle}->on_error( | ||||||||||||
| 73 | 0 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 74 | 0 | my ($handle, $fatal, $message); | |||||||||||
| 75 | 0 | ▶ | if($fatal) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 0 | $log->error("connection fatal error: $message"); | |||||||||||
| 77 | 0 | $self->{connected} = 0; | |||||||||||
| 78 | } | ||||||||||||
| 79 | else { | ||||||||||||
| 80 | 0 | $log->warning("connection error: $message"); | |||||||||||
| 81 | } | ||||||||||||
| 82 | } | ||||||||||||
| 83 | 0 | ); | |||||||||||
| 84 | |||||||||||||
| 85 | 0 | ▶ | $self->{handle}->on_eof( sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 86 | 0 | $self->{connected} = 0; | |||||||||||
| 87 | 0 | ▶ | if($self->{mode} eq 'client') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 0 | $self->_client_schedule_reconnect(); | |||||||||||
| 89 | } | ||||||||||||
| 90 | 0 | }); | |||||||||||
| 91 | |||||||||||||
| 92 | 0 | ▶ | $self->{handle}->on_read( sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=47.0
| |||||||||||||
| 93 | 0 | my ($handle) = @_; | |||||||||||
| 94 | 0 | my $chunk = $handle->{rbuf}; | |||||||||||
| 95 | 0 | $handle->{rbuf} = undef; | |||||||||||
| 96 | |||||||||||||
| 97 | # Handshake | ||||||||||||
| 98 | 0 | ▶ | if (!$ws_handshake->is_done) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 0 | $ws_handshake->parse($chunk); | |||||||||||
| 100 | 0 | ▶ | if ($ws_handshake->is_done) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | 0 | ▶ | if(not $self->{mode} eq 'client') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 0 | $handle->push_write($ws_handshake->to_string); | |||||||||||
| 103 | } | ||||||||||||
| 104 | 0 | $self->{connected} = 1; | |||||||||||
| 105 | 0 | ▶ | if($self->{on_connect}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | 0 | my $cb = $self->{on_connect}; | |||||||||||
| 107 | 0 | &$cb($self); | |||||||||||
| 108 | } | ||||||||||||
| 109 | } | ||||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 0 | ▶ | $self->{connected} or return; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | |||||||||||||
| 114 | # Post-Handshake | ||||||||||||
| 115 | 0 | $ws_frame->append($chunk); | |||||||||||
| 116 | |||||||||||||
| 117 | 0 | ▶ | while (my $message = $ws_frame->next) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | ▶ | if($message =~ /^Chromi (\d+) (\w+) (.*)$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | 0 | my ($id, $status, $reply) = ($1, $2, $3); | |||||||||||
| 120 | 0 | ▶ | if($self->{callbacks}{$id}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 0 | $reply = uri_unescape($reply); | |||||||||||
| 122 | 0 | ▶ | if($reply =~ /^\[(.*)\]$/s) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | 0 | &{$self->{callbacks}{$id}}($status, decode_json($1)); | |||||||||||
| 124 | } | ||||||||||||
| 125 | else { | ||||||||||||
| 126 | 0 | die "error: $reply\n"; | |||||||||||
| 127 | } | ||||||||||||
| 128 | 0 | delete $self->{callbacks}{$id}; | |||||||||||
| 129 | } | ||||||||||||
| 130 | } | ||||||||||||
| 131 | } | ||||||||||||
| 132 | 0 | }); | |||||||||||
| 133 | |||||||||||||
| 134 | 0 | ▶ | if($self->{mode} eq 'client') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | 0 | $self->{handle}->push_write($ws_handshake->to_string); | |||||||||||
| 136 | } | ||||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | sub _client_schedule_reconnect | ||||||||||||
| 140 | 0 | ▶ | { | ||||||||||
|
Subroutine
_client_schedule_reconnect: not called CC=1 SCAR=6.9
| |||||||||||||
| 141 | 0 | my ($self) = @_; | |||||||||||
| 142 | |||||||||||||
| 143 | 0 | $log->info("connection failed. reconnecting in 1 second"); | |||||||||||
| 144 | |||||||||||||
| 145 | 0 | ▶ | $self->{conn_w} = AnyEvent->timer (after => 1, cb => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 146 | 0 | $self->_start_client(); | |||||||||||
| 147 | 0 | }); | |||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | sub _start_client | ||||||||||||
| 151 | 2 | ▶ | { | ||||||||||
|
Subroutine
_start_client: called CC=1 SCAR=3.9
| |||||||||||||
| 152 | 2 | my ($self) = @_; | |||||||||||
| 153 | |||||||||||||
| 154 | 0 | ▶ | $self->{tcp_client} = AnyEvent::Socket::tcp_connect 'localhost', $self->{port}, sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 155 | 0 | my ($fh) = @_; | |||||||||||
| 156 | 0 | ▶ | if(! $fh) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | 0 | $self->_client_schedule_reconnect(); | |||||||||||
| 158 | 0 | return; | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | 0 | $self->_setup_connection($fh); | |||||||||||
| 162 | 2 | }; | |||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | sub _start_server | ||||||||||||
| 166 | 3 | ▶ | { | ||||||||||
|
Subroutine
_start_server: called CC=1 SCAR=1.2
| |||||||||||||
| 167 | 3 | my ($self) = @_; | |||||||||||
| 168 | 0 | ▶ | $self->{tcp_server} = AnyEvent::Socket::tcp_server undef, $self->{port}, sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 169 | 0 | my ($fh, $host, $port) = @_; | |||||||||||
| 170 | 0 | $self->_setup_connection($fh); | |||||||||||
| 171 | 3 | }; | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | 1 | 1; | |||||||||||
| 175 | |||||||||||||
| 176 | =head1 NAME | ||||||||||||
| 177 | |||||||||||||
| 178 | AnyEvent::Chromi - Remotely control Google Chrome from Perl | ||||||||||||
| 179 | |||||||||||||
| 180 | =head2 SYNOPSIS | ||||||||||||
| 181 | |||||||||||||
| 182 | # Start in client mode (need "chromix-server" or examples/server.pl) | ||||||||||||
| 183 | my $chromi AnyEvent::Chromi->new(mode => 'client', on_connect => sub { | ||||||||||||
| 184 | my ($chromi) = @_; | ||||||||||||
| 185 | ... | ||||||||||||
| 186 | $chromi->call(...); | ||||||||||||
| 187 | }); | ||||||||||||
| 188 | |||||||||||||
| 189 | # Start in server mode | ||||||||||||
| 190 | my $chromi AnyEvent::Chromi->new(mode => 'server'); | ||||||||||||
| 191 | |||||||||||||
| 192 | =head2 DESCRIPTION | ||||||||||||
| 193 | |||||||||||||
| 194 | AnyEvent::Chromi allows you to remotely control Google Chrome from a Perl script. | ||||||||||||
| 195 | It requires the Chromi extension L<https://github.com/smblott-github/chromi>, which | ||||||||||||
| 196 | exposes all of the Chrome Extensions API via a websocket connection. | ||||||||||||
| 197 | |||||||||||||
| 198 | =head2 METHODS | ||||||||||||
| 199 | |||||||||||||
| 200 | =over 4 | ||||||||||||
| 201 | |||||||||||||
| 202 | =item $chromi = AnyEvent::Chromi->new(mode => ..., on_connect => ...); | ||||||||||||
| 203 | |||||||||||||
| 204 | =over 4 | ||||||||||||
| 205 | |||||||||||||
| 206 | =item mode => 'client|server' | ||||||||||||
| 207 | |||||||||||||
| 208 | If 'server' (default), it will start a websocket server on port 7441 and wait | ||||||||||||
| 209 | for the connection from Chrome (initiated by the Chromi extension). This is the | ||||||||||||
| 210 | most practical way to use AnyEvent::Chromi if you write a long-running script, | ||||||||||||
| 211 | because it doesn't require a separate daemon. | ||||||||||||
| 212 | |||||||||||||
| 213 | If 'client', it will connect to port 7441 itself, expecting a websocket server, like | ||||||||||||
| 214 | the one provided by chromix-server, or by the examples/server.pl script. | ||||||||||||
| 215 | |||||||||||||
| 216 | =item port => N | ||||||||||||
| 217 | |||||||||||||
| 218 | Use port N instead of 7441. | ||||||||||||
| 219 | |||||||||||||
| 220 | =item on_connect => sub { my ($chromi) = @_; ... } | ||||||||||||
| 221 | |||||||||||||
| 222 | Will be executed as soon as Chrome connects (in server mode), or as the connection | ||||||||||||
| 223 | to the websocket server is done. | ||||||||||||
| 224 | |||||||||||||
| 225 | =back | ||||||||||||
| 226 | |||||||||||||
| 227 | =item $chromi->call($method, $args, $cb) | ||||||||||||
| 228 | |||||||||||||
| 229 | Call the Chrome extension method C<$method>, e.g. C<chrome.windows.getAll>. | ||||||||||||
| 230 | |||||||||||||
| 231 | C<$args> is expected to be a ARRAYREF with the arguments for the method. It will be | ||||||||||||
| 232 | converted to JSON by AnyEvent::Chromi. | ||||||||||||
| 233 | |||||||||||||
| 234 | C<$cb> is a callback for when the reply is received. The first argument to the callback is | ||||||||||||
| 235 | the status (either "done" or "error"), and the second is a ARRAYREF with the data. | ||||||||||||
| 236 | |||||||||||||
| 237 | Note: you need to make sure that the JSON::XS serialization is generating the proper | ||||||||||||
| 238 | data types. This is particularly important for booleans, where C<Types::Serialiser::true> | ||||||||||||
| 239 | and C<Types::Serialiser::false> can be used. | ||||||||||||
| 240 | |||||||||||||
| 241 | =item $chromi->is_connected | ||||||||||||
| 242 | |||||||||||||
| 243 | In server mode: returns true if Chrome is connected and awaits commands. | ||||||||||||
| 244 | |||||||||||||
| 245 | In client mode: returns true if connected to chromix-server. | ||||||||||||
| 246 | |||||||||||||
| 247 | =back | ||||||||||||
| 248 | |||||||||||||
| 249 | =head2 EXAMPLES | ||||||||||||
| 250 | |||||||||||||
| 251 | =over | ||||||||||||
| 252 | |||||||||||||
| 253 | =item * | ||||||||||||
| 254 | |||||||||||||
| 255 | List all tabs | ||||||||||||
| 256 | |||||||||||||
| 257 | $chromi->call( | ||||||||||||
| 258 | 'chrome.windows.getAll', [{ populate => Types::Serialiser::true }], | ||||||||||||
| 259 | sub { | ||||||||||||
| 260 | my ($status, $reply) = @_; | ||||||||||||
| 261 | $status eq 'done' or return; | ||||||||||||
| 262 | defined $reply and ref $reply eq 'ARRAY' or return; | ||||||||||||
| 263 | map { say "$_->{url}" } @{$reply->[0]{tabs}}; | ||||||||||||
| 264 | $cv->send(); | ||||||||||||
| 265 | } | ||||||||||||
| 266 | |||||||||||||
| 267 | =item * Focus a tab | ||||||||||||
| 268 | |||||||||||||
| 269 | $chromi->call( | ||||||||||||
| 270 | 'chrome.tabs.update', [$tab_id, { active => Types::Serialiser::true }], | ||||||||||||
| 271 | ); | ||||||||||||
| 272 | |||||||||||||
| 273 | =back | ||||||||||||
| 274 | |||||||||||||
| 275 | See also the "examples" directory: | ||||||||||||
| 276 | |||||||||||||
| 277 | =over | ||||||||||||
| 278 | |||||||||||||
| 279 | =item examples/client.pl | ||||||||||||
| 280 | |||||||||||||
| 281 | Lists the URLs of all tabs. Requires chromix-server | ||||||||||||
| 282 | |||||||||||||
| 283 | =item examples/server.pl | ||||||||||||
| 284 | |||||||||||||
| 285 | chromix-server replacement written in Perl. Additionally to chromix-server, it | ||||||||||||
| 286 | also properly supports multiple clients with one or more chrome instances. | ||||||||||||
| 287 | |||||||||||||
| 288 | =back | ||||||||||||
| 289 | |||||||||||||
| 290 | =head2 AUTHOR | ||||||||||||
| 291 | |||||||||||||
| 292 | David Schweikert <david@schweikert.ch>, heavily influenced by Chromi/Chromix by | ||||||||||||
| 293 | Stephen Blott. | ||||||||||||
| 294 | |||||||||||||
| 295 | =head2 SEE ALSO | ||||||||||||
| 296 | |||||||||||||
| 297 | =over | ||||||||||||
| 298 | |||||||||||||
| 299 | =item GitHub project | ||||||||||||
| 300 | |||||||||||||
| 301 | L<https://github.com/open-ch/AnyEvent-Chromi> | ||||||||||||
| 302 | |||||||||||||
| 303 | =item Chromi (Chrome extension) | ||||||||||||
| 304 | |||||||||||||
| 305 | L<https://github.com/smblott-github/chromi> | ||||||||||||
| 306 | |||||||||||||
| 307 | =item Chromix (command-line tool) | ||||||||||||
| 308 | |||||||||||||
| 309 | L<https://http://chromix.smblott.org/> | ||||||||||||
| 310 | |||||||||||||
| 311 | =back | ||||||||||||