nodejs_chromium 1.1.21 → 1.1.23
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 +44 -23
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -10,10 +10,8 @@ import chrome from "./src/chrome.js";
|
|
|
10
10
|
// const chrome = require("./src/chrome.js");
|
|
11
11
|
// const { exec } = require("child_process");
|
|
12
12
|
|
|
13
|
-
global.__UA__ = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
|
|
14
|
-
|
|
15
|
-
// const fs = require("fs");
|
|
16
|
-
|
|
13
|
+
global.__UA__ = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) %s Safari/537.36';
|
|
14
|
+
let browser;
|
|
17
15
|
|
|
18
16
|
async function NormalClose(path) {
|
|
19
17
|
/**
|
|
@@ -30,14 +28,26 @@ async function NormalClose(path) {
|
|
|
30
28
|
}
|
|
31
29
|
let text = fs.readFileSync(file);
|
|
32
30
|
let json = JSON.parse(text);
|
|
31
|
+
|
|
33
32
|
json.credentials_enable_service = false; //新增项,禁止保存密码
|
|
34
33
|
json.profile.exit_type = 'Normal'; //原值可能为:Crashed
|
|
34
|
+
if (0) {
|
|
35
|
+
// 添加防止恢复会话的配置
|
|
36
|
+
if (!json.session) json.session = {};
|
|
37
|
+
json.session.restore_on_startup = 0; // 0=不恢复,1=恢复上次会话,4=恢复特定会话
|
|
38
|
+
if (!json.profile) json.profile = {};
|
|
39
|
+
json.profile.exit_type = 'Normal';
|
|
40
|
+
json.profile.using_default_theme = 2; // 使用系统主题,避免恢复页面
|
|
41
|
+
// 禁用密码保存提示
|
|
42
|
+
if (!json.password_manager) json.password_manager = {};
|
|
43
|
+
json.password_manager.enabled = false;
|
|
44
|
+
}
|
|
45
|
+
|
|
35
46
|
// console.log('json.profile.exit_type', json.profile.exit_type);
|
|
36
47
|
fs.writeFileSync(file, JSON.stringify(json));
|
|
37
48
|
console.log(file, json.profile.exit_type)
|
|
38
49
|
}
|
|
39
50
|
|
|
40
|
-
|
|
41
51
|
async function newBrowser(params) {
|
|
42
52
|
try {
|
|
43
53
|
|
|
@@ -46,6 +56,7 @@ async function newBrowser(params) {
|
|
|
46
56
|
visible = false, //是否打开浏览器
|
|
47
57
|
width = 1024, //宽
|
|
48
58
|
height = 768, //高
|
|
59
|
+
maximized = 0, //最大化窗口,如果最大化,则宽高无效
|
|
49
60
|
scale = 1, //缩放比例
|
|
50
61
|
touch = false, //支持触摸
|
|
51
62
|
mobile = false, //手机版
|
|
@@ -61,8 +72,11 @@ async function newBrowser(params) {
|
|
|
61
72
|
timeout = 0, //等待浏览器启动的最长时间(以毫秒为单位)。 0 禁用超时。
|
|
62
73
|
} = params;
|
|
63
74
|
|
|
75
|
+
const userDataDir = `runtime/.cache/${id}`;
|
|
76
|
+
await NormalClose(userDataDir); // 在启动前修改,禁止显示【要恢复页面吗?chromium 未正确关闭。】
|
|
77
|
+
|
|
64
78
|
let option = {
|
|
65
|
-
userDataDir
|
|
79
|
+
userDataDir,
|
|
66
80
|
timeout, //最大允许超时间,默认为3000,最小215,一般不要设置
|
|
67
81
|
slowMo, //每一步停留时间,不能太大,否则会太慢,特别是在类似写入很多Cookies时,每写入一个都要等一下。
|
|
68
82
|
headless: visible ? false : 'new', //'new',设置是否在无头模式下运行浏览器,false=会启动浏览器,true=无界面
|
|
@@ -104,16 +118,17 @@ async function newBrowser(params) {
|
|
|
104
118
|
]
|
|
105
119
|
}
|
|
106
120
|
|
|
121
|
+
if (maximized) {
|
|
122
|
+
option.defaultViewport = null;
|
|
123
|
+
option.args.push('--start-maximized');
|
|
124
|
+
}
|
|
125
|
+
|
|
107
126
|
if (proxy) option.args.push(`--proxy-server=${proxy}`);
|
|
108
127
|
if (incognito) option.args.push('--incognito', '--disable-infobars'); //使用无痕模式启动
|
|
109
128
|
if (!cache) option.args.push('--disable-cache'); //禁缓存
|
|
110
129
|
if (path) option.executablePath = path; //指定chrome安装路径
|
|
111
130
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
// await NormalClose(option.userDataDir); //禁止显示【要恢复页面吗?chromium 未正确关闭。】
|
|
115
|
-
|
|
116
|
-
return browser;
|
|
131
|
+
return await puppeteer.launch(option);
|
|
117
132
|
}
|
|
118
133
|
catch (e) {
|
|
119
134
|
let mt = e.message.match(/\(ver\. (\d+\.\d+\.\d+\.\d+)\)/);
|
|
@@ -127,7 +142,6 @@ async function newBrowser(params) {
|
|
|
127
142
|
}
|
|
128
143
|
}
|
|
129
144
|
|
|
130
|
-
|
|
131
145
|
async function newChrome(params) {
|
|
132
146
|
try {
|
|
133
147
|
let {
|
|
@@ -138,25 +152,33 @@ async function newChrome(params) {
|
|
|
138
152
|
cookies = null, //若cookies=false,则不处理cookies,不指定则由chrome处理,若=文件路径
|
|
139
153
|
abort = [], //要禁止的类型,常见["font","image","ping","stylesheet","document","fetch","script","xhr"]
|
|
140
154
|
headers = {},
|
|
141
|
-
|
|
155
|
+
index = 0, //同一浏览器的第几个窗口
|
|
156
|
+
interception = false, //允许拦截
|
|
157
|
+
device = null //设备类型
|
|
142
158
|
} = params;
|
|
143
159
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
160
|
+
if (!browser) {
|
|
161
|
+
browser = await newBrowser(params);
|
|
162
|
+
if (browser.error) {
|
|
163
|
+
console.log({ browser })
|
|
164
|
+
}
|
|
147
165
|
}
|
|
148
166
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
167
|
+
const page = (index === 0) ? (await browser.pages())[0] : await browser.newPage();
|
|
168
|
+
|
|
169
|
+
if (interception) await page.setRequestInterception(true); //允许拦截
|
|
170
|
+
const version = await browser.version();
|
|
153
171
|
|
|
154
172
|
if (ua) await page.setUserAgent(ua);
|
|
155
173
|
else if (mobile) {
|
|
156
174
|
await page.emulate(device || KnownDevices['iPhone 15 Pro']);
|
|
157
175
|
}
|
|
158
|
-
else
|
|
159
|
-
|
|
176
|
+
else {
|
|
177
|
+
await page.setUserAgent(__UA__.sprintf(version));
|
|
178
|
+
}
|
|
179
|
+
if (device) {
|
|
180
|
+
await page.emulate(device); //必须要放在setUserAgent后面
|
|
181
|
+
}
|
|
160
182
|
|
|
161
183
|
await page.evaluateOnNewDocument(() => {
|
|
162
184
|
const newProto = navigator.__proto__;
|
|
@@ -207,5 +229,4 @@ async function checkInstall(install = false) {
|
|
|
207
229
|
}
|
|
208
230
|
}
|
|
209
231
|
|
|
210
|
-
|
|
211
232
|
export { newChrome, checkInstall }
|