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
49 changes: 16 additions & 33 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 @@ -81,7 +81,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
6 changes: 4 additions & 2 deletions src/telemetry/flag_usage_metrics.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,13 @@
},
{
"name": "experimental_navigation_allowlist",
"flagType": "boolean"
"flagType": "boolean",
"isDeprecated": true
},
{
"name": "experimental_navigation_allowlist_present",
"flagType": "boolean"
"flagType": "boolean",
"isDeprecated": true
},
{
"name": "experimental_page_id_routing",
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
106 changes: 11 additions & 95 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 closePage = defineTool({
},
});

export const newPage = defineTool(args => {
export const newPage = defineTool(() => {
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 @@ export const newPage = defineTool(args => {
'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,14 +130,13 @@ export const newPage = defineTool(args => {
request.params.isolatedContext,
);

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

response.setIncludePages(true);
Expand All @@ -215,7 +145,7 @@ export const newPage = defineTool(args => {
};
});

export const navigatePage = definePageTool(args => {
export const navigatePage = definePageTool(() => {
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 +177,6 @@ export const navigatePage = definePageTool(args => {
.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 @@ -298,11 +218,8 @@ export const navigatePage = definePageTool(args => {
initScriptId = identifier;
}

page.pptrPage.on('dialog', dialogHandler);

try {
await navigateWithInterception(
page,
await page.waitForEventsAfterAction(
async () => {
switch (request.params.type) {
case 'url':
Expand Down Expand Up @@ -363,8 +280,7 @@ export const navigatePage = definePageTool(args => {
break;
}
},
request.params.allowList,
request.params.timeout,
{timeout: request.params.timeout},
);
} finally {
page.pptrPage.off('dialog', dialogHandler);
Expand Down
Loading
Loading