2019-01-15 04:34:50 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-07-13 09:22:26 +00:00
|
|
|
import { assert } from './assert.js';
|
2022-06-14 11:16:21 +00:00
|
|
|
import {
|
|
|
|
addEventListener,
|
|
|
|
PuppeteerEventListener,
|
|
|
|
removeEventListeners,
|
|
|
|
} from './util.js';
|
2020-07-13 09:22:26 +00:00
|
|
|
import { TimeoutError } from './Errors.js';
|
|
|
|
import {
|
|
|
|
FrameManager,
|
|
|
|
Frame,
|
|
|
|
FrameManagerEmittedEvents,
|
|
|
|
} from './FrameManager.js';
|
|
|
|
import { HTTPRequest } from './HTTPRequest.js';
|
|
|
|
import { HTTPResponse } from './HTTPResponse.js';
|
|
|
|
import { NetworkManagerEmittedEvents } from './NetworkManager.js';
|
|
|
|
import { CDPSessionEmittedEvents } from './Connection.js';
|
2021-04-06 08:58:01 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
export type PuppeteerLifeCycleEvent =
|
|
|
|
| 'load'
|
|
|
|
| 'domcontentloaded'
|
|
|
|
| 'networkidle0'
|
|
|
|
| 'networkidle2';
|
2021-04-12 13:57:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type ProtocolLifeCycleEvent =
|
2020-05-07 10:54:55 +00:00
|
|
|
| 'load'
|
|
|
|
| 'DOMContentLoaded'
|
|
|
|
| 'networkIdle'
|
|
|
|
| 'networkAlmostIdle';
|
|
|
|
|
|
|
|
const puppeteerToProtocolLifecycle = new Map<
|
|
|
|
PuppeteerLifeCycleEvent,
|
|
|
|
ProtocolLifeCycleEvent
|
|
|
|
>([
|
2020-04-27 09:03:33 +00:00
|
|
|
['load', 'load'],
|
|
|
|
['domcontentloaded', 'DOMContentLoaded'],
|
|
|
|
['networkidle0', 'networkIdle'],
|
|
|
|
['networkidle2', 'networkAlmostIdle'],
|
|
|
|
]);
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
const noop = (): void => {};
|
|
|
|
|
2020-07-06 11:37:16 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-27 09:03:33 +00:00
|
|
|
export class LifecycleWatcher {
|
2022-06-13 09:16:25 +00:00
|
|
|
#expectedLifecycle: ProtocolLifeCycleEvent[];
|
|
|
|
#frameManager: FrameManager;
|
|
|
|
#frame: Frame;
|
|
|
|
#timeout: number;
|
|
|
|
#navigationRequest: HTTPRequest | null = null;
|
|
|
|
#eventListeners: PuppeteerEventListener[];
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
|
|
|
#sameDocumentNavigationPromise = new Promise<Error | undefined>((fulfill) => {
|
|
|
|
this.#sameDocumentNavigationCompleteCallback = fulfill;
|
2022-05-24 11:14:19 +00:00
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#lifecycleCallback: () => void = noop;
|
|
|
|
#lifecyclePromise: Promise<void> = new Promise((fulfill) => {
|
|
|
|
this.#lifecycleCallback = fulfill;
|
2022-05-24 11:14:19 +00:00
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#newDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
|
|
|
#newDocumentNavigationPromise: Promise<Error | undefined> = new Promise(
|
2022-05-24 11:14:19 +00:00
|
|
|
(fulfill) => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#newDocumentNavigationCompleteCallback = fulfill;
|
2022-05-24 11:14:19 +00:00
|
|
|
}
|
|
|
|
);
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#terminationCallback: (x?: Error) => void = noop;
|
|
|
|
#terminationPromise: Promise<Error | undefined> = new Promise((fulfill) => {
|
|
|
|
this.#terminationCallback = fulfill;
|
2022-05-24 11:14:19 +00:00
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#timeoutPromise: Promise<TimeoutError | undefined>;
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#maximumTimer?: NodeJS.Timeout;
|
|
|
|
#hasSameDocumentNavigation?: boolean;
|
|
|
|
#newDocumentNavigation?: boolean;
|
|
|
|
#swapped?: boolean;
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
frameManager: FrameManager,
|
|
|
|
frame: Frame,
|
|
|
|
waitUntil: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[],
|
|
|
|
timeout: number
|
|
|
|
) {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (Array.isArray(waitUntil)) {
|
|
|
|
waitUntil = waitUntil.slice();
|
|
|
|
} else if (typeof waitUntil === 'string') {
|
|
|
|
waitUntil = [waitUntil];
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#expectedLifecycle = waitUntil.map((value) => {
|
2019-10-23 11:12:33 +00:00
|
|
|
const protocolEvent = puppeteerToProtocolLifecycle.get(value);
|
2019-01-15 04:34:50 +00:00
|
|
|
assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value);
|
2022-05-24 11:14:19 +00:00
|
|
|
return protocolEvent as ProtocolLifeCycleEvent;
|
2019-01-15 04:34:50 +00:00
|
|
|
});
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager = frameManager;
|
|
|
|
this.#frame = frame;
|
|
|
|
this.#timeout = timeout;
|
|
|
|
this.#eventListeners = [
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2020-05-07 10:54:55 +00:00
|
|
|
frameManager._client,
|
2020-07-08 10:11:01 +00:00
|
|
|
CDPSessionEmittedEvents.Disconnected,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#terminate.bind(
|
2022-06-09 17:00:50 +00:00
|
|
|
this,
|
|
|
|
new Error('Navigation failed because browser has disconnected!')
|
|
|
|
)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.LifecycleEvent,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#checkLifecycleComplete.bind(this)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.FrameNavigatedWithinDocument,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#navigatedWithinDocument.bind(this)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2022-05-30 08:30:30 +00:00
|
|
|
FrameManagerEmittedEvents.FrameNavigated,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#navigated.bind(this)
|
2022-05-30 08:30:30 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2022-03-09 11:24:17 +00:00
|
|
|
FrameManagerEmittedEvents.FrameSwapped,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameSwapped.bind(this)
|
2022-03-09 11:24:17 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.FrameDetached,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameDetached.bind(this)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager.networkManager(),
|
2020-07-07 15:43:55 +00:00
|
|
|
NetworkManagerEmittedEvents.Request,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onRequest.bind(this)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2019-01-15 04:34:50 +00:00
|
|
|
];
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#timeoutPromise = this.#createTimeoutPromise();
|
|
|
|
this.#checkLifecycleComplete();
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onRequest(request: HTTPRequest): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (request.frame() !== this.#frame || !request.isNavigationRequest()) {
|
2019-01-15 04:34:50 +00:00
|
|
|
return;
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#navigationRequest = request;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameDetached(frame: Frame): void {
|
|
|
|
if (this.#frame === frame) {
|
|
|
|
this.#terminationCallback.call(
|
2020-05-07 10:54:55 +00:00
|
|
|
null,
|
|
|
|
new Error('Navigating frame was detached')
|
|
|
|
);
|
2019-01-15 04:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#checkLifecycleComplete();
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 07:19:14 +00:00
|
|
|
async navigationResponse(): Promise<HTTPResponse | null> {
|
|
|
|
// We may need to wait for ExtraInfo events before the request is complete.
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#navigationRequest ? this.#navigationRequest.response() : null;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#terminate(error: Error): void {
|
|
|
|
this.#terminationCallback.call(null, error);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
sameDocumentNavigationPromise(): Promise<Error | undefined> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#sameDocumentNavigationPromise;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
newDocumentNavigationPromise(): Promise<Error | undefined> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#newDocumentNavigationPromise;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
lifecyclePromise(): Promise<void> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#lifecyclePromise;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | undefined> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return Promise.race([this.#timeoutPromise, this.#terminationPromise]);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
async #createTimeoutPromise(): Promise<TimeoutError | undefined> {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!this.#timeout) {
|
|
|
|
return new Promise(noop);
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
const errorMessage =
|
2022-06-13 09:16:25 +00:00
|
|
|
'Navigation timeout of ' + this.#timeout + ' ms exceeded';
|
|
|
|
await new Promise(
|
|
|
|
(fulfill) => (this.#maximumTimer = setTimeout(fulfill, this.#timeout))
|
|
|
|
);
|
|
|
|
return new TimeoutError(errorMessage);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#navigatedWithinDocument(frame: Frame): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame !== this.#frame) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#hasSameDocumentNavigation = true;
|
|
|
|
this.#checkLifecycleComplete();
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#navigated(frame: Frame): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame !== this.#frame) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#newDocumentNavigation = true;
|
|
|
|
this.#checkLifecycleComplete();
|
2022-05-30 08:30:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#frameSwapped(frame: Frame): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame !== this.#frame) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#swapped = true;
|
|
|
|
this.#checkLifecycleComplete();
|
2022-03-09 11:24:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#checkLifecycleComplete(): void {
|
2019-01-15 04:34:50 +00:00
|
|
|
// We expect navigation to commit.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!checkLifecycle(this.#frame, this.#expectedLifecycle)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#lifecycleCallback();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#hasSameDocumentNavigation) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sameDocumentNavigationCompleteCallback();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (this.#swapped || this.#newDocumentNavigation) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#newDocumentNavigationCompleteCallback();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2019-01-15 04:34:50 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
function checkLifecycle(
|
|
|
|
frame: Frame,
|
|
|
|
expectedLifecycle: ProtocolLifeCycleEvent[]
|
|
|
|
): boolean {
|
2019-01-15 04:34:50 +00:00
|
|
|
for (const event of expectedLifecycle) {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame._lifecycleEvents.has(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
for (const child of frame.childFrames()) {
|
2022-05-17 12:15:44 +00:00
|
|
|
if (
|
|
|
|
child._hasStartedLoading &&
|
|
|
|
!checkLifecycle(child, expectedLifecycle)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
dispose(): void {
|
2022-06-14 11:16:21 +00:00
|
|
|
removeEventListeners(this.#eventListeners);
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#maximumTimer !== undefined && clearTimeout(this.#maximumTimer);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
}
|