nodejs_chromium 1.0.5 → 1.0.6
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 +774 -732
package/src/chrome.js
CHANGED
|
@@ -1,733 +1,775 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const {parse: parseUrl} = require("url");
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
* class CdpFrame extends _classSuper
|
|
8
|
-
* \node_modules\nodejs_chromium\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\Frame.js
|
|
9
|
-
* \node_modules\nodejs_chromium\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\Page.js
|
|
10
|
-
* \node_modules\puppeteer-core\src\api\Page.ts
|
|
11
|
-
*
|
|
12
|
-
* @type {exports}
|
|
13
|
-
*/
|
|
14
|
-
module.exports = class {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const { parse: parseUrl } = require("url");
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* class CdpFrame extends _classSuper
|
|
8
|
+
* \node_modules\nodejs_chromium\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\Frame.js
|
|
9
|
+
* \node_modules\nodejs_chromium\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\Page.js
|
|
10
|
+
* \node_modules\puppeteer-core\src\api\Page.ts
|
|
11
|
+
*
|
|
12
|
+
* @type {exports}
|
|
13
|
+
*/
|
|
14
|
+
module.exports = class {
|
|
15
|
+
browser = null;
|
|
16
|
+
page = null;
|
|
17
|
+
responseCall = null;
|
|
18
|
+
requestCall = null;
|
|
19
|
+
isFrame = false;
|
|
20
|
+
visible = false;
|
|
21
|
+
params = {};
|
|
22
|
+
|
|
23
|
+
constructor(browser, page, params, isFrame = false) {
|
|
24
|
+
this.browser = browser;
|
|
25
|
+
this.page = page;
|
|
26
|
+
this.params = params;
|
|
27
|
+
this.visible = !!params.view;
|
|
28
|
+
this.isFrame = !!isFrame;
|
|
29
|
+
this.doListening(params);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
request(call) {
|
|
33
|
+
this.requestCall = call;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
response(call) {
|
|
38
|
+
this.responseCall = call;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 重新在当前浏览器创建新窗口
|
|
44
|
+
*/
|
|
45
|
+
async clone() {
|
|
46
|
+
try {
|
|
47
|
+
const page = await this.browser.newPage();
|
|
48
|
+
await page.setRequestInterception(true); //允许拦截
|
|
49
|
+
// await page.setUserAgent(this.params.ua);
|
|
50
|
+
await page.evaluateOnNewDocument(() => {
|
|
51
|
+
const newProto = navigator.__proto__;
|
|
52
|
+
delete newProto.webdriver; //删除 navigator.webdriver字段
|
|
53
|
+
navigator.__proto__ = newProto; //在每次新文档加载时,删除 navigator.webdriver 字段,这有助于防止某些网站检测到自动化行为。
|
|
54
|
+
});
|
|
55
|
+
return new module.exports(this.browser, page, this.params, false); //new 自身
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
console.log('[chrome.iframe.Error]', e.message);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* page.on方法
|
|
64
|
+
*/
|
|
65
|
+
on(key, call) {
|
|
66
|
+
try {
|
|
67
|
+
this.page.on(key, call);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
console.log('[chrome.on.Error]', e.message);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
once(key, call) {
|
|
76
|
+
try {
|
|
77
|
+
this.page.once(key, call);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
console.log('[chrome.on.Error]', e.message);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
off(key, call) {
|
|
86
|
+
try {
|
|
87
|
+
this.page.off(key, call);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
console.log('[chrome.on.Error]', e.message);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
emit(key, value) {
|
|
96
|
+
try {
|
|
97
|
+
this.page.emit(key, value);
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
console.log('[chrome.on.Error]', e.message);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 这必须放在已经打开过这个URL之后
|
|
107
|
+
*/
|
|
108
|
+
waitForFrame() {
|
|
109
|
+
try {
|
|
110
|
+
this.page.waitForFrame(async frame => {
|
|
111
|
+
return frame.name() === 'Test';
|
|
112
|
+
});
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
console.log('[chrome.waitForFrame.Error]', e.message);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 等待框架URL包含指定字串
|
|
123
|
+
*/
|
|
124
|
+
async waitFrame(urlKey, timeout = 5000) {
|
|
125
|
+
try {
|
|
126
|
+
return await this.page.waitForFrame(async frame => {
|
|
127
|
+
return frame.url().indexOf(urlKey) > 0;
|
|
128
|
+
}, { timeout });
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
console.log('[chrome.waitFrame.Error]', e.message);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async iframe(tag) {
|
|
136
|
+
try {
|
|
137
|
+
const frame = await (await this.page.$(tag)).contentFrame();
|
|
138
|
+
// return new iframe(this.browser, frame, this.params);
|
|
139
|
+
return new module.exports(this.browser, frame, this.params, true); //new 自身
|
|
140
|
+
}
|
|
141
|
+
catch (e) {
|
|
142
|
+
console.log('[chrome.iframe.Error]', e.message);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 关闭
|
|
148
|
+
*/
|
|
149
|
+
async close(act = 3) {
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
if (act & 1) await this.page.close();
|
|
153
|
+
if (act & 2) await this.browser.close();
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
catch (e) {
|
|
157
|
+
console.log('[chrome.close.Error]', e.message);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 断开进程与浏览器
|
|
163
|
+
*/
|
|
164
|
+
async disconnect() {
|
|
165
|
+
try {
|
|
166
|
+
await this.browser.disconnect();
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
catch (e) {
|
|
170
|
+
console.log('[chrome.disconnect.Error]', e.message);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
url() {
|
|
175
|
+
return this.page.url();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
async size(width = 1024, height = 768) {
|
|
180
|
+
await this.page.setViewport({ width, height });
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param {Object} url
|
|
186
|
+
* @param {Object} option
|
|
187
|
+
* timeout:
|
|
188
|
+
* referer:
|
|
189
|
+
* referrerPolicy:
|
|
190
|
+
* waitUntil:默认
|
|
191
|
+
* load=*等待“加载”事件。,默认
|
|
192
|
+
* domcontentloaded *等待“DOMContentLoaded”事件。
|
|
193
|
+
* networkidle0:Waits till there are no more than 0 network connections for at least `500`ms
|
|
194
|
+
* networkidle2:Waits till there are no more than 2 network connections for at least `500`ms
|
|
195
|
+
*/
|
|
196
|
+
async goto(url, option = {}) {
|
|
197
|
+
try {
|
|
198
|
+
let { timeout = 0, referer, waitUntil = 'load' } = option;
|
|
199
|
+
if (typeof url === 'number') {
|
|
200
|
+
if (url < 0) {
|
|
201
|
+
await this.page.goBack({ timeout, referer, waitUntil });
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
await this.page.goForward({ timeout, referer, waitUntil });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
await this.page.goto(url, { timeout, referer, waitUntil });
|
|
209
|
+
}
|
|
210
|
+
await this.page.goto(url, { timeout, referer, waitUntil });
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
console.log('[chrome.open.Error]', e.message);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async open(url, option = {}) {
|
|
219
|
+
return await this.goto(url, option);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 直接获取网页全部信息,或设置
|
|
224
|
+
*/
|
|
225
|
+
async content(html, option = {}) {
|
|
226
|
+
try {
|
|
227
|
+
if (html === undefined) return await this.page.content();
|
|
228
|
+
let { timeout = 0, waitUntil = 'load' } = option;
|
|
229
|
+
if (this.isFrame) {
|
|
230
|
+
await this.page.setFrameContent(html);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
await this.page.setContent(html, { timeout, waitUntil });
|
|
234
|
+
}
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
237
|
+
catch (e) {
|
|
238
|
+
console.log('[chrome.content.Error]', e.message);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 获取部分或全部HTML
|
|
244
|
+
* @param {Object} obj
|
|
245
|
+
*/
|
|
246
|
+
async html(obj) {
|
|
247
|
+
try {
|
|
248
|
+
if (obj) {
|
|
249
|
+
return await this.page.evaluate(ele => ele.innerHTML, obj);
|
|
250
|
+
}
|
|
251
|
+
return await this.page.evaluate(() => {
|
|
252
|
+
return document.documentElement.innerHTML;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
catch (e) {
|
|
256
|
+
console.log('[chrome.html.Error]', e.message);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async text(obj) {
|
|
261
|
+
try {
|
|
262
|
+
if (obj) {
|
|
263
|
+
return await this.page.evaluate(ele => ele.textContent, obj);
|
|
264
|
+
}
|
|
265
|
+
return await this.page.evaluate(() => {
|
|
266
|
+
return document.documentElement.textContent;
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
catch (e) {
|
|
270
|
+
console.log('[chrome.text.Error]', e.message);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 显示或隐藏某个标签
|
|
276
|
+
*
|
|
277
|
+
* @param {Object} tag
|
|
278
|
+
* @param {Object} show 默认显示
|
|
279
|
+
*/
|
|
280
|
+
async display(tag, show) {
|
|
281
|
+
try {
|
|
282
|
+
// const element = await this.page.querySelector(tag);
|
|
283
|
+
// element.style.display = (!!show) ? '' : 'none';
|
|
284
|
+
(await this.page.querySelector(tag)).style.display = (!!show) ? '' : 'none';
|
|
285
|
+
return this;
|
|
286
|
+
}
|
|
287
|
+
catch (e) {
|
|
288
|
+
console.log('[chrome.text.Error]', e.message);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 遍历tag1里的tag2
|
|
295
|
+
*
|
|
296
|
+
* @param {Object} tag1
|
|
297
|
+
* @param {Object} tag2
|
|
298
|
+
* @param {Object} call
|
|
299
|
+
*/
|
|
300
|
+
async elements(tag1, tag2, call) {
|
|
301
|
+
try {
|
|
302
|
+
const div = await this.page.$(tag1);
|
|
303
|
+
if (!div) {
|
|
304
|
+
throw new Error(`${tag1} not exists`);
|
|
305
|
+
}
|
|
306
|
+
for (const elm of (await div.$$(tag2))) {
|
|
307
|
+
call(elm, (await elm.evaluate(node => node.innerHTML)));
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
catch (e) {
|
|
311
|
+
console.log('[chrome.elements.Error]', e.message)
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* 支持css普通选择器方式和伪类方式
|
|
318
|
+
* div.body
|
|
319
|
+
* div#body
|
|
320
|
+
* div[name=abc]
|
|
321
|
+
* iframe:first-child
|
|
322
|
+
* div>ul>li:nth-child(4)>a
|
|
323
|
+
*
|
|
324
|
+
* @param {Object} tag
|
|
325
|
+
*/
|
|
326
|
+
async element(tag) {
|
|
327
|
+
try {
|
|
328
|
+
return await this.page.$(tag);
|
|
329
|
+
}
|
|
330
|
+
catch (e) {
|
|
331
|
+
console.log('[chrome.element.Error]', e.message);
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 等待浏览器跳转
|
|
339
|
+
*/
|
|
340
|
+
async navigation(timeout = 0, tryCount = 0) {
|
|
341
|
+
if (timeout < 200) timeout = timeout * 1000;
|
|
342
|
+
try {
|
|
343
|
+
await this.page.waitForNavigation({ timeout });
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
catch (e) {
|
|
347
|
+
if (tryCount > 0) {
|
|
348
|
+
return await this.navigation(timeout, --tryCount)
|
|
349
|
+
}
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* 等待某个元素出现
|
|
356
|
+
*/
|
|
357
|
+
async wait(ele, timeout = 0, tryCount = 0) {
|
|
358
|
+
if (timeout < 200) timeout = timeout * 1000;
|
|
359
|
+
try {
|
|
360
|
+
await this.page.waitForSelector(ele, { timeout });
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
catch (e) {
|
|
364
|
+
if (tryCount > 0) {
|
|
365
|
+
return await this.wait(ele, timeout, --tryCount)
|
|
366
|
+
}
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* 等待x秒
|
|
374
|
+
* @param {Object} time
|
|
375
|
+
*/
|
|
376
|
+
async waiting(time) {
|
|
377
|
+
return await this.sleep(time);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
async sleep(time) {
|
|
381
|
+
if (time < 100) time = time * 1000;
|
|
382
|
+
try {
|
|
383
|
+
await new Promise(res => setTimeout(res, time));
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
catch (e) {
|
|
387
|
+
console.log('[chrome.sleep.Error]', e.message);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* delay=每键入一个字符延迟毫秒
|
|
393
|
+
*/
|
|
394
|
+
async input(el, value, delay = 1) {
|
|
395
|
+
try {
|
|
396
|
+
await this.page.type(el, value, { delay });
|
|
397
|
+
return this;
|
|
398
|
+
}
|
|
399
|
+
catch (e) {
|
|
400
|
+
console.log('[chrome.input.Error]', e.message);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* \node_modules\nodejs_chromium\node_modules\puppeteer-core\lib\cjs\puppeteer\api\Page.js
|
|
406
|
+
* @param {Object} el
|
|
407
|
+
*
|
|
408
|
+
* option:
|
|
409
|
+
* delay =鼠标按下后延迟释放鼠标的时间(以毫秒为单位)。
|
|
410
|
+
* count =次数,默认1
|
|
411
|
+
* offset ={x,y}可单击点相对于边框左上角的偏移。
|
|
412
|
+
*
|
|
413
|
+
*/
|
|
414
|
+
async click(el, option = {}) {
|
|
415
|
+
try {
|
|
416
|
+
let { delay = 100, count = 1, x = 6, y = 3 } = option;
|
|
417
|
+
await this.page.click(el, { delay, count, offset: { x, y } });
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
catch (e) {
|
|
421
|
+
console.log('[chrome.click.Error]', e.message);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
async tap(el) {
|
|
426
|
+
try {
|
|
427
|
+
await this.page.tap(el);
|
|
428
|
+
return this;
|
|
429
|
+
}
|
|
430
|
+
catch (e) {
|
|
431
|
+
console.log('[chrome.hover.Error]', e.message);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
async focus(el) {
|
|
436
|
+
try {
|
|
437
|
+
await this.page.focus(el);
|
|
438
|
+
return this;
|
|
439
|
+
}
|
|
440
|
+
catch (e) {
|
|
441
|
+
console.log('[chrome.focus.Error]', e.message);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
async hover(el) {
|
|
446
|
+
try {
|
|
447
|
+
await this.page.hover(el);
|
|
448
|
+
return this;
|
|
449
|
+
}
|
|
450
|
+
catch (e) {
|
|
451
|
+
console.log('[chrome.hover.Error]', e.message);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* 根据目标要求格式提交
|
|
458
|
+
*
|
|
459
|
+
* @param url
|
|
460
|
+
* @param data
|
|
461
|
+
*/
|
|
462
|
+
async post(url, data) {
|
|
463
|
+
//JSON.stringify(data)
|
|
464
|
+
try {
|
|
465
|
+
await this.page.goto(url, { method: 'POST', body: data, waitUntil: 'load' });
|
|
466
|
+
return this;
|
|
467
|
+
}
|
|
468
|
+
catch (e) {
|
|
469
|
+
console.log('[chrome.post.Error]', e.message);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* 保存HTML
|
|
476
|
+
* @param {Object} file
|
|
477
|
+
*/
|
|
478
|
+
async saveHtml(file) {
|
|
479
|
+
try {
|
|
480
|
+
await this.improveUrls(); //修正js/css的域名
|
|
481
|
+
const body = await this.page.evaluate(() => {
|
|
482
|
+
return document.documentElement.innerHTML;
|
|
483
|
+
});
|
|
484
|
+
await fs.writeFileSync(file, body);
|
|
485
|
+
return this;
|
|
486
|
+
}
|
|
487
|
+
catch (e) {
|
|
488
|
+
console.log('[chrome.saveHtml.Error]', e.parse());
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
jsonArray(cookiesVal, host) {
|
|
494
|
+
if (!cookiesVal) return [];
|
|
495
|
+
return JSON.parse(cookiesVal).map(cook => {
|
|
496
|
+
let { name, value, domain } = cook;
|
|
497
|
+
if (!domain) domain = host;
|
|
498
|
+
return { name, value, domain };
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
|
|
504
|
+
// const url = parseUrl(this.page.url());
|
|
505
|
+
// const host = '.' + url.host.split('.').slice(-2).join('.');
|
|
506
|
+
// let cookiesVal = read(file, 'utf8');
|
|
507
|
+
// cookiesVal = this.jsonArray(cookiesVal, host);
|
|
508
|
+
|
|
509
|
+
*/
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* 合并两个Cookies,并以后面的值为准
|
|
513
|
+
*
|
|
514
|
+
* @param {Object} oldCookies
|
|
515
|
+
* @param {Object} newCookies
|
|
516
|
+
*/
|
|
517
|
+
async mergeNewCookies(oldCookies, newCookies) {
|
|
518
|
+
if (newCookies.length === 0) return oldCookies;
|
|
519
|
+
// let aMap = new Map(oldCookies.map(obj => [obj.name, obj]));//浅拷贝
|
|
520
|
+
let aMap = JSON.parse(JSON.stringify(this.cookies)); //深拷贝
|
|
521
|
+
newCookies.map(obj => {
|
|
522
|
+
aMap.set(obj.name, obj)
|
|
523
|
+
});
|
|
524
|
+
return await Array.from(aMap.values());
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* 获取当前页面的Cookies
|
|
530
|
+
*/
|
|
531
|
+
async getCookies() {
|
|
532
|
+
try {
|
|
533
|
+
return await this.page.cookies();
|
|
534
|
+
}
|
|
535
|
+
catch (e) {
|
|
536
|
+
console.log('[chrome.getCookies.Error]', e.message);
|
|
537
|
+
return [];
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* 设置Cookies
|
|
543
|
+
* @param {Object} cookies
|
|
544
|
+
*/
|
|
545
|
+
async setCookies(cookies) {
|
|
546
|
+
try {
|
|
547
|
+
await this.page.setCookie(...cookies);
|
|
548
|
+
return this;
|
|
549
|
+
}
|
|
550
|
+
catch (e) {
|
|
551
|
+
console.log('[chrome.setCookies.Error]', e.message);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* 保存当前页面中的Cookies
|
|
557
|
+
* @param {Object} file
|
|
558
|
+
*/
|
|
559
|
+
async saveCookies(file, append = true) {
|
|
560
|
+
try {
|
|
561
|
+
let cookies = await this.page.cookies();
|
|
562
|
+
|
|
563
|
+
if (append && fs.existsSync(file)) {
|
|
564
|
+
let dbCookies = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
565
|
+
cookies = this.mergeNewCookies(dbCookies, cookies);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
await fs.writeFileSync(file, JSON.stringify(cookies, null, 2));
|
|
569
|
+
return this;
|
|
570
|
+
}
|
|
571
|
+
catch (e) {
|
|
572
|
+
console.log('[chrome.saveCookies.Error]', e.parse());
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* 加水印,这里实际上是在网页加个DIV并显示时间
|
|
578
|
+
* @param {Object} conf
|
|
579
|
+
*/
|
|
580
|
+
async watermark(conf) {
|
|
581
|
+
try {
|
|
582
|
+
const tmpID = 'watermark' + Date.now();
|
|
583
|
+
await this.page.evaluate((conf, tmpID) => {
|
|
584
|
+
const wmDiv = document.createElement('div');
|
|
585
|
+
wmDiv.id = tmpID;
|
|
586
|
+
wmDiv.style.position = 'fixed';
|
|
587
|
+
wmDiv.style.top = `50%`;
|
|
588
|
+
wmDiv.style.left = `50%`;
|
|
589
|
+
wmDiv.style.transform = 'translate(-50%, -50%)';
|
|
590
|
+
wmDiv.style.fontSize = `36px`;
|
|
591
|
+
wmDiv.style.zIndex = '10000';
|
|
592
|
+
// wmDiv.style.color = color;
|
|
593
|
+
wmDiv.style.color = 'rgba(0, 0, 0, 0.9)';
|
|
594
|
+
wmDiv.style.pointerEvents = 'none';
|
|
595
|
+
wmDiv.innerText = conf.text;
|
|
596
|
+
document.body.appendChild(wmDiv);
|
|
597
|
+
}, conf, tmpID);
|
|
598
|
+
await this.page.waitForSelector(`div#${tmpID}`, { timeout: 1000 });
|
|
599
|
+
return this;
|
|
600
|
+
}
|
|
601
|
+
catch (e) {
|
|
602
|
+
console.log('[chrome.watermark.Error]', e.message);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* 网页拍照
|
|
609
|
+
* @param file
|
|
610
|
+
* @param quality
|
|
611
|
+
*/
|
|
612
|
+
async photograph(file, quality = 50) {
|
|
613
|
+
try {
|
|
614
|
+
await this.page.screenshot({
|
|
615
|
+
path: file,
|
|
616
|
+
fullPage: true, //全屏
|
|
617
|
+
type: 'jpeg',
|
|
618
|
+
quality: quality,
|
|
619
|
+
omitBackground: true, //显示背景
|
|
620
|
+
});
|
|
621
|
+
// console.log('photograph=', file);
|
|
622
|
+
return this;
|
|
623
|
+
}
|
|
624
|
+
catch (e) {
|
|
625
|
+
console.log('[chrome.photograph.Error]', e.message);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* 补全所有本地js/css
|
|
631
|
+
*/
|
|
632
|
+
async improveUrls() {
|
|
633
|
+
const url = parseUrl(this.page.url());
|
|
634
|
+
const domain = url.protocol + '//' + url.host;
|
|
635
|
+
await this.page.evaluate((domain) => {
|
|
636
|
+
try {
|
|
637
|
+
const tags = document.querySelectorAll('script[src], link[href], iframe[src]');
|
|
638
|
+
tags.forEach((ele) => {
|
|
639
|
+
const src = ele.getAttribute('src');
|
|
640
|
+
const href = ele.getAttribute('href');
|
|
641
|
+
if (src && src.startsWith('/')) ele.src = domain + src;
|
|
642
|
+
if (href && href.startsWith('/')) ele.href = domain + href;
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
catch (e) {
|
|
646
|
+
console.log('[chrome.improveUrls.Error]', e.message);
|
|
647
|
+
}
|
|
648
|
+
}, domain);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* 解析网页set-cookies的值
|
|
654
|
+
*
|
|
655
|
+
* @param {Object} strCookies
|
|
656
|
+
*/
|
|
657
|
+
async parseCookies(strCookies) {
|
|
658
|
+
return await strCookies.split("\n").map((ls, i) => {
|
|
659
|
+
let value = {};
|
|
660
|
+
ls.split(';').map((ln, j) => {
|
|
661
|
+
// console.log(ln);
|
|
662
|
+
const arr = ln.split('=');
|
|
663
|
+
const Key = (arr[0]).trim();
|
|
664
|
+
if (!Key) return;
|
|
665
|
+
|
|
666
|
+
if (j === 0) {
|
|
667
|
+
value.name = Key;
|
|
668
|
+
value.value = arr[1];
|
|
669
|
+
}
|
|
670
|
+
else if (Key === 'Max-Age') {
|
|
671
|
+
value.expire = parseInt(arr[1]) + (Date.now() / 1000);
|
|
672
|
+
}
|
|
673
|
+
else if (Key === 'Secure') {
|
|
674
|
+
value.source = true;
|
|
675
|
+
value.sourceScheme = 'Secure';
|
|
676
|
+
}
|
|
677
|
+
else {
|
|
678
|
+
value[Key[0].toLowerCase() + Key.substring(1)] = arr[1] || true;
|
|
679
|
+
}
|
|
680
|
+
})
|
|
681
|
+
return value;
|
|
682
|
+
})
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
async parseResponse(response) {
|
|
687
|
+
// const response = await this.page.waitForResponse(res => res);
|
|
688
|
+
|
|
689
|
+
const value = {};
|
|
690
|
+
const request = await response.request();
|
|
691
|
+
const headers = await response.headers();
|
|
692
|
+
value.method = await request.method();
|
|
693
|
+
if (value.method === 'OPTIONS') return;
|
|
694
|
+
|
|
695
|
+
value.type = await request.resourceType();
|
|
696
|
+
// value.redirect = await response.redirectURL();
|
|
697
|
+
if (value.type === 'xhr') value.type = 'AJAX';
|
|
698
|
+
value.url = await response.url();
|
|
699
|
+
value.domain = parseUrl(value.url)['host'];
|
|
700
|
+
value.content = headers['content-type'];
|
|
701
|
+
value.length = headers['content-length'];
|
|
702
|
+
value.status = await response.status();
|
|
703
|
+
value.ok = await response.ok();
|
|
704
|
+
value.datetime = (new Date(headers['date'])).date('yyyy-mm-dd hh:ii:ss');
|
|
705
|
+
// value.headers = headers;
|
|
706
|
+
if (headers['server']) value.server = headers['server'];
|
|
707
|
+
if (headers['set-cookie']) value.cookies = await this.parseCookies(headers['set-cookie']);
|
|
708
|
+
value.remote = await response.remoteAddress(); //目标服务器
|
|
709
|
+
if (value.status === 301 || value.status === 302) return value;
|
|
710
|
+
if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping', 'fetch'].has(value.type)) return value;
|
|
711
|
+
if (value.content) {
|
|
712
|
+
if (value.content.startsWith('application/vnd')) return value;
|
|
713
|
+
if (value.content.startsWith('application/xml')) return value;
|
|
714
|
+
if (value.content.startsWith('text/css')) return value;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
value.post = await request.postData();
|
|
718
|
+
if (value.post) value.post = value.post.toString();
|
|
719
|
+
|
|
720
|
+
try {
|
|
721
|
+
value.buffer = await response.buffer();
|
|
722
|
+
value.response = value.buffer.toString();
|
|
723
|
+
value.json = JSON.parse(value.response);
|
|
724
|
+
}
|
|
725
|
+
catch (e) {
|
|
726
|
+
value.json = e.parse();
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
return value;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
doListening(params) {
|
|
734
|
+
|
|
735
|
+
this.page.on('request', async (request) => {
|
|
736
|
+
if (this.requestCall) {
|
|
737
|
+
const run = await this.requestCall(request);
|
|
738
|
+
if (run === false) {
|
|
739
|
+
request.abort();
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const { abort_img = false, no_cache = true, append_headers = {} } = params;
|
|
745
|
+
if (abort_img && /\.(?:png|jpg|jpeg|svg|gif)$/i.test(request.url())) {
|
|
746
|
+
request.abort();
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
const headers = request.headers();
|
|
751
|
+
if (append_headers !== {}) Object.assign(headers, append_headers);
|
|
752
|
+
|
|
753
|
+
headers['Access-Control-Allow-Origin'] = '*'; // 设置允许跨源访问的域名,可以根据需求修改
|
|
754
|
+
headers['Access-Control-Allow-Methods'] = '*'; //'GET, POST, PUT, OPTIONS';
|
|
755
|
+
// headers['Access-Control-Allow-Headers'] = 'Content-Type';
|
|
756
|
+
|
|
757
|
+
if (no_cache) {
|
|
758
|
+
const rType = request.resourceType();
|
|
759
|
+
if (rType === 'script' || rType === 'stylesheet') {
|
|
760
|
+
headers['Cache-Control'] = 'no-store'; // 禁用缓存
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
request.continue({ headers });
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
this.page.on('response', async res => {
|
|
768
|
+
if (!this.responseCall) return;
|
|
769
|
+
let json = await this.parseResponse(res);
|
|
770
|
+
await this.responseCall(json);
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
|
|
733
775
|
}
|