Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/tool-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@

- **handleBeforeUnload** (enum: "accept", "decline") _(optional)_: Whether to auto accept or beforeunload dialogs triggered by this navigation. Default is accept.
- **ignoreCache** (boolean) _(optional)_: Whether to ignore cache on reload.
- **initScript** (string) _(optional)_: A JavaScript script to be executed on each new document before any other scripts for the next navigation.
- **timeout** (integer) _(optional)_: Maximum wait time in milliseconds. If set to 0, the default timeout will be used.
- **type** (enum: "url", "back", "forward", "reload") _(optional)_: Navigate the page by URL, back or forward in history, or reload.
- **url** (string) _(optional)_: Target URL (only type=url)
Expand Down
22 changes: 22 additions & 0 deletions src/tools/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ export const navigatePage = defineTool({
.describe(
'Whether to auto accept or beforeunload dialogs triggered by this navigation. Default is accept.',
),
initScript: zod
.string()
.optional()
.describe(
'A JavaScript script to be executed on each new document before any other scripts for the next navigation.',
),
...timeoutSchema,
},
handler: async (request, response, context) => {
Expand Down Expand Up @@ -163,6 +169,15 @@ export const navigatePage = defineTool({
context.clearDialog();
}
};

let initScriptId: string | undefined;
if (request.params.initScript) {
const {identifier} = await page.evaluateOnNewDocument(
request.params.initScript,
);
initScriptId = identifier;
}

page.on('dialog', dialogHandler);

try {
Expand Down Expand Up @@ -224,6 +239,13 @@ export const navigatePage = defineTool({
});
} finally {
page.off('dialog', dialogHandler);
if (initScriptId) {
await page
.removeScriptToEvaluateOnNewDocument(initScriptId)
.catch(error => {
logger(`Failed to remove init script`, error);
});
}
}

response.setIncludePages(true);
Expand Down
22 changes: 22 additions & 0 deletions tests/tools/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,28 @@ describe('pages', () => {
assert.ok(response.includePages);
});
});
it('navigates to correct page with initScript', async () => {
await withMcpContext(async (response, context) => {
await navigatePage.handler(
{
params: {
url: 'data:text/html,<div>Hello MCP</div>',
initScript: 'window.initScript = "completed"',
},
},
response,
context,
);
const page = context.getSelectedPage();

// wait for up to 1s for the global variable to set by the initScript to exist
await page.waitForFunction("window.initScript==='completed'", {
timeout: 1000,
});

assert.ok(response.includePages);
});
});
});
describe('resize', () => {
it('resize the page', async () => {
Expand Down