Giter VIP home page Giter VIP logo

anyevent-chromi's Introduction

NAME

AnyEvent::Chromi - Remotely control Google Chrome from Perl

SYNOPSIS

# Start in client mode (need "chromix-server" or examples/server.pl)
my $chromi AnyEvent::Chromi->new(mode => 'client', on_connect => sub {
    my ($chromi) = @_;
    ...
    $chromi->call(...);
});

# Start in server mode
my $chromi AnyEvent::Chromi->new(mode => 'server');

DESCRIPTION

AnyEvent::Chromi allows you to remotely control Google Chrome from a Perl script. It requires the Chromi extension https://github.com/smblott-github/chromi, which exposes all of the Chrome Extensions API via a websocket connection.

METHODS

  • $chromi = AnyEvent::Chromi->new(mode => ..., on_connect => ...);

    • mode => 'client|server'

      If 'server' (default), it will start a websocket server on port 7441 and wait for the connection from Chrome (initiated by the Chromi extension). This is the most practical way to use AnyEvent::Chromi if you write a long-running script, because it doesn't require a separate daemon.

      If 'client', it will connect to port 7441 itself, expecting a websocket server, like the one provided by chromix-server, or by the examples/server.pl script.

    • port => N

      Use port N instead of 7441.

    • on_connect => sub { my ($chromi) = @_; ... }

      Will be executed as soon as Chrome connects (in server mode), or as the connection to the websocket server is done.

  • $chromi->call($method, $args, $cb)

    Call the Chrome extension method $method, e.g. chrome.windows.getAll.

    $args is expected to be a ARRAYREF with the arguments for the method. It will be converted to JSON by AnyEvent::Chromi.

    $cb is a callback for when the reply is received. The first argument to the callback is the status (either "done" or "error"), and the second is a ARRAYREF with the data.

    Note: you need to make sure that the JSON::XS serialization is generating the proper data types. This is particularly important for booleans, where Types::Serialiser::true and Types::Serialiser::false can be used.

  • $chromi->is_connected

    In server mode: returns true if Chrome is connected and awaits commands.

    In client mode: returns true if connected to chromix-server.

EXAMPLES

  • List all tabs

      $chromi->call(
          'chrome.windows.getAll', [{ populate => Types::Serialiser::true }],
          sub {
              my ($status, $reply) = @_;
              $status eq 'done' or return;
              defined $reply and ref $reply eq 'ARRAY' or return;
              map { say "$_->{url}" } @{$reply->[0]{tabs}};
              $cv->send();
          }
    
  • Focus a tab

      $chromi->call(
          'chrome.tabs.update', [$tab_id, { active => Types::Serialiser::true }],
      );
    

See also the "examples" directory:

  • examples/client.pl

    Lists the URLs of all tabs. Requires chromix-server

  • examples/server.pl

    chromix-server replacement written in Perl. Additionally to chromix-server, it also properly supports multiple clients with one or more chrome instances.

AUTHOR

David Schweikert <[email protected]>, heavily influenced by Chromi/Chromix by Stephen Blott.

SEE ALSO

anyevent-chromi's People

Contributors

raphaelthomas avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.