nodejs_chromium 1.1.23 → 1.1.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +21 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -66,14 +66,16 @@ async function newBrowser(params) {
|
|
|
66
66
|
incognito = true, //使用无痕模式启动
|
|
67
67
|
path = void 0, //Chrome路径,linux下必填
|
|
68
68
|
slowMo = 1, //每一步停留时间
|
|
69
|
+
clean = false, //启动前清空上次会话
|
|
69
70
|
env = void 0, //string,指定浏览器可见的环境变量。process.env 的内容。
|
|
70
71
|
proxy = null, //代理,如:https://proxy.com:789/
|
|
71
72
|
wait = true, //是否等待初始页面准备好。当用户明确禁用该功能时很有用(例如 Chrome 的 --no-startup-window)
|
|
72
73
|
timeout = 0, //等待浏览器启动的最长时间(以毫秒为单位)。 0 禁用超时。
|
|
74
|
+
protocolTimeout = 0, //CDP协议超时时间(毫秒),默认0使用Puppeteer内置默认值(180000)。如遇到ProtocolError超时可增大此值。
|
|
73
75
|
} = params;
|
|
74
76
|
|
|
75
77
|
const userDataDir = `runtime/.cache/${id}`;
|
|
76
|
-
await NormalClose(userDataDir); // 在启动前修改,禁止显示【要恢复页面吗?chromium 未正确关闭。】
|
|
78
|
+
if (clean) await NormalClose(userDataDir); // 在启动前修改,禁止显示【要恢复页面吗?chromium 未正确关闭。】
|
|
77
79
|
|
|
78
80
|
let option = {
|
|
79
81
|
userDataDir,
|
|
@@ -84,6 +86,7 @@ async function newBrowser(params) {
|
|
|
84
86
|
ignoreHTTPSErrors: true, //忽略 HTTPS 错误。屏蔽跳转不同域名的报错
|
|
85
87
|
dumpio, //是否将浏览器的标准输入/输出流(stdio)的内容输出到 Node.js 的 stdout 和 stderr。
|
|
86
88
|
env,
|
|
89
|
+
protocolTimeout,
|
|
87
90
|
waitForInitialPage: wait,
|
|
88
91
|
defaultViewport: {
|
|
89
92
|
width,
|
|
@@ -115,6 +118,11 @@ async function newBrowser(params) {
|
|
|
115
118
|
'--lang=zh-CN', //设置中文环境
|
|
116
119
|
'--disable-extensions', //禁止启动扩展
|
|
117
120
|
'--disable-dev-shm-usage', //Linux系统中使用普通的文件系统缓存避免因为/dev/shm大小不足而导致的问题
|
|
121
|
+
|
|
122
|
+
'--no-zygote', //禁用 Zygote 进程,减少进程创建开销
|
|
123
|
+
'--single-process', // 单进程运行(适合简单任务,减少进程通信开销)
|
|
124
|
+
'--disable-images', // 非必要时禁用图片加载(大幅提速)
|
|
125
|
+
|
|
118
126
|
]
|
|
119
127
|
}
|
|
120
128
|
|
|
@@ -144,7 +152,7 @@ async function newBrowser(params) {
|
|
|
144
152
|
|
|
145
153
|
async function newChrome(params) {
|
|
146
154
|
try {
|
|
147
|
-
|
|
155
|
+
const {
|
|
148
156
|
id = 'myChrome',
|
|
149
157
|
visible = false, //是否打开浏览器
|
|
150
158
|
mobile = false, //手机版
|
|
@@ -154,7 +162,8 @@ async function newChrome(params) {
|
|
|
154
162
|
headers = {},
|
|
155
163
|
index = 0, //同一浏览器的第几个窗口
|
|
156
164
|
interception = false, //允许拦截
|
|
157
|
-
device = null //设备类型
|
|
165
|
+
device = null, //设备类型
|
|
166
|
+
webdriver = null, //删除 navigator.webdriver字段
|
|
158
167
|
} = params;
|
|
159
168
|
|
|
160
169
|
if (!browser) {
|
|
@@ -180,14 +189,16 @@ async function newChrome(params) {
|
|
|
180
189
|
await page.emulate(device); //必须要放在setUserAgent后面
|
|
181
190
|
}
|
|
182
191
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
192
|
+
if (webdriver !== false) {
|
|
193
|
+
await page.evaluateOnNewDocument(() => {
|
|
194
|
+
const newProto = navigator.__proto__;
|
|
195
|
+
delete newProto.webdriver; //删除 navigator.webdriver字段,防止检测到自动化行为
|
|
196
|
+
navigator.__proto__ = newProto;
|
|
197
|
+
});
|
|
198
|
+
}
|
|
188
199
|
|
|
189
|
-
|
|
190
|
-
const pageOption = { cookies, visible, abort, headers, device };
|
|
200
|
+
const cook = (cookies === false) ? `runtime/.cache/${id}/cookies.json` : cookies;
|
|
201
|
+
const pageOption = { cookies: cook, visible, abort, headers, device };
|
|
191
202
|
return new chrome(browser, page, pageOption);
|
|
192
203
|
}
|
|
193
204
|
catch (err) {
|