npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

puppeteer-extra-plugin

v3.2.3

Published

Base class for puppeteer-extra plugins.

Downloads

1,187,106

Readme

puppeteer-extra-plugin GitHub Workflow Status Discord npm

Installation

yarn add puppeteer-extra-plugin

Changelog

  • Now written in TypeScript 🎉
  • Breaking change: Now using a named export:
// Before
const PuppeteerExtraPlugin = require('puppeteer-extra-plugin')

// After (>= v3.0.1)
const { PuppeteerExtraPlugin } = require('puppeteer-extra-plugin')

API

Table of Contents

class: PuppeteerExtraPlugin

  • opts PluginOptions?

Base class for puppeteer-extra plugins.

Provides convenience methods to avoid boilerplate.

All common puppeteer browser events will be bound to the plugin instance, if a respectively named class member is found.

Please refer to the puppeteer API documentation as well.

Example:

// hello-world-plugin.js
const { PuppeteerExtraPlugin } = require('puppeteer-extra-plugin')

class Plugin extends PuppeteerExtraPlugin {
  constructor(opts = {}) {
    super(opts)
  }

  get name() {
    return 'hello-world'
  }

  async onPageCreated(page) {
    this.debug('page created', page.url())
    const ua = await page.browser().userAgent()
    this.debug('user agent', ua)
  }
}

module.exports = function(pluginConfig) {
  return new Plugin(pluginConfig)
}

// foo.js
const puppeteer = require('puppeteer-extra')
puppeteer.use(require('./hello-world-plugin')())
;(async () => {
  const browser = await puppeteer.launch({ headless: false })
  const page = await browser.newPage()
  await page.goto('http://example.com', { waitUntil: 'domcontentloaded' })
  await browser.close()
})()

.name

Type: string

Plugin name (required).

Convention:

  • Package: puppeteer-extra-plugin-anonymize-ua
  • Name: anonymize-ua

Example:

get name () { return 'anonymize-ua' }

.defaults

Type: PluginOptions

Plugin defaults (optional).

If defined will be (deep-)merged with the (optional) user supplied options (supplied during plugin instantiation).

The result of merging defaults with user supplied options can be accessed through this.opts.

Example:

get defaults () {
  return {
    stripHeadless: true,
    makeWindows: true,
    customFn: null
  }
}

// Users can overwrite plugin defaults during instantiation:
puppeteer.use(require('puppeteer-extra-plugin-foobar')({ makeWindows: false }))
  • See: [[opts]]

.requirements

Type: PluginRequirements

Plugin requirements (optional).

Signal certain plugin requirements to the base class and the user.

Currently supported:

  • launch
    • If the plugin only supports locally created browser instances (no puppeteer.connect()), will output a warning to the user.
  • headful
    • If the plugin doesn't work in headless: true mode, will output a warning to the user.
  • dataFromPlugins
    • In case the plugin requires data from other plugins. will enable usage of this.getDataFromPlugins().
  • runLast
    • In case the plugin prefers to run after the others. Useful when the plugin needs data from others.

Example:

get requirements () {
  return new Set(['runLast', 'dataFromPlugins'])
}

.dependencies

Type: PluginDependencies

Plugin dependencies (optional).

Missing plugins will be required() by puppeteer-extra.

Example:

get dependencies () {
  return new Set(['user-preferences'])
}
// Will ensure the 'puppeteer-extra-plugin-user-preferences' plugin is loaded.

.data

Type: Array<PluginData>

Plugin data (optional).

Plugins can expose data (an array of objects), which in turn can be consumed by other plugins, that list the dataFromPlugins requirement (by using this.getDataFromPlugins()).

Convention: [ {name: 'Any name', value: 'Any value'} ]

Example:

// plugin1.js
get data () {
  return [
    {
      name: 'userPreferences',
      value: { foo: 'bar' }
    },
    {
      name: 'userPreferences',
      value: { hello: 'world' }
    }
  ]

// plugin2.js
get requirements () { return new Set(['dataFromPlugins']) }

async beforeLaunch () {
  const prefs = this.getDataFromPlugins('userPreferences').map(d => d.value)
  this.debug(prefs) // => [ { foo: 'bar' }, { hello: 'world' } ]
}
  • See: [[getDataFromPlugins]]

.opts

Type: PluginOptions

Access the plugin options (usually the defaults merged with user defined options)

To skip the auto-merging of defaults with user supplied opts don't define a defaults property and set the this._opts Object in your plugin constructor directly.

Example:

get defaults () { return { foo: "bar" } }

async onPageCreated (page) {
  this.debug(this.opts.foo) // => bar
}
  • See: [[defaults]]

.debug

Type: Debugger

Convenience debug logger based on the debug module. Will automatically namespace the logging output to the plugin package name.

# toggle output using environment variables
DEBUG=puppeteer-extra-plugin:<plugin_name> node foo.js
# to debug all the things:
DEBUG=puppeteer-extra,puppeteer-extra-plugin:* node foo.js

Example:

this.debug('hello world')
// will output e.g. 'puppeteer-extra-plugin:anonymize-ua hello world'

.beforeLaunch(options)

  • options any Puppeteer launch options

Before a new browser instance is created/launched.

Can be used to modify the puppeteer launch options by modifying or returning them.

Plugins using this method will be called in sequence to each be able to update the launch options.

Example:

async beforeLaunch (options) {
  if (this.opts.flashPluginPath) {
    options.args.push(`--ppapi-flash-path=${this.opts.flashPluginPath}`)
  }
}

.afterLaunch(browser, opts)

  • browser Puppeteer.Browser The puppeteer browser instance.
  • opts (optional, default {options:({}as Puppeteer.LaunchOptions)})

After the browser has launched.

Note: Don't assume that there will only be a single browser instance during the lifecycle of a plugin. It's possible that pupeeteer.launch will be called multiple times and more than one browser created. In order to make the plugins as stateless as possible don't store a reference to the browser instance in the plugin but rather consider alternatives.

E.g. when using onPageCreated you can get a browser reference by using page.browser().

Alternatively you could expose a class method that takes a browser instance as a parameter to work with:

const fancyPlugin = require('puppeteer-extra-plugin-fancy')()
puppeteer.use(fancyPlugin)
const browser = await puppeteer.launch()
await fancyPlugin.killBrowser(browser)

Example:

async afterLaunch (browser, opts) {
  this.debug('browser has been launched', opts.options)
}

.beforeConnect(options)

  • options Object Puppeteer connect options

Before connecting to an existing browser instance.

Can be used to modify the puppeteer connect options by modifying or returning them.

Plugins using this method will be called in sequence to each be able to update the launch options.


.afterConnect(browser, opts)

  • browser Puppeteer.Browser The puppeteer browser instance.
  • opts Object (optional, default {})
    • opts.options Object Puppeteer connect options used.

After connecting to an existing browser instance.

Note: Don't assume that there will only be a single browser instance during the lifecycle of a plugin.


.onBrowser(browser, opts)

  • browser Puppeteer.Browser The puppeteer browser instance.
  • opts any

Returns: Promise<void>

Called when a browser instance is available.

This applies to both puppeteer.launch() and puppeteer.connect().

Convenience method created for plugins that need access to a browser instance and don't mind if it has been created through launch or connect.

Note: Don't assume that there will only be a single browser instance during the lifecycle of a plugin.


.onTargetCreated(target)

  • target Puppeteer.Target

Called when a target is created, for example when a new page is opened by window.open or browser.newPage.

Note: This includes target creations in incognito browser contexts.

Note: This includes browser instances created through .launch() as well as .connect().


.onPageCreated(page, target)

  • page Puppeteer.Page
  • target Puppeteer.Target

Same as onTargetCreated but prefiltered to only contain Pages, for convenience.

Note: This includes page creations in incognito browser contexts.

Note: This includes browser instances created through .launch() as well as .connect().

Example:

async onPageCreated (page) {
  let ua = await page.browser().userAgent()
  if (this.opts.stripHeadless) {
    ua = ua.replace('HeadlessChrome/', 'Chrome/')
  }
  this.debug('new ua', ua)
  await page.setUserAgent(ua)
}

.onTargetChanged(target)

  • target Puppeteer.Target

Called when the url of a target changes.

Note: This includes target changes in incognito browser contexts.

Note: This includes browser instances created through .launch() as well as .connect().


.onTargetDestroyed(target)

  • target Puppeteer.Target

Called when a target is destroyed, for example when a page is closed.

Note: This includes target destructions in incognito browser contexts.

Note: This includes browser instances created through .launch() as well as .connect().


.onDisconnected()

Called when Puppeteer gets disconnected from the Chromium instance.

This might happen because of one of the following:

  • Chromium is closed or crashed
  • The browser.disconnect method was called

.onClose()

Deprecated: Since puppeteer v1.6.0 onDisconnected has been improved and should be used instead of onClose.

In puppeteer < v1.6.0 onDisconnected was not catching all exit scenarios. In order for plugins to clean up properly (e.g. deleting temporary files) the onClose method had been introduced.

Note: Might be called multiple times on exit.

Note: This only includes browser instances created through .launch().


.onPluginRegistered()

After the plugin has been registered in puppeteer-extra.

Normally right after puppeteer.use(plugin) is called


.getDataFromPlugins(name?)

  • name string? Filter data by name property

Returns: Array<PluginData>

Helper method to retrieve data objects from other plugins.

A plugin needs to state the dataFromPlugins requirement in order to use this method. Will be mapped to puppeteer.getPluginData.

  • See: [data]
  • See: [requirements]