豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 498bd19

Browse files
committed
chore: make changes
1 parent 44ea547 commit 498bd19

File tree

2 files changed

+16
-65
lines changed

2 files changed

+16
-65
lines changed

src/tools/pages.ts

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,11 @@ export const navigatePage = defineTool({
133133
.describe(
134134
'Whether to auto accept or beforeunload dialogs triggered by this navigation. Default is accept.',
135135
),
136-
initScript: zod.string().optional().describe(
137-
`(optional) A JavaScript function to be executed by the tool on new document for every page load before any other scripts.
138-
`),
139136
initScript: zod
140137
.string()
141138
.optional()
142139
.describe(
143-
'(optional) A JavaScript function to be executed by the tool on new document for every page load before any other scripts.',
140+
'A JavaScript script to be executed on each new document before any other scripts for the next navigation.',
144141
),
145142
...timeoutSchema,
146143
},
@@ -170,70 +167,17 @@ export const navigatePage = defineTool({
170167
}
171168
// We are not going to report the dialog like regular dialogs.
172169
context.clearDialog();
173-
if (request.params.initScript) {
174-
await page.evaluateOnNewDocument(request.params.initScript);
175-
}
176-
177-
await context.waitForEventsAfterAction(async () => {
178-
switch (request.params.type) {
179-
case 'url':
180-
if (!request.params.url) {
181-
throw new Error('A URL is required for navigation of type=url.');
182-
}
183-
try {
184-
await page.goto(request.params.url, options);
185-
response.appendResponseLine(
186-
`Successfully navigated to ${request.params.url}.`,
187-
);
188-
} catch (error) {
189-
response.appendResponseLine(
190-
`Unable to navigate in the selected page: ${error.message}.`,
191-
);
192-
}
193-
break;
194-
case 'back':
195-
try {
196-
await page.goBack(options);
197-
response.appendResponseLine(
198-
`Successfully navigated back to ${page.url()}.`,
199-
);
200-
} catch (error) {
201-
response.appendResponseLine(
202-
`Unable to navigate back in the selected page: ${error.message}.`,
203-
);
204-
}
205-
break;
206-
case 'forward':
207-
try {
208-
await page.goForward(options);
209-
response.appendResponseLine(
210-
`Successfully navigated forward to ${page.url()}.`,
211-
);
212-
} catch (error) {
213-
response.appendResponseLine(
214-
`Unable to navigate forward in the selected page: ${error.message}.`,
215-
);
216-
}
217-
break;
218-
case 'reload':
219-
try {
220-
await page.reload({
221-
...options,
222-
ignoreCache: request.params.ignoreCache,
223-
});
224-
response.appendResponseLine(`Successfully reloaded the page.`);
225-
} catch (error) {
226-
response.appendResponseLine(
227-
`Unable to reload the selected page: ${error.message}.`,
228-
);
229-
}
230-
break;
231170
}
232-
});
171+
};
233172

173+
let initScriptId: string | undefined;
234174
if (request.params.initScript) {
235-
await page.evaluateOnNewDocument(request.params.initScript);
175+
const {identifier} = await page.evaluateOnNewDocument(
176+
request.params.initScript,
177+
);
178+
initScriptId = identifier;
236179
}
180+
237181
page.on('dialog', dialogHandler);
238182

239183
try {
@@ -295,6 +239,13 @@ export const navigatePage = defineTool({
295239
});
296240
} finally {
297241
page.off('dialog', dialogHandler);
242+
if (initScriptId) {
243+
await page
244+
.removeScriptToEvaluateOnNewDocument(initScriptId)
245+
.catch(error => {
246+
logger(`Failed to remove init script`, error);
247+
});
248+
}
298249
}
299250

300251
response.setIncludePages(true);

tests/tools/pages.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ describe('pages', () => {
298298
});
299299
});
300300
it('navigates to correct page with initScript', async () => {
301-
await withBrowser(async (response, context) => {
301+
await withMcpContext(async (response, context) => {
302302
await navigatePage.handler(
303303
{
304304
params: {

0 commit comments

Comments
 (0)