nodejs_chromium 1.1.22 → 1.1.25

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 +44 -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)
@@ -54,14 +66,18 @@ async function newBrowser(params) {
54
66
  incognito = true, //使用无痕模式启动
55
67
  path = void 0, //Chrome路径,linux下必填
56
68
  slowMo = 1, //每一步停留时间
69
+ clean = false, //启动前清空上次会话
57
70
  env = void 0, //string,指定浏览器可见的环境变量。process.env 的内容。
58
71
  proxy = null, //代理,如:https://proxy.com:789/
59
72
  wait = true, //是否等待初始页面准备好。当用户明确禁用该功能时很有用(例如 Chrome 的 --no-startup-window)
60
73
  timeout = 0, //等待浏览器启动的最长时间(以毫秒为单位)。 0 禁用超时。
61
74
  } = params;
62
75
 
76
+ const userDataDir = `runtime/.cache/${id}`;
77
+ if (clean) await NormalClose(userDataDir); // 在启动前修改,禁止显示【要恢复页面吗?chromium 未正确关闭。】
78
+
63
79
  let option = {
64
- userDataDir: `runtime/.cache/${id}`,
80
+ userDataDir,
65
81
  timeout, //最大允许超时间,默认为3000,最小215,一般不要设置
66
82
  slowMo, //每一步停留时间,不能太大,否则会太慢,特别是在类似写入很多Cookies时,每写入一个都要等一下。
67
83
  headless: visible ? false : 'new', //'new',设置是否在无头模式下运行浏览器,false=会启动浏览器,true=无界面
@@ -100,6 +116,11 @@ async function newBrowser(params) {
100
116
  '--lang=zh-CN', //设置中文环境
101
117
  '--disable-extensions', //禁止启动扩展
102
118
  '--disable-dev-shm-usage', //Linux系统中使用普通的文件系统缓存避免因为/dev/shm大小不足而导致的问题
119
+
120
+ '--no-zygote', //禁用 Zygote 进程,减少进程创建开销
121
+ '--single-process', // 单进程运行(适合简单任务,减少进程通信开销)
122
+ '--disable-images', // 非必要时禁用图片加载(大幅提速)
123
+
103
124
  ]
104
125
  }
105
126
 
@@ -113,11 +134,7 @@ async function newBrowser(params) {
113
134
  if (!cache) option.args.push('--disable-cache'); //禁缓存
114
135
  if (path) option.executablePath = path; //指定chrome安装路径
115
136
 
116
- const browser = await puppeteer.launch(option);
117
-
118
- // await NormalClose(option.userDataDir); //禁止显示【要恢复页面吗?chromium 未正确关闭。】
119
-
120
- return browser;
137
+ return await puppeteer.launch(option);
121
138
  }
122
139
  catch (e) {
123
140
  let mt = e.message.match(/\(ver\. (\d+\.\d+\.\d+\.\d+)\)/);
@@ -141,25 +158,33 @@ async function newChrome(params) {
141
158
  cookies = null, //若cookies=false,则不处理cookies,不指定则由chrome处理,若=文件路径
142
159
  abort = [], //要禁止的类型,常见["font","image","ping","stylesheet","document","fetch","script","xhr"]
143
160
  headers = {},
144
- device = null
161
+ index = 0, //同一浏览器的第几个窗口
162
+ interception = false, //允许拦截
163
+ device = null //设备类型
145
164
  } = params;
146
165
 
147
- const browser = await newBrowser(params);
148
- if (browser.error) {
149
- console.log({ browser })
166
+ if (!browser) {
167
+ browser = await newBrowser(params);
168
+ if (browser.error) {
169
+ console.log({ browser })
170
+ }
150
171
  }
151
172
 
152
- // const version = await browser.version();
153
- const page = (await browser.pages())[0];
154
- // const page = await browser.newPage();
155
- // await page.setRequestInterception(true); //允许拦截
173
+ const page = (index === 0) ? (await browser.pages())[0] : await browser.newPage();
174
+
175
+ if (interception) await page.setRequestInterception(true); //允许拦截
176
+ const version = await browser.version();
156
177
 
157
178
  if (ua) await page.setUserAgent(ua);
158
179
  else if (mobile) {
159
180
  await page.emulate(device || KnownDevices['iPhone 15 Pro']);
160
181
  }
161
- else await page.setUserAgent(__UA__);
162
- if (device) await page.emulate(device); //必须要放在setUserAgent后面
182
+ else {
183
+ await page.setUserAgent(__UA__.sprintf(version));
184
+ }
185
+ if (device) {
186
+ await page.emulate(device); //必须要放在setUserAgent后面
187
+ }
163
188
 
164
189
  await page.evaluateOnNewDocument(() => {
165
190
  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.25",
4
4
  "type": "module",
5
5
  "description": "for pupeteer chromium",
6
6
  "main": "index.js",