nodejs_chromium 1.1.11 → 1.1.13

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/package.json +1 -1
  2. package/src/chrome.js +25 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs_chromium",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "for pupeteer chromium",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/chrome.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const fs = require("fs");
2
2
  const Cookies = require("./cookies");
3
- const { parse: parseUrl } = require("url");
3
+ const { URL } = require('url');
4
4
 
5
5
 
6
6
  /**
@@ -53,7 +53,7 @@ module.exports = class {
53
53
  await page.evaluateOnNewDocument(() => {
54
54
  const newProto = navigator.__proto__;
55
55
  delete newProto.webdriver; //删除 navigator.webdriver字段
56
- navigator.__proto__ = newProto; //在每次新文档加载时,删除 navigator.webdriver 字段,这有助于防止某些网站检测到自动化行为。
56
+ navigator.__proto__ = newProto; //删除 navigator.webdriver 字段,有助于防止某些网站检测到自动化行为
57
57
  });
58
58
  return new module.exports(this.browser, page, this.options, false); //new 自身
59
59
  }
@@ -152,7 +152,7 @@ module.exports = class {
152
152
  /**
153
153
  * 关闭
154
154
  */
155
- async close(act = 0) {
155
+ async close(act = 1) {
156
156
  try {
157
157
  if (act & 1) await this.page.close();
158
158
  if (act & 2) await this.browser.close();
@@ -577,7 +577,7 @@ module.exports = class {
577
577
  * 补全所有本地js/css,一般用于保存html之前
578
578
  */
579
579
  async improveUrls() {
580
- const url = parseUrl(this.page.url());
580
+ const url = new URL(this.page.url());
581
581
  const domain = url.protocol + '//' + url.host;
582
582
  await this.page.evaluate((domain) => {
583
583
  try {
@@ -595,6 +595,17 @@ module.exports = class {
595
595
  }, domain);
596
596
  }
597
597
 
598
+ skipTypes = ['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping'];
599
+ setSkipType(items) {
600
+ this.skipTypes.length = 0;
601
+ this.skipTypes = [...items];
602
+ return this;
603
+ }
604
+
605
+ delSkipType(...item) {
606
+ this.skipTypes = this.skipTypes.del(...item);
607
+ return this;
608
+ }
598
609
 
599
610
  async parseResponse(response) {
600
611
  // const response = await this.page.waitForResponse(res => res);
@@ -611,7 +622,7 @@ module.exports = class {
611
622
  if (value.type === 'xhr') value.type = 'AJAX';
612
623
  value.url = await response.url();
613
624
  value.headers = { request: request.headers(), response: headers };
614
- value.domain = parseUrl(value.url)['host'];
625
+ value.domain = (new URL(value.url))['host'];
615
626
  value.content = headers['content-type'];
616
627
  value.length = headers['content-length'];
617
628
  value.status = await response.status();
@@ -622,7 +633,11 @@ module.exports = class {
622
633
  if (headers['set-cookie']) value.cookies = await this.cookies.parse(headers['set-cookie']);
623
634
  value.remote = await response.remoteAddress(); //目标服务器
624
635
  if (value.status === 301 || value.status === 302) return value;
625
- if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping'].has(value.type)) return value;
636
+
637
+ // if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping'].has(value.type)) return value;
638
+ // if (['image', 'font', 'other', 'script', 'stylesheet', 'ping'].includes(value.type)) return value;
639
+ if (this.skipTypes.includes(value.type)) return value;
640
+
626
641
  if (value.content) {
627
642
  if (value.content.startsWith('application/vnd')) return value;
628
643
  if (value.content.startsWith('application/xml')) return value;
@@ -646,9 +661,8 @@ module.exports = class {
646
661
  }
647
662
 
648
663
  host(url) {
649
- // const domain = url.protocol + '//' + url.host;
650
- const urls = parseUrl(url);
651
- return '.' + urls.host.split('.').slice(-2).join('.');
664
+ const myUrl = new URL(url);
665
+ return '.' + myUrl.host.split('.').slice(-2).join('.');
652
666
  }
653
667
 
654
668
 
@@ -669,7 +683,7 @@ module.exports = class {
669
683
 
670
684
  if (abort.length > 0 && abort.some(t => t === rType)) return request.abort();
671
685
 
672
- if (optHead !== {}) Object.assign(headers, optHead);
686
+ if (JSON.stringify(optHead) !== '{}') Object.assign(headers, optHead);
673
687
 
674
688
  headers['Access-Control-Allow-Origin'] = '*'; // 设置允许跨源访问的域名,可以根据需求修改
675
689
  headers['Access-Control-Allow-Methods'] = '*'; //'GET, POST, PUT, OPTIONS';
@@ -679,8 +693,7 @@ module.exports = class {
679
693
 
680
694
  this.page.on('response', async res => {
681
695
  if (!this.responseCall) return;
682
- let json = await this.parseResponse(res);
683
- await this.responseCall(json);
696
+ await this.responseCall(await this.parseResponse(res));
684
697
  });
685
698
 
686
699
  }