summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/packages/puppeteer-core/src/bidi/core/Navigation.ts
diff options
context:
space:
mode:
Diffstat (limited to 'remote/test/puppeteer/packages/puppeteer-core/src/bidi/core/Navigation.ts')
-rw-r--r--remote/test/puppeteer/packages/puppeteer-core/src/bidi/core/Navigation.ts75
1 files changed, 52 insertions, 23 deletions
diff --git a/remote/test/puppeteer/packages/puppeteer-core/src/bidi/core/Navigation.ts b/remote/test/puppeteer/packages/puppeteer-core/src/bidi/core/Navigation.ts
index a7efbfeb2c..50040164a5 100644
--- a/remote/test/puppeteer/packages/puppeteer-core/src/bidi/core/Navigation.ts
+++ b/remote/test/puppeteer/packages/puppeteer-core/src/bidi/core/Navigation.ts
@@ -41,9 +41,10 @@ export class Navigation extends EventEmitter<{
// keep-sorted start
#request: Request | undefined;
+ #navigation: Navigation | undefined;
readonly #browsingContext: BrowsingContext;
readonly #disposables = new DisposableStack();
- readonly #id = new Deferred<string>();
+ readonly #id = new Deferred<string | null>();
// keep-sorted end
private constructor(context: BrowsingContext) {
@@ -65,31 +66,48 @@ export class Navigation extends EventEmitter<{
this.dispose();
});
- this.#browsingContext.on('request', ({request}) => {
- if (request.navigation === this.#id.value()) {
- this.#request = request;
- this.emit('request', request);
+ browsingContextEmitter.on('request', ({request}) => {
+ if (
+ request.navigation === undefined ||
+ this.#request !== undefined ||
+ // If a request with a navigation ID comes in, then the navigation ID is
+ // for this navigation.
+ !this.#matches(request.navigation)
+ ) {
+ return;
}
+
+ this.#request = request;
+ this.emit('request', request);
});
const sessionEmitter = this.#disposables.use(
new EventEmitter(this.#session)
);
- // To get the navigation ID if any.
+ sessionEmitter.on('browsingContext.navigationStarted', info => {
+ if (
+ info.context !== this.#browsingContext.id ||
+ this.#navigation !== undefined
+ ) {
+ return;
+ }
+ this.#navigation = Navigation.from(this.#browsingContext);
+ });
+
for (const eventName of [
'browsingContext.domContentLoaded',
'browsingContext.load',
] as const) {
sessionEmitter.on(eventName, info => {
- if (info.context !== this.#browsingContext.id) {
- return;
- }
- if (!info.navigation) {
+ if (
+ info.context !== this.#browsingContext.id ||
+ info.navigation === null ||
+ !this.#matches(info.navigation)
+ ) {
return;
}
- if (!this.#id.resolved()) {
- this.#id.resolve(info.navigation);
- }
+
+ this.dispose();
});
}
@@ -99,18 +117,15 @@ export class Navigation extends EventEmitter<{
['browsingContext.navigationAborted', 'aborted'],
] as const) {
sessionEmitter.on(eventName, info => {
- if (info.context !== this.#browsingContext.id) {
- return;
- }
- if (!info.navigation) {
- return;
- }
- if (!this.#id.resolved()) {
- this.#id.resolve(info.navigation);
- }
- if (this.#id.value() !== info.navigation) {
+ if (
+ info.context !== this.#browsingContext.id ||
+ // Note we don't check if `navigation` is null since `null` means the
+ // fragment navigated.
+ !this.#matches(info.navigation)
+ ) {
return;
}
+
this.emit(event, {
url: info.url,
timestamp: new Date(info.timestamp),
@@ -120,6 +135,17 @@ export class Navigation extends EventEmitter<{
}
}
+ #matches(navigation: string | null): boolean {
+ if (this.#navigation !== undefined && !this.#navigation.disposed) {
+ return false;
+ }
+ if (!this.#id.resolved()) {
+ this.#id.resolve(navigation);
+ return true;
+ }
+ return this.#id.value() === navigation;
+ }
+
// keep-sorted start block=yes
get #session() {
return this.#browsingContext.userContext.browser.session;
@@ -130,6 +156,9 @@ export class Navigation extends EventEmitter<{
get request(): Request | undefined {
return this.#request;
}
+ get navigation(): Navigation | undefined {
+ return this.#navigation;
+ }
// keep-sorted end
@inertIfDisposed