Skip to content

Commit a75a706

Browse files
Lightning00BladeOrKoN
authored andcommitted
fix: move dialogHandler inside conditional block
1 parent 1ccfc3f commit a75a706

4 files changed

Lines changed: 21 additions & 21 deletions

File tree

src/McpPage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class McpPage implements ContextPage {
117117

118118
waitForEventsAfterAction(
119119
action: () => Promise<unknown>,
120-
options?: {timeout?: number; dialog?: 'accept' | 'dismiss'},
120+
options?: {timeout?: number; handleDialog?: boolean},
121121
): Promise<void> {
122122
const helper = this.createWaitForHelper(
123123
this.cpuThrottlingRate,

src/WaitForHelper.ts

Lines changed: 8 additions & 12 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 {
@@ -126,17 +126,16 @@ export class WaitForHelper {
126126

127127
async waitForEventsAfterAction(
128128
action: () => Promise<unknown>,
129-
options?: {timeout?: number; dialog?: 'accept' | 'dismiss'},
129+
options?: {timeout?: number; handleDialog?: boolean},
130130
): Promise<void> {
131-
const dialogHandler = (dialog: any) => {
132-
if (options?.dialog === 'dismiss') {
133-
void dialog.dismiss();
134-
} else {
131+
if (options?.handleDialog) {
132+
const dialogHandler = (dialog: Pick<Dialog, 'accept'>) => {
135133
void dialog.accept();
136-
}
137-
};
138-
if (options?.dialog) {
134+
};
139135
this.#page.on('dialog', dialogHandler);
136+
this.#abortController.signal.addEventListener('abort', () => {
137+
this.#page.off('dialog', dialogHandler);
138+
});
140139
}
141140

142141
const navigationFinished = this.waitForNavigationStarted()
@@ -169,9 +168,6 @@ export class WaitForHelper {
169168
logger(error);
170169
} finally {
171170
this.#abortController.abort();
172-
if (options?.dialog) {
173-
this.#page.off('dialog', dialogHandler);
174-
}
175171
}
176172
}
177173
}

src/tools/ToolDefinition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export type ContextPage = Readonly<{
247247
clearDialog(): void;
248248
waitForEventsAfterAction(
249249
action: () => Promise<unknown>,
250-
options?: {timeout?: number; dialog?: 'accept' | 'dismiss'},
250+
options?: {timeout?: number; handleDialog?: boolean},
251251
): Promise<void>;
252252
getInPageTools(): ToolGroup<InPageToolDefinition> | undefined;
253253
}>;

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+
{handleDialog: true},
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+
{handleDialog: true},
110+
);
107111
} finally {
108112
void Promise.allSettled(args.map(arg => arg.dispose()));
109113
}

0 commit comments

Comments
 (0)