### Quickstart: Launch Puppeteer with REPL plugin
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-repl/readme.md
Launch Puppeteer and enable the REPL plugin. This example demonstrates how to use the plugin to start interactive REPL sessions with `page` and `browser` instances. The plugin is required and then used with `puppeteer.use()`.
```javascript
const puppeteer = require('puppeteer-extra')
puppeteer.use(require('puppeteer-extra-plugin-repl')())
puppeteer.launch({ headless: true }).then(async browser => {
const page = await browser.newPage()
await page.goto('https://example.com')
// Start an interactive REPL here with the `page` instance.
await page.repl()
// Afterwards start REPL with the `browser` instance.
await browser.repl()
await browser.close()
})
```
--------------------------------
### Quickstart: Using puppeteer-extra with Stealth and Adblocker Plugins
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra/readme.md
Demonstrates how to use puppeteer-extra as a drop-in replacement for Puppeteer, integrating the stealth and adblocker plugins. It launches a browser, navigates to specified URLs, takes screenshots, and logs output. This example requires `puppeteer-extra-plugin-stealth` and `puppeteer-extra-plugin-adblocker` to be installed.
```javascript
// puppeteer-extra is a drop-in replacement for puppeteer,
// it augments the installed puppeteer with plugin functionality.
// Any number of plugins can be added through `puppeteer.use()`
const puppeteer = require('puppeteer-extra')
// Add stealth plugin and use defaults (all tricks to hide puppeteer usage)
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
// Add adblocker plugin to block all ads and trackers (saves bandwidth)
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker')
puppeteer.use(AdblockerPlugin({ blockTrackers: true }))
// That's it, the rest is puppeteer usage as normal 😊
puppeteer.launch({ headless: true }).then(async browser => {
const page = await browser.newPage()
await page.setViewport({ width: 800, height: 600 })
console.log(`Testing adblocker plugin..`)
await page.goto('https://www.vanityfair.com')
await page.waitForTimeout(1000)
await page.screenshot({ path: 'adblocker.png', fullPage: true })
console.log(`Testing the stealth plugin..`)
await page.goto('https://bot.sannysoft.com')
await page.waitForTimeout(5000)
await page.screenshot({ path: 'stealth.png', fullPage: true })
console.log(`All done, check the screenshots. ✨`)
await browser.close()
})
```
--------------------------------
### Quickstart: Launch Playwright with Stealth Plugin (TypeScript/ESM)
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/playwright-extra/readme.md
Provides a TypeScript example for using playwright-extra with the stealth plugin, leveraging ES Module syntax. It demonstrates importing modules and using the stealth plugin similarly to the JavaScript example. Requires 'playwright-extra-plugin-stealth' to be installed.
```typescript
// playwright-extra is a drop-in replacement for playwright,
// it augments the installed playwright with plugin functionality
import { chromium } from 'playwright-extra'
// Load the stealth plugin and use defaults (all tricks to hide playwright usage)
// Note: playwright-extra is compatible with most puppeteer-extra plugins
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
// Add the plugin to playwright (any number of plugins can be added)
chromium.use(StealthPlugin())
// ...(the rest of the quickstart code example is the same)
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()
})
```
--------------------------------
### Setup TypeScript Project for Playwright-Extra
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/playwright-extra/readme.md
Guides through setting up a new TypeScript project for use with Playwright-Extra. It covers installing necessary dependencies like TypeScript, Node types, esbuild, and Playwright related packages, along with configuring tsconfig.json and running TypeScript files directly.
```bash
# Optional: If you don't have yarn yet
npm i --global yarn
# Optional: Create new package.json if it's a new project
yarn init -y
# Add basic typescript dependencies
yarn add --dev typescript @types/node esbuild esbuild-register
# Bootstrap a tsconfig.json
yarn tsc --init --target ES2020 --lib ES2020 --module commonjs --rootDir src --outDir dist
# Add dependencies used in the quick start example
yarn add playwright playwright-extra puppeteer-extra-plugin-stealth
# Create source folder for the .ts files
mkdir src
# Now place the example code above in `src/index.ts`
# Run the typescript code without the need of compiling it first
node -r esbuild-register src/index.ts
```
--------------------------------
### Install puppeteer-extra-plugin-adblocker
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-adblocker/readme.md
Installs the adblocker plugin and its core dependencies using either Yarn or npm. This is the initial setup step for using the plugin.
```bash
yarn add puppeteer-extra-plugin-adblocker
# - or -
npm install puppeteer-extra-plugin-adblocker
```
```bash
yarn add puppeteer puppeteer-extra puppeteer-extra-plugin-adblocker
# - or -
npm install puppeteer puppeteer-extra puppeteer-extra-plugin-adblocker
```
--------------------------------
### REPL Usage Examples
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-repl/readme.md
Examples of interacting with the REPL. These demonstrate common Puppeteer actions like getting the current URL, clicking elements, navigating, typing into input fields, and evaluating JavaScript within the page context, all from within the interactive REPL.
```javascript
> page.url()
// => https://example.com
> page.click('a')
> page.url()
// => https://www.iana.org/domains/reserved
> page.content()
// =>
...
> page.goto('https://google.com')
> page.type('input', 'what is the answer to life the universe and everything')
> page.click('input[type=submit]')
> page.url()
// => https://www.google.com/search?source=hp&ei=u9oXW5HpO8a ...
> page.evaluate(() => document.querySelector('h3 a').textContent)
// => Question 42 (The Impossible Quiz) - The Impossible Quiz Wiki - Fandom
```
--------------------------------
### Install puppeteer-extra and stealth plugin
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-stealth/readme.md
Installs puppeteer, puppeteer-extra, and the stealth plugin together. This is recommended for first-time users of puppeteer-extra plugins.
```bash
yarn add puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
# - or -
pm install puppeteer puppeteer-extra puppeteer-extra-plugin-stealth
```
--------------------------------
### Install puppeteer-extra-plugin-block-resources
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-block-resources/readme.md
Installs the puppeteer-extra-plugin-block-resources package using Yarn. This is the first step to integrate the plugin into your project.
```bash
yarn add puppeteer-extra-plugin-block-resources
```
--------------------------------
### Quickstart: Launch Playwright with Stealth Plugin (JavaScript)
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/playwright-extra/readme.md
Demonstrates how to use the playwright-extra library with the stealth plugin to hide Playwright's usage. It launches a browser, navigates to a test page, takes a screenshot, and then closes the browser. Requires 'playwright-extra-plugin-stealth' to be installed.
```javascript
// playwright-extra is a drop-in replacement for playwright,
// it augments the installed playwright with plugin functionality
const { chromium } = require('playwright-extra')
// Load the stealth plugin and use defaults (all tricks to hide playwright usage)
// Note: playwright-extra is compatible with most puppeteer-extra plugins
const stealth = require('puppeteer-extra-plugin-stealth')()
// Add the plugin to playwright (any number of plugins can be added)
chromium.use(stealth)
// That's it, the rest is playwright usage as normal 😊
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()
})
```
--------------------------------
### Install Playwright-Extra
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/playwright-extra/readme.md
Installs the playwright-extra package and playwright itself using either yarn or npm. This is the initial step to use the plugin framework.
```bash
yarn add playwright playwright-extra
# - or -
npm install playwright playwright-extra
```
--------------------------------
### Install puppeteer-extra-plugin-devtools
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-devtools/readme.md
Installs the puppeteer-extra-plugin-devtools package using Yarn.
```bash
yarn add puppeteer-extra-plugin-devtools
```
--------------------------------
### Install puppeteer-extra-plugin-click-and-wait
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-click-and-wait/readme.md
Installs the puppeteer-extra-plugin-click-and-wait package using Yarn. This is a prerequisite for using the plugin in your project.
```bash
yarn add puppeteer-extra-plugin-click-and-wait
```
--------------------------------
### Starting and Managing 3proxy Service
Source: https://github.com/berstend/puppeteer-extra/wiki/Using-proxies
These bash commands demonstrate how to start the 3proxy service using a specified configuration file, monitor its log output in real-time, and reload its configuration by signaling the process. This allows for dynamic updates to proxy settings, such as changing IP addresses by modifying the authentication credentials.
```bash
3proxy /path/to/config-file.cfg
```
```bash
tail -f /tmp/3proxy.log
```
```bash
kill -SIGUSR1 $(cat /tmp/3proxy.pid)
```
--------------------------------
### Install puppeteer-extra-plugin-font-size
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-font-size/readme.md
Installs the puppeteer-extra-plugin-font-size package using Yarn. This is the first step before using the plugin in your project.
```bash
yarn add puppeteer-extra-plugin-font-size
```
--------------------------------
### Install puppeteer-extra-plugin-repl
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-repl/readme.md
Install the puppeteer-extra-plugin-repl package using Yarn. This is the first step to integrating the REPL functionality into your Puppeteer project.
```bash
yarn add puppeteer-extra-plugin-repl
```
--------------------------------
### Install puppeteer-extra-plugin-anonymize-ua
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-anonymize-ua/readme.md
Install the puppeteer-extra-plugin-anonymize-ua package using yarn.
```bash
yarn add puppeteer-extra-plugin-anonymize-ua
```
--------------------------------
### Install puppeteer-extra-plugin
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin/readme.md
This command installs the puppeteer-extra-plugin package using Yarn. It's a prerequisite for developing custom plugins.
```bash
yarn add puppeteer-extra-plugin
```
--------------------------------
### Quickstart: Launch Puppeteer with DevTools Tunnel
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-devtools/readme.md
Demonstrates how to use the puppeteer-extra-plugin-devtools to launch a headless Puppeteer browser and create a public tunnel to its DevTools frontend.
```javascript
const puppeteer = require('puppeteer-extra')
const devtools = require('puppeteer-extra-plugin-devtools')()
puppeteer.use(devtools)
puppeteer
.launch({ headless: true, defaultViewport: null })
.then(async browser => {
console.log('Start')
const tunnel = await devtools.createTunnel(browser)
console.log(tunnel.url)
const page = await browser.newPage()
await page.goto('https://example.com')
console.log('All setup.')
})
```
--------------------------------
### Plugin Name Convention Example
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin/readme.md
Provides an example of how to define the 'name' property for a custom puppeteer-extra plugin, following the recommended convention.
```javascript
get name () { return 'anonymize-ua' }
```
--------------------------------
### Install puppeteer-extra-plugin-flash with Yarn
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-flash/readme.md
This snippet shows how to install the puppeteer-extra-plugin-flash package using the yarn package manager. This is the first step to using the plugin in a project.
```bash
yarn add puppeteer-extra-plugin-flash
```
--------------------------------
### Basic Plugin Example with Puppeteer Extra
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin/readme.md
Illustrates how to create a custom plugin ('hello-world-plugin.js') that extends PuppeteerExtraPlugin and how to use it with puppeteer-extra ('foo.js'). The plugin logs page creation events and user agent information.
```javascript
// 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()
})()
```
--------------------------------
### Install puppeteer-extra-plugin-user-preferences with Yarn
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-user-preferences/readme.md
This snippet shows how to install the puppeteer-extra-plugin-user-preferences package using the Yarn package manager. This is a prerequisite for using the plugin in your project.
```bash
yarn add puppeteer-extra-plugin-user-preferences
```
--------------------------------
### TypeScript and ESM Usage with Proxy Router (Playwright & Puppeteer)
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/plugin-proxy-router/readme.md
Demonstrates how to import and use the ProxyRouter plugin in a TypeScript environment using ECMAScript Modules (ESM). Separate examples are provided for Playwright (using `firefox` as an example) and Puppeteer, showing the import syntax for both the browser automation library and the plugin.
```javascript
// You can use any browser: chromium, firefox, webkit
import { firefox } from 'playwright-extra'
import ProxyRouter from '@extra/proxy-router'
// ...
firefox.use(proxyRouter)
```
```javascript
import puppeteer from 'puppeteer-extra'
import ProxyRouter from '@extra/proxy-router'
// ...
puppeteer.use(proxyRouter)
```
--------------------------------
### Install puppeteer-extra-plugin-stealth
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-stealth/readme.md
Installs the puppeteer-extra-plugin-stealth package using either yarn or npm. This is the initial step to integrate stealth functionalities.
```bash
yarn add puppeteer-extra-plugin-stealth
# - or -
pm install puppeteer-extra-plugin-stealth
```
--------------------------------
### Configure Single Proxy for All Connections (Playwright)
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/plugin-proxy-router/readme.md
This example demonstrates how to configure the ProxyRouter plugin to use a single default proxy for all browser connections. It shows setting up the proxy with authentication and launching a browser to verify the outbound IP address. This is useful for ensuring all traffic goes through a specific proxy.
```javascript
// playwright-extra is a drop-in replacement for playwright,
// it augments the installed playwright with plugin functionality
// Note: Instead of chromium you can use firefox and webkit as well.
const { chromium } = require('playwright-extra')
// Configure and add the proxy router plugin with a default proxy
const ProxyRouter = require('@extra/proxy-router')
chromium.use(
ProxyRouter({
proxies: { DEFAULT: 'http://user:pass@proxyhost:port' },
})
)
// That's it, the default proxy will be used and proxy authentication handled automatically
chromium.launch({ headless: false }).then(async (browser) => {
const page = await browser.newPage()
await page.goto('https://canhazip.com', { waitUntil: 'domcontentloaded' })
const ip = await page.evaluate('document.body.innerText')
console.log('Outbound IP:', ip)
await browser.close()
})
```
--------------------------------
### Install puppeteer-extra and core Puppeteer
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra/readme.md
Installs the puppeteer-extra library along with the core Puppeteer package. Supports both yarn and npm package managers. You can also specify a particular version of Puppeteer.
```bash
yarn add puppeteer puppeteer-extra
# - or -
npm install puppeteer puppeteer-extra
# puppeteer-extra works with any puppeteer version:
yarn add puppeteer@2.0.0 puppeteer-extra
```
--------------------------------
### Dynamic Proxy Routing by Hostname (Playwright)
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/plugin-proxy-router/readme.md
This example illustrates advanced proxy routing using the ProxyRouter plugin. It configures multiple proxies and defines a `routeByHost` function to dynamically select which proxy to use for different hostnames, or to block/use direct connections. It also logs connection statistics, including bytes used per proxy and host.
```javascript
// playwright-extra is a drop-in replacement for playwright,
// it augments the installed playwright with plugin functionality
// Note: Instead of chromium you can use firefox and webkit as well.
const { chromium } = require('playwright-extra')
// Configure the proxy router plugin
const ProxyRouter = require('@extra/proxy-router')
const proxyRouter = ProxyRouter({
// define the available proxies (replace this with your proxies)
proxies: {
// the default browser proxy, can be `null` as well for direct connections
DEFAULT: 'http://user:pass@proxyhost:port',
// optionally define more proxies you can use in `routeByHost`
// you can use whatever names you'd like for them
DATACENTER: 'http://user:pass@proxyhost2:port',
RESIDENTIAL_US: 'http://user:pass@proxyhost3:port',
},
// optional function for flexible proxy routing
// if this is not specified the `DEFAULT` proxy will be used for all connections
routeByHost: async ({ host }) => {
if (['pagead2.googlesyndication.com', 'fonts.gstatic.com'].includes(host)) {
return 'ABORT' // block connection to certain hosts
}
if (host.includes('google')) {
return 'DIRECT' // use a direct connection for all google domains
}
if (host.endsWith('.tile.openstreetmap.org')) {
return 'DATACENTER' // route heavy images through datacenter proxy
}
if (host === 'canhazip.com') {
return 'RESIDENTIAL_US' // special proxy for this domain
}
// everything else will use `DEFAULT` proxy
},
})
// Add the plugin
chromium.use(proxyRouter)
// Launch a browser and run some IP checks
chromium.launch({ headless: true }).then(async (browser) => {
const page = await browser.newPage()
await page.goto('https://showmyip.com/', { waitUntil: 'domcontentloaded' })
const ip1 = await page.evaluate("document.querySelector('#ipv4').innerText")
console.log('Outbound IP #1:', ip1)
// => 77.191.128.0 (the DEFAULT proxy)
await page.goto('https://canhazip.com', { waitUntil: 'domcontentloaded' })
const ip2 = await page.evaluate('document.body.innerText')
console.log('Outbound IP #2:', ip2)
// => 104.179.129.27 (the RESIDENTIAL_US proxy)
console.log(proxyRouter.stats.connectionLog) // list of connections (host => proxy name)
// { id: 0, proxy: 'DIRECT', host: 'accounts.google.com' },
// { id: 1, proxy: 'DEFAULT', host: 'www.showmyip.com' },
// { id: 2, proxy: 'ABORT', host: 'pagead2.googlesyndication.com' },
// { id: 3, proxy: 'DEFAULT', host: 'unpkg.com' },
// ...
console.log(proxyRouter.stats.byProxy) // bytes used by proxy
// {
// DATACENTER: 441734,
// DEFAULT: 125823,
// DIRECT: 100457,
// RESIDENTIAL_US: 4764,
// ABORT: 0
// }
console.log(proxyRouter.stats.byHost) // bytes used by host
// {
// 'a.tile.openstreetmap.org': 150685,
// 'c.tile.openstreetmap.org': 147054,
// 'b.tile.openstreetmap.org': 143995,
// 'unpkg.com': 57621,
// 'www.googletagmanager.com': 49572,
// 'www.showmyip.com': 40408,
// ...
await browser.close()
})
```
--------------------------------
### API: Using repl.repl() standalone
Source: https://github.com/berstend/puppeteer-extra/blob/master/packages/puppeteer-extra-plugin-repl/readme.md
Example of using the `repl.repl()` method directly to create an interactive REPL for a given object. This is useful when the plugin's methods are not automatically attached to Puppeteer instances.
```javascript
const repl = require('puppeteer-extra-plugin-repl')()
await repl.repl(