1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const {helper} = require('./helper');
|
18 | const {FrameManager} = require('./FrameManager');
|
19 |
|
20 | class NavigatorWatcher {
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | constructor(frameManager, frame, timeout, options = {}) {
|
28 | console.assert(options.networkIdleTimeout === undefined, 'ERROR: networkIdleTimeout option is no longer supported.');
|
29 | console.assert(options.networkIdleInflight === undefined, 'ERROR: networkIdleInflight option is no longer supported.');
|
30 | console.assert(options.waitUntil !== 'networkidle', 'ERROR: "networkidle" option is no longer supported. Use "networkidle2" instead');
|
31 | let waitUntil = ['load'];
|
32 | if (Array.isArray(options.waitUntil))
|
33 | waitUntil = options.waitUntil.slice();
|
34 | else if (typeof options.waitUntil === 'string')
|
35 | waitUntil = [options.waitUntil];
|
36 | this._expectedLifecycle = waitUntil.map(value => {
|
37 | const protocolEvent = puppeteerToProtocolLifecycle[value];
|
38 | console.assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value);
|
39 | return protocolEvent;
|
40 | });
|
41 |
|
42 | this._frameManager = frameManager;
|
43 | this._frame = frame;
|
44 | this._initialLoaderId = frame._loaderId;
|
45 | this._timeout = timeout;
|
46 | this._hasSameDocumentNavigation = false;
|
47 | this._eventListeners = [
|
48 | helper.addEventListener(this._frameManager, FrameManager.Events.LifecycleEvent, this._checkLifecycleComplete.bind(this)),
|
49 | helper.addEventListener(this._frameManager, FrameManager.Events.FrameNavigatedWithinDocument, this._navigatedWithinDocument.bind(this)),
|
50 | helper.addEventListener(this._frameManager, FrameManager.Events.FrameDetached, this._checkLifecycleComplete.bind(this))
|
51 | ];
|
52 |
|
53 | const lifecycleCompletePromise = new Promise(fulfill => {
|
54 | this._lifecycleCompleteCallback = fulfill;
|
55 | });
|
56 | this._navigationPromise = Promise.race([
|
57 | this._createTimeoutPromise(),
|
58 | lifecycleCompletePromise
|
59 | ]).then(error => {
|
60 | this._cleanup();
|
61 | return error;
|
62 | });
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 | _createTimeoutPromise() {
|
69 | if (!this._timeout)
|
70 | return new Promise(() => {});
|
71 | const errorMessage = 'Navigation Timeout Exceeded: ' + this._timeout + 'ms exceeded';
|
72 | return new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, this._timeout))
|
73 | .then(() => new Error(errorMessage));
|
74 | }
|
75 |
|
76 | |
77 |
|
78 |
|
79 | async navigationPromise() {
|
80 | return this._navigationPromise;
|
81 | }
|
82 |
|
83 | |
84 |
|
85 |
|
86 | _navigatedWithinDocument(frame) {
|
87 | if (frame !== this._frame)
|
88 | return;
|
89 | this._hasSameDocumentNavigation = true;
|
90 | this._checkLifecycleComplete();
|
91 | }
|
92 |
|
93 | _checkLifecycleComplete() {
|
94 |
|
95 | if (this._frame._loaderId === this._initialLoaderId && !this._hasSameDocumentNavigation)
|
96 | return;
|
97 | if (!checkLifecycle(this._frame, this._expectedLifecycle))
|
98 | return;
|
99 | this._lifecycleCompleteCallback();
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 | function checkLifecycle(frame, expectedLifecycle) {
|
107 | for (const event of expectedLifecycle) {
|
108 | if (!frame._lifecycleEvents.has(event))
|
109 | return false;
|
110 | }
|
111 | for (const child of frame.childFrames()) {
|
112 | if (!checkLifecycle(child, expectedLifecycle))
|
113 | return false;
|
114 | }
|
115 | return true;
|
116 | }
|
117 | }
|
118 |
|
119 | cancel() {
|
120 | this._cleanup();
|
121 | }
|
122 |
|
123 | _cleanup() {
|
124 | helper.removeEventListeners(this._eventListeners);
|
125 | this._lifecycleCompleteCallback(new Error('Navigation failed'));
|
126 | clearTimeout(this._maximumTimer);
|
127 | }
|
128 | }
|
129 |
|
130 | const puppeteerToProtocolLifecycle = {
|
131 | 'load': 'load',
|
132 | 'domcontentloaded': 'DOMContentLoaded',
|
133 | 'networkidle0': 'networkIdle',
|
134 | 'networkidle2': 'networkAlmostIdle',
|
135 | };
|
136 |
|
137 | module.exports = NavigatorWatcher;
|