A light-weight wrapper around puppeteer
to enable cool plugins through a clean interface.
Installation
yarn add puppeteer puppeteer-extra
npm install puppeteer puppeteer-extra
yarn add puppeteer@2.0.0 puppeteer-extra
Quickstart
const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
const UserAgentPlugin = require('puppeteer-extra-plugin-anonymize-ua')
puppeteer.use(UserAgentPlugin({ makeWindows: true }))
puppeteer.launch({ headless: true }).then(async browser => {
const page = await browser.newPage()
await page.setViewport({ width: 800, height: 600 })
console.log(`Testing the user agent plugin..`)
await page.goto('https://httpbin.org/headers')
await page.waitFor(1000)
await page.screenshot({ path: 'headers.png', fullPage: true })
console.log(`Testing the stealth plugin..`)
await page.goto('https://bot.sannysoft.com')
await page.waitFor(5000)
await page.screenshot({ path: 'stealth.png', fullPage: true })
console.log(`All done, check the screenshots. ✨`)
await browser.close()
})
The above example uses the stealth
and anonymize-ua
plugin, which need to be installed as well:
yarn add puppeteer-extra-plugin-stealth puppeteer-extra-plugin-anonymize-ua
npm install puppeteer-extra-plugin-stealth puppeteer-extra-plugin-anonymize-ua
If you'd like to see debug output just run your script like so:
DEBUG=puppeteer-extra,puppeteer-extra-plugin:* node myscript.js
More examples
TypeScript usage
import puppeteer from 'puppeteer-extra'
import RecaptchaPlugin from 'puppeteer-extra-plugin-recaptcha'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
puppeteer.use(RecaptchaPlugin()).use(StealthPlugin())
puppeteer.launch({ headless: false }).then(async browser => {
const page = await browser.newPage()
await page.setViewport({ width: 800, height: 600 })
await page.goto('https://bot.sannysoft.com')
await page.waitFor(5000)
await page.screenshot({ path: 'stealth.png', fullPage: true })
await browser.close()
})
Firefox usage
const { addExtra } = require('puppeteer-extra')
const puppeteer = addExtra(require('puppeteer-firefox'))
puppeteer.launch({ headless: false }).then(async browser => {
const page = await browser.newPage()
await page.setViewport({ width: 800, height: 600 })
await page.goto('https://www.spacejam.com/archive/spacejam/movie/jam.htm')
await page.waitFor(10 * 1000)
await browser.close()
})
Plugins
- Solves reCAPTCHAs automatically, using a single line of code:
page.solveRecaptchas()
.
- Applies various evasion techniques to make detection of headless puppeteer harder.
- Makes puppeteer browser debugging possible from anywhere.
- Creates a secure tunnel to make the devtools frontend (incl. screencasting) accessible from the public internet
- Makes quick puppeteer debugging and exploration fun with an interactive REPL.
- Blocks resources (images, media, css, etc.) in puppeteer.
- Supports all resource types, blocking can be toggled dynamically.
- Allows flash content to run on all sites without user interaction.
- Anonymizes the user-agent on all pages.
- Supports dynamic replacing, so the browser version stays intact and recent.
Check out the packages folder for more plugins.
Further info
Contributing
PRs and new plugins are welcome! 🎉 The plugin API for puppeteer-extra
is clean and fun to use. Have a look the PuppeteerExtraPlugin base class documentation to get going and check out the existing plugins (minimal example is the anonymize-ua plugin) for reference.
We use a monorepo powered by Lerna (and yarn workspaces), ava for testing, TypeScript for the core, the standard style for linting and JSDoc heavily to auto-generate markdown documentation based on code. :-)
Kudos
Compatibility
puppeteer-extra
and all plugins are tested continously against all relevant NodeJS (v8-v13) and puppeteer versions.
We never broke compatibility and still support puppeteer down to version 1.6.2 (Released Aug 1, 2018).
A few plugins won't work in headless mode (it's noted if that's the case) due to Chrome limitations (e.g. the user-preferences
plugin), look into xvfb-run
if you still require a headless experience in these circumstances.
Changelog
2.1.6 ➠ 3.1.1
2.1.6
➠ 3.1.1
Big refactor, the core is now written in TypeScript 🎉
That means out of the box type safety for fellow TS users and nice auto-completion in VSCode for JS users. Also:
- A new
addExtra
export, to patch any puppeteer compatible library with plugin functionality (puppeteer-firefox
, chrome-aws-lambda
, etc). This also allows for multiple puppeteer instances with different plugins.
The API is backwards compatible, I bumped the major version just in case I missed something. Please report any issues you might find with the new release. :)
API
Table of Contents
Modular plugin framework to teach puppeteer
new tricks.
This module acts as a drop-in replacement for puppeteer
.
Allows PuppeteerExtraPlugin's to register themselves and
to extend puppeteer with additional functionality.
Example:
const puppeteer = require('puppeteer-extra')
puppeteer.use(require('puppeteer-extra-plugin-anonymize-ua')())
puppeteer.use(
require('puppeteer-extra-plugin-font-size')({ defaultFontSize: 18 })
)
;(async () => {
const browser = await puppeteer.launch({ headless: false })
const page = await browser.newPage()
await page.goto('http://example.com', { waitUntil: 'domcontentloaded' })
await browser.close()
})()
plugin
PuppeteerExtraPlugin
Returns: this The same PuppeteerExtra
instance (for optional chaining)
The main interface to register puppeteer-extra
plugins.
Example:
puppeteer.use(plugin1).use(plugin2)
Returns: Promise<Puppeteer.Browser>
The method launches a browser instance with given arguments. The browser will be closed when the parent node.js process is closed.
Augments the original puppeteer.launch
method with plugin lifecycle methods.
All registered plugins that have a beforeLaunch
method will be called
in sequence to potentially update the options
Object before launching the browser.
Example:
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null
})
Returns: Promise<Puppeteer.Browser>
Attach Puppeteer to an existing Chromium instance.
Augments the original puppeteer.connect
method with plugin lifecycle methods.
All registered plugins that have a beforeConnect
method will be called
in sequence to potentially update the options
Object before launching the browser.
Returns: Array<string>
The default flags that Chromium will be launched with.
Returns: string
Path where Puppeteer expects to find bundled Chromium.
Returns: Puppeteer.BrowserFetcher
This methods attaches Puppeteer to an existing Chromium instance.
Type: Array<PuppeteerExtraPlugin>
Get a list of all registered plugins.
name
string? Filter data by optional plugin name
Collects the exposed data
property of all registered plugins.
Will be reduced/flattened to a single array.
Can be accessed by plugins that listed the dataFromPlugins
requirement.
Implemented mainly for plugins that need data from other plugins (e.g. user-preferences
).
Type: PuppeteerExtra
The default export will behave exactly the same as the regular puppeteer
(just with extra plugin functionality) and can be used as a drop-in replacement.
Behind the scenes it will try to require either puppeteer
or puppeteer-core
from the installed dependencies.
Example:
const puppeteer = require('puppeteer-extra')
import puppeteer from 'puppeteer-extra'
puppeteer.use(...)
puppeteer
VanillaPuppeteer Any puppeteer API-compatible puppeteer implementation or version.
Returns: PuppeteerExtra A fresh PuppeteerExtra instance using the provided puppeteer
An alternative way to use puppeteer-extra
: Augments the provided puppeteer with extra plugin functionality.
This is useful in case you need multiple puppeteer instances with different plugins or to add plugins to a non-standard puppeteer package.
Example:
const { addExtra } = require('puppeteer-extra')
import { addExtra } from 'puppeteer-extra'
const puppeteer = addExtra(require('puppeteer-firefox'))
puppeteer.use(...)
License
Copyright © 2019, berstend̡̲̫̹̠̖͚͓̔̄̓̐̄͛̀͘. Released under the MIT License.