Scroll to navigation

Chrome::DevToolsProtocol(3pm) User Contributed Perl Documentation Chrome::DevToolsProtocol(3pm)

NAME

Chrome::DevToolsProtocol - asynchronous dispatcher for the DevTools protocol

SYNOPSIS

    # Usually, WWW::Mechanize::Chrome automatically creates a driver for you
    my $driver = Chrome::DevToolsProtocol->new(
        port => 9222,
        host => '127.0.0.1',
        auto_close => 0,
        error_handler => sub {
            # Reraise the error
            croak $_[1]
        },
    );
    $driver->connect( new_tab => 1 )->get

METHODS

"->new( %args )"

    my $driver = Chrome::DevToolsProtocol->new(
        port => 9222,
        host => '127.0.0.1',
        auto_close => 0,
        error_handler => sub {
            # Reraise the error
            croak $_[1]
        },
    );

These members can mostly be set through the constructor arguments:

The hostname to connect to
The port to connect to
The JSON decoder used
The Future::HTTP instance to talk to the Chrome DevTools
Which tab to reuse (if any)
A premade Log::Log4perl object to act as logger
The event-loop specific transport backend

"->future"

    my $f = $driver->future();

Returns a backend-specific generic future

"->endpoint"

    my $url = $driver->endpoint();

Returns the URL endpoint to talk to for the connected tab

"json_log_fh"

Filehandle where all communications will be logged to, one line per message/response. Each line will be of the format

  { type: "message", payload: { ... } }
  { type: "reply", payload: { ... } }
  { type: "event", payload: { ... } }

The filehandle must be opened as :raw , as UTF-8 encoded bytes will be written to it.

"->add_listener"

    my $l = $driver->add_listener(
        'Page.domContentEventFired',
        sub {
            warn "The DOMContent event was fired";
        },
    );
    # ...
    undef $l; # stop listening

Adds a callback for the given event name. The callback will be removed once the return value goes out of scope.

"->remove_listener"

    $driver->remove_listener($l);

Explicitly remove a listener.

"->log"

    $driver->log('debug', "Warbling doodads", { doodad => 'this' } );

Log a message

"->connect"

    my $f = $driver->connect()->get;

Asynchronously connect to the Chrome browser, returning a Future.

"->close"

    $driver->close();

Shut down the connection to Chrome

"->sleep"

    $driver->sleep(0.2)->get;

Sleep for the amount of seconds in an event-loop compatible way

"->one_shot"

    my $f = $driver->one_shot('Page.domContentEventFired')->get;

Returns a future that resolves when the event is received

"$chrome->json_get"

    my $data = $driver->json_get( 'version' )->get;

Requests an URL and returns decoded JSON from the future

"$chrome->send_packet"

  $chrome->send_packet('Page.handleJavaScriptDialog',
      accept => JSON::true,
  );

Sends a JSON packet to the remote end

"$chrome->send_message"

  my $future = $chrome->send_message('DOM.querySelectorAll',
      selector => 'p',
      nodeId => $node,
  );
  my $nodes = $future->get;

This function expects a response. The future will not be resolved until Chrome has sent a response to this query.

"$chrome->callFunctionOn"

"$chrome->evaluate"

"$chrome->eval"

    my $result = $chrome->eval('2+2');

Evaluates a Javascript string and returns the result.

"$chrome->version_info"

    print $chrome->version_info->get->{"Protocol-Version"};

Returns the implemented ChromeDevTooslProtocol protocol version.

"$chrome->protocol_version"

    print $chrome->protocol_version->get;

"$target->getVersion"

Returns information about the Chrome instance we are connected to.

"$chrome->get_domains"

    my $schema = $chrome->get_domains->get;

Returns the topics of this Chrome DevToolsProtocol implementation.

"$chrome->list_tabs"

  my @tabs = $chrome->list_tabs->get();

"$chrome->new_tab"

    my $new_tab = $chrome->new_tab('https://www.google.com')->get;

"$chrome->activate_tab"

    $chrome->activate_tab( $tab )->get

Brings the tab to the foreground of the application

"$chrome->close_tab"

    $chrome->close_tab( $tab )->get

Closes the tab

"$chrome->getTargets"

    my @targets = $chrome->getTargets->get;

Gets the list of available targets

"$target->getTargetInfo"

    my $info = $chrome->getTargetInfo( $targetId )->get;
    print $info->{title};

Returns information about the current target

"$target->createTarget"

    my $targetId = $chrome->createTarget(
        url => 'about:blank',
        width => 1280,
        height => 800,
        newWindow => JSON::false,
        background => JSON::false,
    )->get;
    print $targetId;

Creates a new target, optionally in a new window

"$target->attachToTarget"

    my $sessionId = $chrome->attachToTarget(
        targetId => $targetId,
    )->get;
    print $sessionId;

Attaches to the target so we receive events generated by the target

"$target->closeTarget"

    my $targetId = $chrome->closeTarget(
        targetId => $targetId
    )->get;

Creates a new target

"$target->getWindowForTarget"

    my $info = $chrome->getWindowForTarget( $targetId )->get;
    print $info->{windowId};

Returns information about the window of the current target

"$target->setWindowBoundsForTarget"

    my $info = $chrome->getWindowForTarget( $targetId )->get;
    $info->{bounds}->{width} = 100;
    $chrome->setWindowBounds( $info->{windowId}, $info->{bounds} )->get;

Sets the window dimensions of the current target

"$chrome->getBrowserContexts"

    my @browserContextIds = $chrome->getBrowserContexts->get;

Gets the list of available browser contexts. These are separate sets of user cookies etc.

SEE ALSO

The unofficial Chrome debugger API documentation at <https://github.com/buggerjs/bugger-daemon/blob/master/README.md#api>

Chrome DevTools at <https://chromedevtools.github.io/devtools-protocol/1-2>

REPOSITORY

The public repository of this module is <https://github.com/Corion/www-mechanize-chrome>.

SUPPORT

The public support forum of this module is <https://perlmonks.org/>.

BUG TRACKER

Please report bugs in this module via the Github bug queue at <https://github.com/Corion/WWW-Mechanize-Chrome/issues>

AUTHOR

Max Maischein "corion@cpan.org"

COPYRIGHT (c)

Copyright 2010-2024 by Max Maischein "corion@cpan.org".

LICENSE

This module is released under the same terms as Perl itself.

2026-02-25 perl v5.40.1