Skip to content

Commit 002feb2

Browse files
authored
dismiss js dialog popups (#895)
move the JS dialog handler to not be only for autoclick, dismiss all JS dialogs (alert(), prompt()) to avoid blocking page fixes #891
1 parent 2270964 commit 002feb2

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/crawler.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -852,31 +852,34 @@ self.__bx_behaviors.selectMainBehavior();
852852
await this.browser.addInitScript(page, initScript);
853853
}
854854

855-
// only add if running with autoclick behavior
856-
if (this.params.behaviors.includes("autoclick")) {
857-
// Ensure off-page navigation is canceled while behavior is running
858-
page.on("dialog", async (dialog) => {
859-
let accepted = true;
860-
if (dialog.type() === "beforeunload") {
861-
if (opts.pageBlockUnload) {
862-
accepted = false;
863-
await dialog.dismiss();
864-
} else {
865-
await dialog.accept();
866-
}
855+
// Handle JS dialogs:
856+
// - Ensure off-page navigation is canceled while behavior is running
857+
// - dismiss close all other dialogs if not blocking unload
858+
page.on("dialog", async (dialog) => {
859+
let accepted = true;
860+
if (dialog.type() === "beforeunload") {
861+
if (opts.pageBlockUnload) {
862+
accepted = false;
863+
await dialog.dismiss();
867864
} else {
868865
await dialog.accept();
869866
}
870-
logger.debug("JS Dialog", {
871-
accepted,
872-
blockingUnload: opts.pageBlockUnload,
873-
message: dialog.message(),
874-
type: dialog.type(),
875-
page: page.url(),
876-
workerid,
877-
});
867+
} else {
868+
// other JS dialog, just dismiss
869+
await dialog.dismiss();
870+
}
871+
logger.debug("JS Dialog", {
872+
accepted,
873+
blockingUnload: opts.pageBlockUnload,
874+
message: dialog.message(),
875+
type: dialog.type(),
876+
page: page.url(),
877+
workerid,
878878
});
879+
});
879880

881+
// only add if running with autoclick behavior
882+
if (this.params.behaviors.includes("autoclick")) {
880883
// Close any windows opened during navigation from autoclick
881884
await cdp.send("Target.setDiscoverTargets", { discover: true });
882885

0 commit comments

Comments
 (0)