Skip to content
Draft
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
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"sinon": "^22.0.0",
"typescript": "^6.0.2",
"typescript-eslint": "^8.43.0",
"urlpattern-polyfill": "^10.1.0",
"yargs": "18.0.0"
},
"engines": {
Expand Down
5 changes: 0 additions & 5 deletions src/bin/chrome-devtools-mcp-cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,6 @@ export const cliOptions = {
describe:
'Whether to include all kinds of pages such as webviews or background pages as pages.',
},
experimentalNavigationAllowlist: {
type: 'boolean',
describe: 'Whether to enable navigation allowlist tool parameter.',
hidden: true,
},
experimentalInteropTools: {
type: 'boolean',
describe: 'Whether to enable interoperability tools',
Expand Down
1 change: 0 additions & 1 deletion src/third_party/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

import 'urlpattern-polyfill';
import 'core-js/modules/es.promise.with-resolvers.js';
import 'core-js/modules/es.set.union.v2.js';
import 'core-js/proposals/iterator-helpers.js';
Expand Down
211 changes: 58 additions & 153 deletions src/tools/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,17 @@
*/

import {logger} from '../logger.js';
import type {CdpPage, Dialog, HTTPRequest} from '../third_party/index.js';
import type {CdpPage, Dialog} from '../third_party/index.js';
import {zod} from '../third_party/index.js';

import {ToolCategory} from './categories.js';
import type {ContextPage} from './ToolDefinition.js';
import {
CLOSE_PAGE_ERROR,
definePageTool,
defineTool,
timeoutSchema,
} from './ToolDefinition.js';

async function navigateWithInterception(
page: ContextPage,
action: () => Promise<unknown>,
allowListString?: string,
timeout?: number,
): Promise<void> {
const allowList = allowListString
? allowListString.split(',').map((p: string) => new URLPattern(p.trim()))
: undefined;

const requestHandler = (interceptedRequest: HTTPRequest) => {
if (!interceptedRequest.isNavigationRequest()) {
void interceptedRequest.continue();
return;
}
const requestUrl = interceptedRequest.url();
const isAllowed = allowList!.some((pattern: URLPattern) =>
pattern.test(requestUrl),
);

if (isAllowed) {
void interceptedRequest.continue();
} else {
logger?.(`Blocking request to: ${requestUrl}`);
void interceptedRequest.abort('blockedbyclient');
}
};

const cleanupInterception = async () => {
if (allowList) {
page.pptrPage.off('request', requestHandler);
await page.pptrPage.setRequestInterception(false).catch(error => {
logger?.(`Failed to disable request interception`, error);
});
}
};

if (allowList) {
await page.pptrPage.setRequestInterception(true);
page.pptrPage.on('request', requestHandler);
}

try {
await page.waitForEventsAfterAction(
async () => {
try {
await action();
} finally {
await cleanupInterception();
}
},
{timeout},
);
} finally {
await cleanupInterception();
}
}

export const listPages = defineTool(args => {
return {
name: 'list_pages',
Expand Down Expand Up @@ -155,7 +96,7 @@
},
});

export const newPage = defineTool(args => {

Check failure on line 99 in src/tools/pages.ts

View workflow job for this annotation

GitHub Actions / [Required] Check docs updated

'args' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 99 in src/tools/pages.ts

View workflow job for this annotation

GitHub Actions / [Required] Check correct format

'args' is defined but never used. Allowed unused args must match /^_/u
return {
name: 'new_page',
description: `Open a new tab and load a URL. Use project URL if not specified otherwise.`,
Expand All @@ -179,16 +120,6 @@
'Pages in the same browser context share cookies and storage. ' +
'Pages in different browser contexts are fully isolated.',
),
...(args?.experimentalNavigationAllowlist
? {
allowList: zod
.string()
.optional()
.describe(
'Optional comma-separated list of URL patterns to allow. If provided, all other navigations will be blocked.',
),
}
: {}),
...timeoutSchema,
},
blockedByDialog: false,
Expand All @@ -199,15 +130,9 @@
request.params.isolatedContext,
);

await navigateWithInterception(
page,
() =>
page.pptrPage.goto(request.params.url, {
timeout: request.params.timeout,
}),
request.params.allowList,
request.params.timeout,
);
await page.pptrPage.goto(request.params.url, {
timeout: request.params.timeout,
});

response.setIncludePages(true);
response.setListThirdPartyDeveloperTools();
Expand All @@ -215,7 +140,7 @@
};
});

export const navigatePage = definePageTool(args => {

Check failure on line 143 in src/tools/pages.ts

View workflow job for this annotation

GitHub Actions / [Required] Check docs updated

'args' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 143 in src/tools/pages.ts

View workflow job for this annotation

GitHub Actions / [Required] Check correct format

'args' is defined but never used. Allowed unused args must match /^_/u
return {
name: 'navigate_page',
description: `Go to a URL, or back, forward, or reload. Use project URL if not specified otherwise.`,
Expand Down Expand Up @@ -247,16 +172,7 @@
.describe(
'A JavaScript script to be executed on each new document before any other scripts for the next navigation.',
),
...(args?.experimentalNavigationAllowlist
? {
allowList: zod
.string()
.optional()
.describe(
'Optional comma-separated list of URL patterns to allow. If provided, all other navigations will be blocked.',
),
}
: {}),

...timeoutSchema,
},
blockedByDialog: false,
Expand Down Expand Up @@ -301,71 +217,60 @@
page.pptrPage.on('dialog', dialogHandler);

try {
await navigateWithInterception(
page,
async () => {
switch (request.params.type) {
case 'url':
if (!request.params.url) {
throw new Error(
'A URL is required for navigation of type=url.',
);
}
try {
await page.pptrPage.goto(request.params.url, options);
response.appendResponseLine(
`Successfully navigated to ${request.params.url}.`,
);
} catch (error) {
response.appendResponseLine(
`Unable to navigate in the selected page: ${error.message}.`,
);
}
break;
case 'back':
try {
await page.pptrPage.goBack(options);
response.appendResponseLine(
`Successfully navigated back to ${page.pptrPage.url()}.`,
);
} catch (error) {
response.appendResponseLine(
`Unable to navigate back in the selected page: ${error.message}.`,
);
}
break;
case 'forward':
try {
await page.pptrPage.goForward(options);
response.appendResponseLine(
`Successfully navigated forward to ${page.pptrPage.url()}.`,
);
} catch (error) {
response.appendResponseLine(
`Unable to navigate forward in the selected page: ${error.message}.`,
);
}
break;
case 'reload':
try {
await page.pptrPage.reload({
...options,
ignoreCache: request.params.ignoreCache,
});
response.appendResponseLine(
`Successfully reloaded the page.`,
);
} catch (error) {
response.appendResponseLine(
`Unable to reload the selected page: ${error.message}.`,
);
}
break;
switch (request.params.type) {
case 'url':
if (!request.params.url) {
throw new Error('A URL is required for navigation of type=url.');
}
},
request.params.allowList,
request.params.timeout,
);
try {
await page.pptrPage.goto(request.params.url, options);
response.appendResponseLine(
`Successfully navigated to ${request.params.url}.`,
);
} catch (error) {
response.appendResponseLine(
`Unable to navigate in the selected page: ${error.message}.`,
);
}
break;
case 'back':
try {
await page.pptrPage.goBack(options);
response.appendResponseLine(
`Successfully navigated back to ${page.pptrPage.url()}.`,
);
} catch (error) {
response.appendResponseLine(
`Unable to navigate back in the selected page: ${error.message}.`,
);
}
break;
case 'forward':
try {
await page.pptrPage.goForward(options);
response.appendResponseLine(
`Successfully navigated forward to ${page.pptrPage.url()}.`,
);
} catch (error) {
response.appendResponseLine(
`Unable to navigate forward in the selected page: ${error.message}.`,
);
}
break;
case 'reload':
try {
await page.pptrPage.reload({
...options,
ignoreCache: request.params.ignoreCache,
});
response.appendResponseLine(`Successfully reloaded the page.`);
} catch (error) {
response.appendResponseLine(
`Unable to reload the selected page: ${error.message}.`,
);
}
break;
}
} finally {
page.pptrPage.off('dialog', dialogHandler);
if (initScriptId) {
Expand Down
Loading
Loading