Giter Club home page Giter Club logo

chrome-har-capturer's Introduction

chrome-har-capturer Build Status

Capture HAR files from a headless Chrome instance.

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

Screenshot

Setup

Install this module from NPM:

npm install chrome-har-capturer

Start Chrome like this:

google-chrome --remote-debugging-port=9222 --headless

Important note: this is a complete rewrite that uses brand new JavaScript features and takes full advantage of the headless mode of Chrome; the upshot is that it requires Node.js version 7.6.0+ and can only work in headless mode. It also introduces breaking changes in the API. If these requirements are unfeasible stick with version 0.9.5 but consider that it will not be supported anymore.

Command line utility

The command line utility can be used to generate HAR files from a list of URLs. The following options are available:

-h, --help           output usage information
-t, --host <host>    Chrome Debugging Protocol host
-p, --port <port>    Chrome Debugging Protocol port
-x, --width <dip>    frame width in DIP
-y, --height <dip>   frame height in DIP
-o, --output <file>  write to file instead of stdout
-c, --content        also capture the requests body
-a, --agent <agent>  user agent override
-i, --insecure       ignore certificate errors
-g, --grace <ms>     time to wait after the load event
-u, --timeout <ms>   time to wait before giving up the URL
-l, --parallel <n>   load <n> URLs in parallel

Library

Alternatively this module provides a simple API that can be used to write custom applications. See the command line utility source code for a working example.

API

run(urls, [options])

Start the loading of a batch of URLs. Returns an event emitter (see below for the list of supported events).

urls is array of URLs.

options is an object with the following optional properties:

  • host: Chrome Debugging Protocol host. Defaults to localhost;

  • port: Chrome Debugging Protocol port. Defaults to 9222;

  • width: frame width in DIP. Defaults to a Chrome-defined value;

  • height: frame height in DIP. Defaults to a Chrome-defined value;

  • content: if true also capture the requests body. Defaults to false;

  • timeout: milliseconds to wait before giving up with a URL;

  • parallel: if true load the URLs in parallel (warning: this may spoil time-based metrics). Defaults to false;

  • preHook: function returning a Promise executed before each page load:

    • url: the current URL;
    • client: CDP client instance;
    • index: index of url in urls;
    • urls: input URL array.
  • postHook: function returning a Promise executed after each page load event:

    • url: the current URL;
    • client: CDP client instance;
    • index: index of url in urls;
    • urls: input URL array.

    If this hook resolves to a value then it is included in the resulting HAR object as the value of the _user key of the this URL's page object.

Event: 'load'

function (url, index, urls) {}

Emitted when Chrome is about to load url. index is the index of url in urls. urls is the array passed to run().

Event: 'done'

function (url, index, urls) {}

Emitted when Chrome finished loading url. index is the index of url in urls. urls is the array passed to run().

Event: fail'

function (url, err, index, urls) {}

Emitted when Chrome cannot load url. The Error object err contains the failure reason. Failed URLs will not appear in the resulting HAR object. index is the index of url in urls. urls is the array passed to run().

Event: 'har'

function (har) {}

Emitted when all the URLs have been processed. If all the URLs fails then a valid empty HAR object is returned. har is the resulting HAR object.

Resources

chrome-har-capturer's People

Contributors

calvinnwq avatar cyrus-and avatar paulirish avatar

Watchers

 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.