nodejs_chromium 1.1.22 → 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.
Files changed (2) hide show
  1. package/index.js +38 -19
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -10,9 +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) Chrome/122.0.0.0 Safari/537.36';
14
- const mob_ua = 'Mozilla/5.0 (Linux; Android 13;Build/TKQ1.220905.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/116.0.0.0 Mobile Safari/537.36';
15
- // const fs = require("fs");
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;
16
15
 
17
16
  async function NormalClose(path) {
18
17
  /**
@@ -29,8 +28,21 @@ async function NormalClose(path) {
29
28
  }
30
29
  let text = fs.readFileSync(file);
31
30
  let json = JSON.parse(text);
31
+
32
32
  json.credentials_enable_service = false; //新增项,禁止保存密码
33
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
+
34
46
  // console.log('json.profile.exit_type', json.profile.exit_type);
35
47
  fs.writeFileSync(file, JSON.stringify(json));
36
48
  console.log(file, json.profile.exit_type)
@@ -60,8 +72,11 @@ async function newBrowser(params) {
60
72
  timeout = 0, //等待浏览器启动的最长时间(以毫秒为单位)。 0 禁用超时。
61
73
  } = params;
62
74
 
75
+ const userDataDir = `runtime/.cache/${id}`;
76
+ await NormalClose(userDataDir); // 在启动前修改,禁止显示【要恢复页面吗?chromium 未正确关闭。】
77
+
63
78
  let option = {
64
- userDataDir: `runtime/.cache/${id}`,
79
+ userDataDir,
65
80
  timeout, //最大允许超时间,默认为3000,最小215,一般不要设置
66
81
  slowMo, //每一步停留时间,不能太大,否则会太慢,特别是在类似写入很多Cookies时,每写入一个都要等一下。
67
82
  headless: visible ? false : 'new', //'new',设置是否在无头模式下运行浏览器,false=会启动浏览器,true=无界面
@@ -113,11 +128,7 @@ async function newBrowser(params) {
113
128
  if (!cache) option.args.push('--disable-cache'); //禁缓存
114
129
  if (path) option.executablePath = path; //指定chrome安装路径
115
130
 
116
- const browser = await puppeteer.launch(option);
117
-
118
- // await NormalClose(option.userDataDir); //禁止显示【要恢复页面吗?chromium 未正确关闭。】
119
-
120
- return browser;
131
+ return await puppeteer.launch(option);
121
132
  }
122
133
  catch (e) {
123
134
  let mt = e.message.match(/\(ver\. (\d+\.\d+\.\d+\.\d+)\)/);
@@ -141,25 +152,33 @@ async function newChrome(params) {
141
152
  cookies = null, //若cookies=false,则不处理cookies,不指定则由chrome处理,若=文件路径
142
153
  abort = [], //要禁止的类型,常见["font","image","ping","stylesheet","document","fetch","script","xhr"]
143
154
  headers = {},
144
- device = null
155
+ index = 0, //同一浏览器的第几个窗口
156
+ interception = false, //允许拦截
157
+ device = null //设备类型
145
158
  } = params;
146
159
 
147
- const browser = await newBrowser(params);
148
- if (browser.error) {
149
- console.log({ browser })
160
+ if (!browser) {
161
+ browser = await newBrowser(params);
162
+ if (browser.error) {
163
+ console.log({ browser })
164
+ }
150
165
  }
151
166
 
152
- // const version = await browser.version();
153
- const page = (await browser.pages())[0];
154
- // const page = await browser.newPage();
155
- // await page.setRequestInterception(true); //允许拦截
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();
156
171
 
157
172
  if (ua) await page.setUserAgent(ua);
158
173
  else if (mobile) {
159
174
  await page.emulate(device || KnownDevices['iPhone 15 Pro']);
160
175
  }
161
- else await page.setUserAgent(__UA__);
162
- if (device) await page.emulate(device); //必须要放在setUserAgent后面
176
+ else {
177
+ await page.setUserAgent(__UA__.sprintf(version));
178
+ }
179
+ if (device) {
180
+ await page.emulate(device); //必须要放在setUserAgent后面
181
+ }
163
182
 
164
183
  await page.evaluateOnNewDocument(() => {
165
184
  const newProto = navigator.__proto__;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs_chromium",
3
- "version": "1.1.22",
3
+ "version": "1.1.23",
4
4
  "type": "module",
5
5
  "description": "for pupeteer chromium",
6
6
  "main": "index.js",