nodejs_chromium 1.1.11 → 1.1.15
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.
- package/package.json +1 -1
- package/src/chrome.js +66 -15
package/package.json
CHANGED
package/src/chrome.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
const Cookies = require("./cookies");
|
|
3
|
-
const {
|
|
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;
|
|
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 =
|
|
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();
|
|
@@ -305,15 +305,18 @@ module.exports = class {
|
|
|
305
305
|
*/
|
|
306
306
|
async elements(tag1, tag2, call) {
|
|
307
307
|
try {
|
|
308
|
+
|
|
308
309
|
const div = await this.page.$(tag1);
|
|
309
310
|
if (!div) {
|
|
310
311
|
throw new Error(`${tag1} not exists`);
|
|
311
312
|
}
|
|
313
|
+
let index = 0;
|
|
312
314
|
for (const elm of (await div.$$(tag2))) {
|
|
313
315
|
const html = await elm.evaluate(node => node.outerHTML);
|
|
314
316
|
const text = await elm.evaluate(node => node.innerHTML);
|
|
315
|
-
call(elm, { html, text });
|
|
317
|
+
call(elm, { html, text, index: index++ });
|
|
316
318
|
}
|
|
319
|
+
|
|
317
320
|
}
|
|
318
321
|
catch (e) {
|
|
319
322
|
console.log('[chrome.elements.Error]', e.message)
|
|
@@ -321,6 +324,40 @@ module.exports = class {
|
|
|
321
324
|
}
|
|
322
325
|
|
|
323
326
|
|
|
327
|
+
/**
|
|
328
|
+
* 遍历tag1里的tag2,并由call判断,只有call返回true时才resolve
|
|
329
|
+
*
|
|
330
|
+
* @param {Object} tag1
|
|
331
|
+
* @param {Object} tag2
|
|
332
|
+
* @param {Object} call
|
|
333
|
+
*/
|
|
334
|
+
async search(tag1, tag2, call) {
|
|
335
|
+
return await new Promise(async (resolve) => {
|
|
336
|
+
try {
|
|
337
|
+
|
|
338
|
+
const div = await this.page.$(tag1);
|
|
339
|
+
if (!div) {
|
|
340
|
+
throw new Error(`${tag1} not exists`);
|
|
341
|
+
}
|
|
342
|
+
let index = 0;
|
|
343
|
+
for (const ele of (await div.$$(tag2))) {
|
|
344
|
+
index++;
|
|
345
|
+
const html = await ele.evaluate(node => node.outerHTML);
|
|
346
|
+
const text = await ele.evaluate(node => node.innerHTML);
|
|
347
|
+
let val = call(ele, { html, text, index });
|
|
348
|
+
if (val === true) return resolve({ success: true, ele, html, text, index });
|
|
349
|
+
}
|
|
350
|
+
resolve({ success: false });
|
|
351
|
+
|
|
352
|
+
}
|
|
353
|
+
catch (e) {
|
|
354
|
+
console.log('[chrome.elements.Error]', e.message)
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
|
|
324
361
|
/**
|
|
325
362
|
* 支持css普通选择器方式和伪类方式
|
|
326
363
|
* div.body
|
|
@@ -444,8 +481,8 @@ module.exports = class {
|
|
|
444
481
|
async click(el, option = {}) {
|
|
445
482
|
try {
|
|
446
483
|
let { delay = 100, count = 1, x = 6, y = 3 } = option;
|
|
447
|
-
await this.page.click(el, { delay, count, offset: { x, y } });
|
|
448
|
-
return this;
|
|
484
|
+
return await this.page.click(el, { delay, count, offset: { x, y } });
|
|
485
|
+
// return this;
|
|
449
486
|
}
|
|
450
487
|
catch (e) {
|
|
451
488
|
console.log('[chrome.click.Error]', e.message);
|
|
@@ -577,7 +614,7 @@ module.exports = class {
|
|
|
577
614
|
* 补全所有本地js/css,一般用于保存html之前
|
|
578
615
|
*/
|
|
579
616
|
async improveUrls() {
|
|
580
|
-
const url =
|
|
617
|
+
const url = new URL(this.page.url());
|
|
581
618
|
const domain = url.protocol + '//' + url.host;
|
|
582
619
|
await this.page.evaluate((domain) => {
|
|
583
620
|
try {
|
|
@@ -595,6 +632,18 @@ module.exports = class {
|
|
|
595
632
|
}, domain);
|
|
596
633
|
}
|
|
597
634
|
|
|
635
|
+
skipTypes = ['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping'];
|
|
636
|
+
|
|
637
|
+
setSkipType(items) {
|
|
638
|
+
this.skipTypes.length = 0;
|
|
639
|
+
this.skipTypes = [...items];
|
|
640
|
+
return this;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
delSkipType(...item) {
|
|
644
|
+
this.skipTypes = this.skipTypes.del(...item);
|
|
645
|
+
return this;
|
|
646
|
+
}
|
|
598
647
|
|
|
599
648
|
async parseResponse(response) {
|
|
600
649
|
// const response = await this.page.waitForResponse(res => res);
|
|
@@ -611,7 +660,7 @@ module.exports = class {
|
|
|
611
660
|
if (value.type === 'xhr') value.type = 'AJAX';
|
|
612
661
|
value.url = await response.url();
|
|
613
662
|
value.headers = { request: request.headers(), response: headers };
|
|
614
|
-
value.domain =
|
|
663
|
+
value.domain = (new URL(value.url))['host'];
|
|
615
664
|
value.content = headers['content-type'];
|
|
616
665
|
value.length = headers['content-length'];
|
|
617
666
|
value.status = await response.status();
|
|
@@ -622,7 +671,11 @@ module.exports = class {
|
|
|
622
671
|
if (headers['set-cookie']) value.cookies = await this.cookies.parse(headers['set-cookie']);
|
|
623
672
|
value.remote = await response.remoteAddress(); //目标服务器
|
|
624
673
|
if (value.status === 301 || value.status === 302) return value;
|
|
625
|
-
|
|
674
|
+
|
|
675
|
+
// if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping'].has(value.type)) return value;
|
|
676
|
+
// if (['image', 'font', 'other', 'script', 'stylesheet', 'ping'].includes(value.type)) return value;
|
|
677
|
+
if (this.skipTypes.includes(value.type)) return value;
|
|
678
|
+
|
|
626
679
|
if (value.content) {
|
|
627
680
|
if (value.content.startsWith('application/vnd')) return value;
|
|
628
681
|
if (value.content.startsWith('application/xml')) return value;
|
|
@@ -646,9 +699,8 @@ module.exports = class {
|
|
|
646
699
|
}
|
|
647
700
|
|
|
648
701
|
host(url) {
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
return '.' + urls.host.split('.').slice(-2).join('.');
|
|
702
|
+
const myUrl = new URL(url);
|
|
703
|
+
return '.' + myUrl.host.split('.').slice(-2).join('.');
|
|
652
704
|
}
|
|
653
705
|
|
|
654
706
|
|
|
@@ -669,7 +721,7 @@ module.exports = class {
|
|
|
669
721
|
|
|
670
722
|
if (abort.length > 0 && abort.some(t => t === rType)) return request.abort();
|
|
671
723
|
|
|
672
|
-
if (optHead !== {}) Object.assign(headers, optHead);
|
|
724
|
+
if (JSON.stringify(optHead) !== '{}') Object.assign(headers, optHead);
|
|
673
725
|
|
|
674
726
|
headers['Access-Control-Allow-Origin'] = '*'; // 设置允许跨源访问的域名,可以根据需求修改
|
|
675
727
|
headers['Access-Control-Allow-Methods'] = '*'; //'GET, POST, PUT, OPTIONS';
|
|
@@ -679,8 +731,7 @@ module.exports = class {
|
|
|
679
731
|
|
|
680
732
|
this.page.on('response', async res => {
|
|
681
733
|
if (!this.responseCall) return;
|
|
682
|
-
|
|
683
|
-
await this.responseCall(json);
|
|
734
|
+
await this.responseCall(await this.parseResponse(res));
|
|
684
735
|
});
|
|
685
736
|
|
|
686
737
|
}
|