nodejs_chromium 1.1.10 → 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 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs_chromium",
3
- "version": "1.1.10",
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();
@@ -214,7 +214,6 @@ module.exports = class {
214
214
  else {
215
215
  await this.page.goto(url, { timeout, referer, waitUntil });
216
216
  }
217
- await this.page.goto(url, { timeout, referer, waitUntil });
218
217
  return this;
219
218
  }
220
219
  catch (e) {
@@ -578,7 +577,7 @@ module.exports = class {
578
577
  * 补全所有本地js/css,一般用于保存html之前
579
578
  */
580
579
  async improveUrls() {
581
- const url = parseUrl(this.page.url());
580
+ const url = new URL(this.page.url());
582
581
  const domain = url.protocol + '//' + url.host;
583
582
  await this.page.evaluate((domain) => {
584
583
  try {
@@ -596,6 +595,17 @@ module.exports = class {
596
595
  }, domain);
597
596
  }
598
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
+ }
599
609
 
600
610
  async parseResponse(response) {
601
611
  // const response = await this.page.waitForResponse(res => res);
@@ -612,7 +622,7 @@ module.exports = class {
612
622
  if (value.type === 'xhr') value.type = 'AJAX';
613
623
  value.url = await response.url();
614
624
  value.headers = { request: request.headers(), response: headers };
615
- value.domain = parseUrl(value.url)['host'];
625
+ value.domain = (new URL(value.url))['host'];
616
626
  value.content = headers['content-type'];
617
627
  value.length = headers['content-length'];
618
628
  value.status = await response.status();
@@ -623,7 +633,11 @@ module.exports = class {
623
633
  if (headers['set-cookie']) value.cookies = await this.cookies.parse(headers['set-cookie']);
624
634
  value.remote = await response.remoteAddress(); //目标服务器
625
635
  if (value.status === 301 || value.status === 302) return value;
626
- 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
+
627
641
  if (value.content) {
628
642
  if (value.content.startsWith('application/vnd')) return value;
629
643
  if (value.content.startsWith('application/xml')) return value;
@@ -647,9 +661,8 @@ module.exports = class {
647
661
  }
648
662
 
649
663
  host(url) {
650
- // const domain = url.protocol + '//' + url.host;
651
- const urls = parseUrl(url);
652
- return '.' + urls.host.split('.').slice(-2).join('.');
664
+ const myUrl = new URL(url);
665
+ return '.' + myUrl.host.split('.').slice(-2).join('.');
653
666
  }
654
667
 
655
668
 
@@ -670,7 +683,7 @@ module.exports = class {
670
683
 
671
684
  if (abort.length > 0 && abort.some(t => t === rType)) return request.abort();
672
685
 
673
- if (optHead !== {}) Object.assign(headers, optHead);
686
+ if (JSON.stringify(optHead) !== '{}') Object.assign(headers, optHead);
674
687
 
675
688
  headers['Access-Control-Allow-Origin'] = '*'; // 设置允许跨源访问的域名,可以根据需求修改
676
689
  headers['Access-Control-Allow-Methods'] = '*'; //'GET, POST, PUT, OPTIONS';
@@ -680,8 +693,7 @@ module.exports = class {
680
693
 
681
694
  this.page.on('response', async res => {
682
695
  if (!this.responseCall) return;
683
- let json = await this.parseResponse(res);
684
- await this.responseCall(json);
696
+ await this.responseCall(await this.parseResponse(res));
685
697
  });
686
698
 
687
699
  }