nodejs_chromium 1.1.5 → 1.1.7

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 (3) hide show
  1. package/index.js +29 -2
  2. package/package.json +1 -1
  3. package/src/chrome.js +21 -2
package/index.js CHANGED
@@ -6,6 +6,28 @@ global.__UA__ = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (K
6
6
  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';
7
7
  let browser;
8
8
 
9
+
10
+ async function NormalClose(path) {
11
+
12
+ const fs = require("fs");
13
+ /**
14
+ * 禁止显示【要恢复页面吗?chromium 未正确关闭。】
15
+ * PATH为chrome.userDataDir
16
+ * 修改`PATH\Default\Preferences`文件,
17
+ * 将`"profile.exit_type":“Crashed"` 改为`"Normal"`。
18
+ */
19
+ let file = `${path}/Default/Preferences`;
20
+ // console.log({file});
21
+ if (!fs.existsSync(file)) return;
22
+ let text = fs.readFileSync(file);
23
+ let json = JSON.parse(text);
24
+ json.credentials_enable_service = false; //新增项,禁止保存密码
25
+ json.profile.exit_type = 'Normal'; //原值可能为:Crashed
26
+ // console.log('json.profile.exit_type', json.profile.exit_type);
27
+ fs.writeFileSync(file, JSON.stringify(json));
28
+ }
29
+
30
+
9
31
  async function newBrowser(params) {
10
32
  try {
11
33
 
@@ -60,6 +82,7 @@ async function newBrowser(params) {
60
82
  '--disable-web-security', //禁用浏览器的同源策略(Same-Origin Policy)和跨站请求伪造(CSRF)保护
61
83
  `--window-size=${width},${height}`,
62
84
  '--no-sandbox', //禁用沙箱模式
85
+ '--disable-password-saving', //禁止保存密码
63
86
  '--disable-autofill-backend', //禁止自动填充
64
87
  '--disable-setuid-sandbox', //禁用 setuid 沙箱。这是另一种沙箱模式,通常用于 Linux 系统上。
65
88
  // '--disable-infobars', //禁用自动化控制时显示的信息栏,告诉用户浏览器正在被自动化工具控制。
@@ -77,6 +100,9 @@ async function newBrowser(params) {
77
100
  if (path) option.executablePath = path; //指定chrome安装路径
78
101
 
79
102
  browser = await puppeteer.launch(option);
103
+
104
+ await NormalClose(option.userDataDir); //禁止显示【要恢复页面吗?chromium 未正确关闭。】
105
+
80
106
  return browser;
81
107
  }
82
108
  catch (e) {
@@ -95,7 +121,8 @@ async function newBrowser(params) {
95
121
  async function newChrome(params) {
96
122
  try {
97
123
  let {
98
- visible = false, //是否打开浏览器
124
+ id = 'myChrome',
125
+ visible = false, //是否打开浏览器
99
126
  mobile = false, //手机版
100
127
  ua = void 0, //若不指定ua则用__UA__
101
128
  cookies = null, //若cookies=false,则不处理cookies,不指定则由chrome处理,若=文件路径
@@ -120,7 +147,7 @@ async function newChrome(params) {
120
147
  navigator.__proto__ = newProto;
121
148
  });
122
149
 
123
- if (cookies !== false) cookies = `${option.userDataDir}/cookies.json`;
150
+ if (cookies !== false) cookies = `runtime/.cache/${id}/cookies.json`;
124
151
  const pageOption = { cookies, visible, abort, headers };
125
152
  return new chrome(browser, page, pageOption);
126
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs_chromium",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "for pupeteer chromium",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/chrome.js CHANGED
@@ -399,12 +399,30 @@ module.exports = class {
399
399
  }
400
400
  }
401
401
 
402
+ /**
403
+ * delay=每键入一个字符延迟毫秒
404
+ */
405
+ async clear(el) {
406
+ try {
407
+ await this.page.$eval(el, ele => ele.value = '');
408
+ return this;
409
+ }
410
+ catch (e) {
411
+ console.log('[chrome.clear.Error]', e.message);
412
+ }
413
+ }
414
+
402
415
  /**
403
416
  * delay=每键入一个字符延迟毫秒
404
417
  */
405
418
  async input(el, value, delay = 1) {
406
419
  try {
407
- await this.page.type(el, value, { delay });
420
+ if (value === '') {
421
+ await this.page.$eval(el, ele => ele.value = '');
422
+ }
423
+ else {
424
+ await this.page.type(el, value, { delay });
425
+ }
408
426
  return this;
409
427
  }
410
428
  catch (e) {
@@ -588,6 +606,7 @@ module.exports = class {
588
606
 
589
607
  value.type = await request.resourceType();
590
608
  // value.redirect = await response.redirectURL();
609
+ if (value.type === 'fetch') value.type = 'xhr';
591
610
  if (value.type === 'xhr') value.type = 'AJAX';
592
611
  value.url = await response.url();
593
612
  value.headers = { request: request.headers(), response: headers };
@@ -602,7 +621,7 @@ module.exports = class {
602
621
  if (headers['set-cookie']) value.cookies = await this.cookies.parse(headers['set-cookie']);
603
622
  value.remote = await response.remoteAddress(); //目标服务器
604
623
  if (value.status === 301 || value.status === 302) return value;
605
- if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping', 'fetch'].has(value.type)) return value;
624
+ if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping'].has(value.type)) return value;
606
625
  if (value.content) {
607
626
  if (value.content.startsWith('application/vnd')) return value;
608
627
  if (value.content.startsWith('application/xml')) return value;