Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/utils/WaitForHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class WaitForHelper {

// A scoped AbortController used to clean up navigation probe listeners.
// When aborted (either after navigation detection finishes or if this.#abortController
// aborts), it removes the CDP Page.frameStartedNavigating listener and automatically
// aborts), it removes CDP navigation probe listeners and automatically
// detaches the abort listener from this.#abortController.signal.
const navigationAbortController = new AbortController();
const navigationStartedResolvers = Promise.withResolvers<boolean>();
Expand All @@ -177,12 +177,25 @@ export class WaitForHelper {

navigationStartedResolvers.resolve(true);
};
const scheduledNavigationListener = (
event: Protocol.Page.FrameScheduledNavigationEvent,
) => {
if (event.frameId === this.#page.mainFrame()._id) {
navigationStartedResolvers.resolve(true);
}
};

this.#page._client().on('Page.frameStartedNavigating', navigationListener);
this.#page
._client()
.on('Page.frameScheduledNavigation', scheduledNavigationListener);
navigationAbortController.signal.addEventListener('abort', () => {
this.#page
._client()
.off('Page.frameStartedNavigating', navigationListener);
this.#page
._client()
.off('Page.frameScheduledNavigation', scheduledNavigationListener);
});
this.#abortController.signal.addEventListener(
'abort',
Expand Down