Skip to content

Commit 942eb5f

Browse files
committed
refactor: extract waitForEventsAfterAction out of performEvaluation
Service worker evaluation should not trigger DOM stability wait since service workers don't interact with the page DOM. Move the waitForEventsAfterAction call to the normal page evaluation path only, removing the page parameter from performEvaluation.
1 parent 62c1955 commit 942eb5f

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

src/tools/script.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {Frame, JSHandle, Page, WebWorker} from '../third_party/index.js';
99
import type {ExtensionServiceWorker} from '../types.js';
1010

1111
import {ToolCategory} from './categories.js';
12-
import type {Context, ContextPage, Response} from './ToolDefinition.js';
12+
import type {Context, Response} from './ToolDefinition.js';
1313
import {defineTool, pageIdSchema} from './ToolDefinition.js';
1414

1515
export type Evaluatable = Page | Frame | WebWorker;
@@ -77,7 +77,7 @@ Example with arguments: \`(el) => {
7777
}
7878

7979
const worker = await getWebWorker(context, serviceWorkerId);
80-
await performEvaluation(worker, fnString, [], response, context.getSelectedMcpPage());
80+
await performEvaluation(worker, fnString, [], response);
8181
return;
8282
}
8383

@@ -97,7 +97,9 @@ Example with arguments: \`(el) => {
9797

9898
const evaluatable = await getPageOrFrame(page, frames);
9999

100-
await performEvaluation(evaluatable, fnString, args, response, mcpPage);
100+
await mcpPage.waitForEventsAfterAction(async () => {
101+
await performEvaluation(evaluatable, fnString, args, response);
102+
});
101103
} finally {
102104
void Promise.allSettled(args.map(arg => arg.dispose()));
103105
}
@@ -110,24 +112,21 @@ const performEvaluation = async (
110112
fnString: string,
111113
args: Array<JSHandle<unknown>>,
112114
response: Response,
113-
page: ContextPage,
114115
) => {
115116
const fn = await evaluatable.evaluateHandle(`(${fnString})`);
116117
try {
117-
await page.waitForEventsAfterAction(async () => {
118-
const result = await evaluatable.evaluate(
119-
async (fn, ...args) => {
120-
// @ts-expect-error no types for function fn
121-
return JSON.stringify(await fn(...args));
122-
},
123-
fn,
124-
...args,
125-
);
126-
response.appendResponseLine('Script ran on page and returned:');
127-
response.appendResponseLine('```json');
128-
response.appendResponseLine(`${result}`);
129-
response.appendResponseLine('```');
130-
});
118+
const result = await evaluatable.evaluate(
119+
async (fn, ...args) => {
120+
// @ts-expect-error no types for function fn
121+
return JSON.stringify(await fn(...args));
122+
},
123+
fn,
124+
...args,
125+
);
126+
response.appendResponseLine('Script ran on page and returned:');
127+
response.appendResponseLine('```json');
128+
response.appendResponseLine(`${result}`);
129+
response.appendResponseLine('```');
131130
} finally {
132131
void fn.dispose();
133132
}

0 commit comments

Comments
 (0)