Skip to content

Commit dafbd29

Browse files
committed
fix: detect scheduled script navigations
1 parent 2dc104c commit dafbd29

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/utils/WaitForHelper.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class WaitForHelper {
157157

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

178178
navigationStartedResolvers.resolve(true);
179179
};
180+
const scheduledNavigationListener = (
181+
event: Protocol.Page.FrameScheduledNavigationEvent,
182+
) => {
183+
if (event.frameId === this.#page.mainFrame()._id) {
184+
navigationStartedResolvers.resolve(true);
185+
}
186+
};
180187

181188
this.#page._client().on('Page.frameStartedNavigating', navigationListener);
189+
this.#page
190+
._client()
191+
.on('Page.frameScheduledNavigation', scheduledNavigationListener);
182192
navigationAbortController.signal.addEventListener('abort', () => {
183193
this.#page
184194
._client()
185195
.off('Page.frameStartedNavigating', navigationListener);
196+
this.#page
197+
._client()
198+
.off('Page.frameScheduledNavigation', scheduledNavigationListener);
186199
});
187200
this.#abortController.signal.addEventListener(
188201
'abort',

0 commit comments

Comments
 (0)