A modular plugin framework for playwright to enable cool plugins through a clean interface.
Installation
yarn add playwright playwright-extra
npm install playwright playwright-extra
Changelog
Please check the announcements
channel in our discord server until we've automated readme updates. :)
- v4.3
- Rerelease due to versioning issues with previous beta packages
- v3.3
Quickstart
const { chromium } = require('playwright-extra')
const stealth = require('puppeteer-extra-plugin-stealth')()
chromium.use(stealth)
chromium.launch({ headless: true }).then(async browser => {
const page = await browser.newPage()
console.log('Testing the stealth plugin..')
await page.goto('https://bot.sannysoft.com', { waitUntil: 'networkidle' })
await page.screenshot({ path: 'stealth.png', fullPage: true })
console.log('All done, check the screenshot. ✨')
await browser.close()
})
The above example uses the compatible stealth
plugin from puppeteer-extra, that plugin needs to be installed as well:
yarn add puppeteer-extra-plugin-stealth
npm install puppeteer-extra-plugin-stealth
If you'd like to see debug output just run your script like so:
DEBUG=playwright-extra*,puppeteer-extra* node myscript.js
$env:DEBUG='playwright-extra*,puppeteer-extra*';node myscript.js
More examples
TypeScript & ESM usage
playwright-extra
and most plugins are written in TS, so you get perfect type support out of the box. :)
import { chromium } from 'playwright-extra'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
chromium.use(StealthPlugin())
chromium.launch({ headless: true }).then(async browser => {
const page = await browser.newPage()
console.log('Testing the stealth plugin..')
await page.goto('https://bot.sannysoft.com', { waitUntil: 'networkidle' })
await page.screenshot({ path: 'stealth.png', fullPage: true })
console.log('All done, check the screenshot. ✨')
await browser.close()
})
New to Typescript? Here it is in 30 seconds or less 😄:
npm i --global yarn
yarn init -y
yarn add --dev typescript @types/node esbuild esbuild-register
yarn tsc --init --target ES2020 --lib ES2020 --module commonjs --rootDir src --outDir dist
yarn add playwright playwright-extra puppeteer-extra-plugin-stealth
mkdir src
node -r esbuild-register src/index.ts
Using different browsers
import { chromium, firefox, webkit } from 'playwright-extra'
chromium.use(plugin)
firefox.use(plugin)
webkit.use(plugin)
Multiple instances with different plugins
Node.js imports are cached, therefore the default chromium
, firefox
, webkit
export from playwright-extra
will always return the same playwright instance.
import playwright from 'playwright'
import { addExtra } from 'playwright-extra'
const chromium1 = addExtra(playwright.chromium)
const chromium2 = addExtra(playwright.chromium)
chromium1.use(onePlugin)
chromium2.use(anotherPlugin)
Plugins
We're currently in the process of making the existing puppeteer-extra plugins compatible with playwright-extra, the following plugins have been successfully tested already:
- Applies various evasion techniques to make detection of an automated browser harder
- Compatible with Puppeteer & Playwright and chromium based browsers
Example: Using stealth in Playwright with custom options
import { chromium } from 'playwright-extra'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
chromium.use(StealthPlugin())
chromium.plugins.setDependencyDefaults('stealth/evasions/webgl.vendor', {
vendor: 'Bob',
renderer: 'Alice'
})
chromium.launch({ headless: true }).then(async browser => {
const page = await browser.newPage()
console.log('Testing the webgl spoofing feature of the stealth plugin..')
await page.goto('https://webglreport.com', { waitUntil: 'networkidle' })
await page.screenshot({ path: 'webgl.png', fullPage: true })
console.log('All done, check the screenshot. ✨')
await browser.close()
})
- Solves reCAPTCHAs and hCaptchas automatically, using a single line of code:
page.solveRecaptchas()
- Compatible with Puppeteer & Playwright and all browsers (chromium, firefox, webkit)
Example: Solving captchas in Playwright & Firefox
import { firefox } from 'playwright-extra'
import RecaptchaPlugin from 'puppeteer-extra-plugin-recaptcha'
firefox.use(
RecaptchaPlugin({
provider: {
id: '2captcha',
token: process.env.TWOCAPTCHA_TOKEN || 'YOUR_API_KEY'
}
})
)
firefox.launch({ headless: false }).then(async browser => {
const context = await browser.newContext()
const page = await context.newPage()
const url = 'https://www.google.com/recaptcha/api2/demo'
await page.goto(url, { waitUntil: 'networkidle' })
console.log('Solving captchas..')
await page.solveRecaptchas()
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle' }),
page.click(`#recaptcha-demo-submit`)
])
const content = await page.content()
const isSuccess = content.includes('Verification Success')
console.log('Done', { isSuccess })
await browser.close()
})
- Use multiple proxies dynamically with flexible per-host routing and more
- Compatible with Puppeteer & Playwright and all browsers (chromium, firefox, webkit)
Notes
- If you're in need of adblocking use this package or block resources natively
- We're focussing on compatiblity with existing plugins at the moment, more documentation on how to write your own playwright-extra plugins will follow
Contributors
License
Copyright © 2018 - 2023, berstend̡̲̫̹̠̖͚͓̔̄̓̐̄͛̀͘. Released under the MIT License.