Giter Site home page Giter Site logo

chrome-har-capturer's Introduction

chrome-har-capturer

Capture HAR files from a remote Chrome instance.

Under the hood this module uses chrome-remote-interface to instrument Chrome.

Usage

Start Chrome with options:

  • --remote-debugging-port=<port> to enable the Remote Debugging Protocol on the port <port>;

  • --enable-benchmarking --enable-net-benchmarking to enable the Javascript interface that allows chrome-har-capturer to flush the DNS cache and the socket pool before loading each URL.

For example:

google-chrome --remote-debugging-port=9222 \
              --enable-benchmarking \
              --enable-net-benchmarking

Use the bundled utility

Usage: cli [options] URL...

Options:

  -h, --help           output usage information
  -t, --host <host>    Remote Debugging Protocol host
  -p, --port <port>    Remote Debugging Protocol port
  -o, --output <file>  dump to file instead of stdout
  -c, --content        also capture the requests body
  -d, --delay <ms>     time to wait after the load event
  -v, --verbose        enable verbose output on stderr

This module comes with a utility that can be used to generate a cumulative HAR file from a list of URLs.

Install globally with:

sudo npm install -g chrome-har-capturer

Load a list of URL with:

chrome-har-capturer -o out.har \
    https://github.com \
    http://www.reddit.com \
    http://iwillfail \
    http://www.reddit.com/help/faq

Write a custom application

Install locally with:

npm install chrome-har-capturer

The following snippet loads an array of URLs serially and generate a cumulative HAR file, just like the Record button in the Network Panel of Chrome Developer Tools.

var fs = require('fs');
var chc = require('chrome-har-capturer');
var c = chc.load(['https://github.com',
                  'http://www.reddit.com',
                  'http://iwillfail',
                  'http://www.reddit.com/help/faq']);
c.on('connect', function () {
    console.log('Connected to Chrome');
});
c.on('end', function (har) {
    fs.writeFileSync('out.har', JSON.stringify(har));
});
c.on('error', function () {
    console.error('Cannot connect to Chrome');
});

API

load(urls, [options])

Connects to a remote instance of Google Chrome using the Remote Debugging Protocol and loads a list of URLs serially. Returns an instance of the Client class.

urls is either an array or a single URL (note that URLs must contain the schema, otherwise they will be rejected by Chrome).

options is an object with the following optional properties:

  • host: Remote Debugging Protocol host. Defaults to localhost;
  • port: Remote Debugging Protocol port. Defaults to 9222;
  • chooseTab: Callback used to determine which remote tab attach to. Takes the JSON array returned by http://host:port/json containing the tab list and must return the numeric index of a tab. Defaults to a function that returns the active one (function (tabs) { return 0; });
  • fetchContent: If true also capture the requests body. Defaults to false;
  • onLoadDelay: Milliseconds to wait after the load event is fired before stop capturing events. Defaults to 0.

setVerbose([verbose])

Enable or disable verbose prints for debugging purposes.

verbose: Verbosity flag. Defaults to true.

Class: Client

Event: 'connect'

function () {}

Emitted when a connection to Chrome has been established.

Event: 'pageStart'

function (url) {}

Emitted when Chrome is about to load url.

Event: 'pageEnd'

function (url) {}

Emitted when Chrome has finished loading url.

Event: 'pageError'

function (url) {}

Emitted when Chrome has failed loading url. Failed URLs will not appear in the cumulative HAR object.

Event: 'end'

function (har) {}

Emitted when every given URL has been loaded. har is the cumulative HAR object.

Event: 'error'

function (err) {}

Emitted when http://host:port/json can't be reached or if there are unexpected behaviors with Chrome. err in an instance of Error.

Issues

There are some known issues regarding the timings computed by this module, there are in general differences with the HAR files exported by Chrome itself via DevTools. See issue #19 for more details.

Resources

chrome-har-capturer's People

Contributors

cyrus-and avatar

Stargazers

 avatar

Watchers

 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.