Skip to content
Open
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 @@ -149,6 +149,7 @@
**Parameters:**

- **ignoreCache** (boolean) _(optional)_: Whether to ignore cache on reload.
- **initScript** (string) _(optional)_: (optional) A JavaScript function to be executed by the tool on new document for every page load before any other scripts.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR! I have asked a few details on the issue. Do you think the new_page tool could be a better place for the init script? Any thoughts how to remove a previously specified init script and if removing it is needed? For your use case, could it be better to pass an init script for all pages via a CLI argument or do you want the LLM to generate the init script?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't thought that much about the different approaches to be honest.
I would be fine to just close this PR for now as it's very minimal (it's just a snapshot basically of what I'm playing with locally at the moment) and keep the discussions in the issues for now to land on a good / better general solution for the problem I'm trying to solve :)

- **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
10 changes: 10 additions & 0 deletions src/tools/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ export const navigatePage = defineTool({
.boolean()
.optional()
.describe('Whether to ignore cache on reload.'),
initScript: zod
.string()
.optional()
.describe(
'(optional) A JavaScript function to be executed by the tool on new document for every page load before any other scripts.',
),
...timeoutSchema,
},
handler: async (request, response, context) => {
Expand All @@ -132,6 +138,10 @@ export const navigatePage = defineTool({
request.params.type = 'url';
}

if (request.params.initScript) {
await page.evaluateOnNewDocument(request.params.initScript);
}

await context.waitForEventsAfterAction(async () => {
switch (request.params.type) {
case 'url':
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 @@ -193,6 +193,28 @@ describe('pages', () => {
assert.ok(response.includePages);
});
});
it('navigates to correct page with initScript', async () => {
await withBrowser(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('create a page', async () => {
Expand Down