-
-
Notifications
You must be signed in to change notification settings - Fork 731
/
readme.md
142 lines (95 loc) · 4.46 KB
/
readme.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# puppeteer-extra-plugin-repl
> A plugin for [puppeteer-extra](https://github.com/berstend/puppeteer-extra).
## Installation
```bash
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](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) in your console.
- Adds convenience `.repl()` methods to `Page` and `Browser` 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](https://github.com/nswbmw/puppeteer-debug) from [nswbmw](https://github.com/nswbmw), thanks!
## REPL
![repl](https://i.imgur.com/xeP7hEc.gif)
## Quickstart
```es6
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):
```es6
> 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
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
#### Table of Contents
- [Plugin](#plugin)
- [repl](#repl)
### [Plugin](https://github.com/berstend/puppeteer-extra/blob/db57ea66cf10d407cf63af387892492e495a84f2/packages/puppeteer-extra-plugin-repl/index.js#L38-L83)
**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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Options (optional, default `{}`)
- `opts.addToPuppeteerClass` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** If a `.repl()` method should be attached to Puppeteer `Page` and `Browser` instances (default: true).
Example:
```javascript
// 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](https://github.com/berstend/puppeteer-extra/blob/db57ea66cf10d407cf63af387892492e495a84f2/packages/puppeteer-extra-plugin-repl/index.js#L70-L70)
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](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object or class instance to use in the repl (e.g. `page`, `browser`)
Example:
```javascript
const repl = require('puppeteer-extra-plugin-repl')()
await repl.repl(<object or class instance to inspect>)
```
* * *