Skip to content

Commit 9473cc4

Browse files
committed
Puppeteer extra plugin installation support added
1 parent 0e523f8 commit 9473cc4

File tree

9 files changed

+174
-34
lines changed

9 files changed

+174
-34
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,42 @@ async function test() {
117117

118118
**disableXvfb:** In Linux, when headless is false, a virtual screen is created and the browser is run there. You can set this value to true if you want to see the browser.
119119

120+
## How to Install Puppeteer-extra Plugins?
121+
Some plugins, such as puppeteer-extra-plugin-anonymize-ua, may cause you to be detected. You can use the plugin installation test in the library's test file to see if it will cause you to be detected.
122+
123+
The following is an example of installing a plugin. You can install other plugins in the same way as this example.
124+
125+
```bash
126+
npm i puppeteer-extra-plugin-click-and-wait
127+
```
128+
129+
```js
130+
131+
const test = require('node:test');
132+
const assert = require('node:assert');
133+
const { connect } = require('puppeteer-real-browser');
134+
135+
test('Puppeteer Extra Plugin', async () => {
136+
const { page, browser } = await connect({
137+
args: ["--start-maximized"],
138+
turnstile: true,
139+
headless: false,
140+
// disableXvfb: true,
141+
customConfig: {},
142+
connectOption: {
143+
defaultViewport: null
144+
},
145+
plugins: [
146+
require('puppeteer-extra-plugin-click-and-wait')()
147+
]
148+
})
149+
await page.goto("https://google.com", { waitUntil: "domcontentloaded" })
150+
await page.clickAndWaitForNavigation('body')
151+
await browser.close()
152+
})
153+
154+
```
155+
120156
## Docker
121157

122158
You can use the Dockerfile file in the main directory to use this library with docker. It has been tested with docker on Ubuntu server operating systems.

lib/cjs/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const puppeteer = require('puppeteer-core-patch')
1+
var puppeteer = require('puppeteer-core-patch')
22
const { pageController } = require('./module/pageController.js')
33
const Xvfb = require('xvfb');
44

55
process.env.REBROWSER_PATCHES_RUNTIME_FIX_MODE = "alwaysIsolated"
66
// process.env.REBROWSER_PATCHES_DEBUG = 1
7-
async function connect({ args = [], headless = false, customConfig = {}, proxy = {}, turnstile = false, connectOption = {}, disableXvfb = false }) {
7+
async function connect({ args = [], headless = false, customConfig = {}, proxy = {}, turnstile = false, connectOption = {}, disableXvfb = false, plugins = [] }) {
88
const { launch, Launcher } = await import('chrome-launcher');
99

1010
let xvfbsession = null
@@ -33,14 +33,24 @@ async function connect({ args = [], headless = false, customConfig = {}, proxy =
3333
...customConfig
3434
});
3535

36+
if (plugins.length > 0) {
37+
const { addExtra } = await import('puppeteer-extra');
38+
39+
puppeteer = addExtra(puppeteer);
40+
41+
for (const item of plugins) {
42+
puppeteer.use(item);
43+
}
44+
}
45+
3646
const browser = await puppeteer.connect({
3747
browserURL: `http://127.0.0.1:${chrome.port}`,
3848
...connectOption
3949
});
4050

4151
let [page] = await browser.pages();
4252

43-
let pageControllerConfig = { browser, page, proxy, turnstile, xvfbsession, pid: chrome.pid }
53+
let pageControllerConfig = { browser, page, proxy, turnstile, xvfbsession, pid: chrome.pid, plugins }
4454

4555
page = await pageController(pageControllerConfig);
4656

lib/cjs/module/pageController.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ function getRandomInt(min, max) {
88
return Math.floor(Math.random() * (max - min + 1)) + min;
99
}
1010

11-
async function pageController({ browser, page, proxy, turnstile, xvfbsession, pid }) {
11+
async function pageController({ browser, page, proxy, turnstile, xvfbsession, pid, plugins }) {
12+
13+
if (plugins.length > 0) {
14+
for (const plugin of plugins) {
15+
plugin.onPageCreated(page)
16+
}
17+
}
18+
1219
let solveStatus = turnstile
1320

1421
page.on('close', () => {

lib/esm/index.mjs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Xvfb from 'xvfb';
55

66
process.env.REBROWSER_PATCHES_RUNTIME_FIX_MODE = "alwaysIsolated"
77
// process.env.REBROWSER_PATCHES_DEBUG=1
8-
export async function connect({ args = [], headless = false, customConfig = {}, proxy = {}, turnstile = false, connectOption = {}, disableXvfb = false }) {
8+
export async function connect({ args = [], headless = false, customConfig = {}, proxy = {}, turnstile = false, connectOption = {}, disableXvfb = false, plugins = [] }) {
99
let xvfbsession = null
1010
if (headless == 'auto') headless = false
1111

@@ -31,15 +31,25 @@ export async function connect({ args = [], headless = false, customConfig = {},
3131
],
3232
...customConfig
3333
});
34+
let pextra = null
35+
if (plugins.length > 0) {
36+
const { addExtra } = await import('puppeteer-extra');
3437

35-
const browser = await puppeteer.connect({
38+
pextra = addExtra(puppeteer);
39+
40+
for (const item of plugins) {
41+
pextra.use(item);
42+
}
43+
}
44+
45+
const browser = await (pextra ? pextra : puppeteer).connect({
3646
browserURL: `http://127.0.0.1:${chrome.port}`,
3747
...connectOption
3848
});
3949

4050
let [page] = await browser.pages();
4151

42-
let pageControllerConfig = { browser, page, proxy, turnstile, xvfbsession, pid: chrome.pid }
52+
let pageControllerConfig = { browser, page, proxy, turnstile, xvfbsession, pid: chrome.pid, plugins }
4353

4454
page = await pageController(pageControllerConfig);
4555

lib/esm/module/pageController.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ function getRandomInt(min, max) {
88
return Math.floor(Math.random() * (max - min + 1)) + min;
99
}
1010

11-
export async function pageController({ browser, page, proxy, turnstile, xvfbsession, pid }) {
11+
export async function pageController({ browser, page, proxy, turnstile, xvfbsession, pid, plugins }) {
12+
13+
if (plugins.length > 0) {
14+
for (const plugin of plugins) {
15+
plugin.onPageCreated(page)
16+
}
17+
}
18+
1219
let solveStatus = turnstile
1320

1421
page.on('close', () => {

package-lock.json

Lines changed: 57 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "puppeteer-real-browser",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "This package is designed to bypass puppeteer's bot-detecting captchas such as Cloudflare. It acts like a real browser and can be managed with puppeteer.",
55
"main": "lib/cjs/index.js",
66
"module": "lib/esm/index.mjs",
@@ -39,6 +39,7 @@
3939
"chrome-launcher": "^1.1.2",
4040
"ghost-cursor": "^1.3.0",
4141
"puppeteer-core-patch": "^1.0.1",
42+
"puppeteer-extra": "^3.3.6",
4243
"tree-kill": "^1.2.2",
4344
"xvfb": "^0.4.0"
4445
},

test/cjs/test.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@ const realBrowserOption = {
88
turnstile: true,
99
headless: false,
1010
// disableXvfb: true,
11-
customConfig: {
12-
},
11+
customConfig: {},
1312
connectOption: {
1413
defaultViewport: null
15-
}
14+
},
15+
plugins: []
1616
}
1717

18+
// test('Puppeteer Extra Plugin', async () => {
19+
// /*
20+
// Run with:
21+
// npm i puppeteer-extra-plugin-click-and-wait
22+
// */
23+
// realBrowserOption.plugins = [
24+
// require('puppeteer-extra-plugin-click-and-wait')()
25+
// ]
26+
// const { page, browser } = await connect(realBrowserOption)
27+
// await page.goto("https://google.com", { waitUntil: "domcontentloaded" })
28+
// await page.clickAndWaitForNavigation('body')
29+
// await browser.close()
30+
// })
31+
32+
1833
test('DrissionPage Detector', async () => {
1934
const { page, browser } = await connect(realBrowserOption)
2035
await page.goto("https://drissionpage.pages.dev/");
@@ -83,11 +98,3 @@ test('Recaptcha V3 Score (hard)', async () => {
8398
// if (Number(score) >= 0.7) console.log('Recaptcha V3 Score: ' + score);
8499
assert.strictEqual(Number(score) >= 0.7, true, "Recaptcha V3 Score (hard) should be >=0.7. Score Result: " + score)
85100
})
86-
87-
88-
89-
90-
91-
92-
93-

test/esm/test.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,29 @@ const realBrowserOption = {
77
turnstile: true,
88
headless: false,
99
// disableXvfb: true,
10-
customConfig: {
11-
},
10+
customConfig: {},
1211
connectOption: {
1312
defaultViewport: null
14-
}
13+
},
14+
plugins: []
1515
}
1616

1717

18+
// test('Puppeteer Extra Plugin', async () => {
19+
// /*
20+
// Run with:
21+
// npm i puppeteer-extra-plugin-click-and-wait
22+
// */
23+
// const clickAndWait = await (await import('puppeteer-extra-plugin-click-and-wait')).default
24+
// realBrowserOption.plugins = [
25+
// clickAndWait()
26+
// ]
27+
// const { page, browser } = await connect(realBrowserOption)
28+
// await page.goto("https://google.com", { waitUntil: "domcontentloaded" })
29+
// await page.clickAndWaitForNavigation('body')
30+
// await browser.close()
31+
// })
32+
1833
test('DrissionPage Detector', async () => {
1934
const { page, browser } = await connect(realBrowserOption)
2035
await page.goto("https://drissionpage.pages.dev/");
@@ -82,12 +97,4 @@ test('Recaptcha V3 Score (hard)', async () => {
8297
await browser.close()
8398
// if (Number(score) >= 0.7) console.log('Recaptcha V3 Score: ' + score);
8499
assert.strictEqual(Number(score) >= 0.7, true, "Recaptcha V3 Score (hard) should be >=0.7. Score Result: " + score)
85-
})
86-
87-
88-
89-
90-
91-
92-
93-
100+
})

0 commit comments

Comments
 (0)