Skip to content

Commit 33ddb8f

Browse files
fix: move dialogHandler inside conditional block
Moved `dialogHandler` inside the `if (options?.dialog)` block to avoid unnecessarily declaring it when it is not needed. Also imported the correct `Dialog` type from Puppeteer. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 3abedb8 commit 33ddb8f

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

src/WaitForHelper.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import {logger} from './logger.js';
8-
import type {Page, Protocol, CdpPage} from './third_party/index.js';
8+
import type {Page, Protocol, CdpPage, Dialog} from './third_party/index.js';
99
import type {PredefinedNetworkConditions} from './third_party/index.js';
1010

1111
export class WaitForHelper {
@@ -128,15 +128,16 @@ export class WaitForHelper {
128128
action: () => Promise<unknown>,
129129
options?: {timeout?: number; dialog?: 'accept' | 'dismiss'},
130130
): Promise<void> {
131-
const dialogHandler = (dialog: any) => {
132-
if (options?.dialog === 'dismiss') {
133-
void dialog.dismiss();
134-
} else {
135-
void dialog.accept();
136-
}
137-
};
131+
let dialogHandler: ((dialog: Dialog) => void) | undefined;
138132
if (options?.dialog) {
139-
this.#page.on('dialog', dialogHandler);
133+
dialogHandler = (dialog: Dialog) => {
134+
if (options.dialog === 'dismiss') {
135+
void dialog.dismiss();
136+
} else {
137+
void dialog.accept();
138+
}
139+
};
140+
(this.#page as unknown as Page).on('dialog', dialogHandler);
140141
}
141142

142143
const navigationFinished = this.waitForNavigationStarted()
@@ -169,8 +170,8 @@ export class WaitForHelper {
169170
logger(error);
170171
} finally {
171172
this.#abortController.abort();
172-
if (options?.dialog) {
173-
this.#page.off('dialog', dialogHandler);
173+
if (options?.dialog && dialogHandler) {
174+
(this.#page as unknown as Page).off('dialog', dialogHandler);
174175
}
175176
}
176177
}

src/tools/script.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ Example with arguments: \`(el) => {
7777
}
7878

7979
const worker = await getWebWorker(context, serviceWorkerId);
80-
await context
81-
.getSelectedMcpPage()
82-
.waitForEventsAfterAction(async () => {
80+
await context.getSelectedMcpPage().waitForEventsAfterAction(
81+
async () => {
8382
await performEvaluation(worker, fnString, [], response);
84-
}, {dialog: 'accept'});
83+
},
84+
{dialog: 'accept'},
85+
);
8586
return;
8687
}
8788

@@ -101,9 +102,12 @@ Example with arguments: \`(el) => {
101102

102103
const evaluatable = await getPageOrFrame(page, frames);
103104

104-
await mcpPage.waitForEventsAfterAction(async () => {
105-
await performEvaluation(evaluatable, fnString, args, response);
106-
}, {dialog: 'accept'});
105+
await mcpPage.waitForEventsAfterAction(
106+
async () => {
107+
await performEvaluation(evaluatable, fnString, args, response);
108+
},
109+
{dialog: 'accept'},
110+
);
107111
} finally {
108112
void Promise.allSettled(args.map(arg => arg.dispose()));
109113
}

0 commit comments

Comments
 (0)