diff --git a/docs/tool-reference.md b/docs/tool-reference.md index 0e126b8c..64054697 100644 --- a/docs/tool-reference.md +++ b/docs/tool-reference.md @@ -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. - **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) diff --git a/src/tools/pages.ts b/src/tools/pages.ts index f4638860..270c08e4 100644 --- a/src/tools/pages.ts +++ b/src/tools/pages.ts @@ -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) => { @@ -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': diff --git a/tests/tools/pages.test.ts b/tests/tools/pages.test.ts index a4a76bcd..6e69de61 100644 --- a/tests/tools/pages.test.ts +++ b/tests/tools/pages.test.ts @@ -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,