Git Product home page Git Product logo

capture-all's Introduction

capture-all

Flexible utility to get screenshots from Web pages

Install

$ npm i capture-all
# or
$ yarn add capture-all

Example

const { captureAll } = require('capture-all')
const fs = require('fs')

captureAll([
  {
    // Web page URL which will be captured
    url: 'https://www.google.com/',

    // Selector for capturing element
    target: 'body',

    // Selectors to hide from capture (add `visibility: hidden;` to the elements)
    hidden: ['#gb'],

    // Selectors to remove from capture (add `display: none;` to the elements)
    remove: ['#fbar'],

    // Delay (milliseconds) before taking screenshot
    delay: 3000,

    // Viewport size of a browser
    viewport: {
      width: 1024,
      height: 800
    }
  }
]).then(results => {
  results.forEach((result, i) => {
    fs.writeFileSync(`result-${i}.png`, result.image)
  })
})

Hooking Capturing Processing

You can define detailed behavior of the capturing processing with capture option. The capture option is an function receving Puppeteer's Page instance as the 1st argument and a capturing function as the 2nd argument. You have to call the 2nd argument function by your hand when you specify capture option.

const { captureAll } = require('capture-all')
const fs = require('fs')

captureAll([
  {
    url: 'https://www.google.com/',
    target: 'body',
    viewport: {
      width: 1024,
      height: 800
    },

    // Define the behavior around capturing
    capture: async (page, capture) => {
      // Click a button in the page by using Puppeteer API
      const button = await page.$('#some-button')
      await button.click()

      // Capture the page at this moment
      await capture()

      // Click the button again
      await button.click()

      // Capture the page again
      await capture()
    }
  }
]).then(results => {
  results.forEach((result, i) => {
    fs.writeFileSync(`result-${i}.png`, result.image)
  })
})

API

captureAll(targets: CaptureTarget[], options?: CaptureOptions): Promise<CaptureResult[]>

Capture screenshots of Web pages which specified by targets and return an array of CaptureResult object including captured image buffer.

CaptureTarget may have the following properties:

  • url: Web page URL which will be captured (required)
  • target: a selector for capturing element
  • hidden: an array of selector to hide matched elements from captured image
  • remove: an array of selector to remove matched elements from captured image
  • disableCssAnimation: true if css animations / transitions are to be disabled. (default: true)
  • delay: Delay (milliseconds) before taking screenshot
  • viewport: viewport size of browser
  • capture: You can hook the capturing processing by using this option. See Hooking Capturing Processing for details.

CaptureOptions may have the following properties:

  • concurrency: a number of process which will be created for capture
  • puppeteer: an object passed to puppeteer.launch

CaptureResult has the following properties:

  • index: Index of capture results generated by the same CaptureTarget. The result can be more than one if you specify capture option.
  • image: captured image buffer
  • url: captured Web page URL
  • target: a selector of captured element
  • hidden: an array of selector which is hidden from captured image
  • remove: an array of selector which is removed from captured image
  • disableCssAnimation: true if css animations / transitions are to be disabled
  • delay: Delay (milliseconds) before taking screenshot
  • viewport: viewport size of browser

createCaptureStream(targets: CaptureTarget[], options?: CaptureOptions): ReadableStream<CaptureResult>

Similar to captureAll but returns readable stream of CaptureResult instead.

License

MIT

capture-all's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar ktsn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cumet04

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.