puppeteer-extra-plugin-repl
v2.3.3
Published
Start an interactive REPL in your puppeteer code.
Downloads
2,836
Maintainers
Readme
puppeteer-extra-plugin-repl
A plugin for puppeteer-extra.
Installation
yarn add puppeteer-extra-plugin-repl
Purpose
Make quick puppeteer debugging and exploration fun with an interactive REPL.
- Can interrupt your code at anytime to start an interactive REPL in your console.
- Adds convenience
.repl()
methods toPage
andBrowser
instances. - Supports inspecting arbitrary objects and instances.
- Features tab auto-completion for the available object properties and a colorized prompt.
Kudos
- Inspired by puppeteer-debug from nswbmw, thanks!
REPL
Quickstart
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()
})
In the REPL session (hit tab
two times to see all available properties):
> page.url()
// => https://example.com
> page.click('a')
> page.url()
// => https://www.iana.org/domains/reserved
> page.content()
// => <!DOCTYPE html><html><head> ...
> 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
- Type
inspect
to return the current object. - Type
exit
(or hit ctrl+c) to leave the repl.
API
Table of Contents
Plugin
Extends: PuppeteerExtraPlugin
Interrupt your puppeteer code with an interactive REPL.
Features tab auto-completion for the given object properties and a colorized prompt.
Works with arbitrary objects ands class instances, though Page
& Browser
make the most sense. :-)
opts
Type: function (opts)
opts
Object Options (optional, default{}
)opts.addToPuppeteerClass
boolean? If a.repl()
method should be attached to PuppeteerPage
andBrowser
instances (default: true).
Example:
// In this example we don't extend the native puppeteer classes
const puppeteer = require('puppeteer-extra')
const repl = require('puppeteer-extra-plugin-repl')({ addToPuppeteerClass: false })
puppeteer.use(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 repl.repl(page)
// Afterwards start REPL with the `repl` instance itself. 🐴
await repl.repl(repl)
await browser.close()
})
repl
Create an interactive REPL for the provided object.
Uses an extended (colorized) readline interface under the hood. Will resolve the returned Promise when the readline interface is closed.
If opts.addToPuppeteerClass
is true (default) then page.repl()
/browser.repl()
will point to this method, for convenience.
Can be used standalone as well, to inspect an arbitrary class instance or object.
Type: function (obj): Promise
obj
Object An object or class instance to use in the repl (e.g.page
,browser
)
Example:
const repl = require('puppeteer-extra-plugin-repl')()
await repl.repl(<object or class instance to inspect>)