From 158a970fe551700a0f23f989c46e98a28a25fcfb Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Mon, 18 May 2026 14:06:20 +0200 Subject: [PATCH] fix: improve geolocation emulation --- docs/tool-reference.md | 2 +- src/McpResponse.ts | 9 +++++++++ src/bin/chrome-devtools-cli-options.ts | 2 +- src/tools/ToolDefinition.ts | 2 +- src/tools/emulation.ts | 5 +++-- tests/tools/emulation.test.ts | 2 +- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/docs/tool-reference.md b/docs/tool-reference.md index 578fae1a0..79596def9 100644 --- a/docs/tool-reference.md +++ b/docs/tool-reference.md @@ -256,7 +256,7 @@ - **colorScheme** (enum: "dark", "light", "auto") _(optional)_: [`Emulate`](#emulate) the dark or the light mode. Set to "auto" to reset to the default. - **cpuThrottlingRate** (number) _(optional)_: Represents the CPU slowdown factor. Omit or set the rate to 1 to disable throttling -- **geolocation** (string) _(optional)_: Geolocation (`<latitude>x<longitude>`) to [`emulate`](#emulate). Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override. +- **geolocation** (string) _(optional)_: Geolocation (`<latitude>,<longitude>`) to [`emulate`](#emulate). Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override. - **networkConditions** (enum: "Offline", "Slow 3G", "Fast 3G", "Slow 4G", "Fast 4G") _(optional)_: Throttle network. Omit to disable throttling. - **userAgent** (string) _(optional)_: User agent to [`emulate`](#emulate). Set to empty string to clear the user agent override. - **viewport** (string) _(optional)_: [`Emulate`](#emulate) device viewports '<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]'. 'touch' and 'mobile' to [`emulate`](#emulate) mobile devices. 'landscape' to [`emulate`](#emulate) landscape mode. diff --git a/src/McpResponse.ts b/src/McpResponse.ts index 64404bcab..ec6857418 100644 --- a/src/McpResponse.ts +++ b/src/McpResponse.ts @@ -746,6 +746,7 @@ export class McpResponse implements Response { extensionPages?: object[]; errorMessage?: string; navigatedToUrl?: string; + geolocation?: {latitude: number; longitude: number}; } = {}; const response = []; @@ -773,6 +774,14 @@ export class McpResponse implements Response { structuredContent.navigationTimeout = timeout; } + const geolocation = this.#page?.geolocation; + if (geolocation) { + response.push( + `Emulating geolocation: latitude=${geolocation.latitude}, longtitude=${geolocation.longitude}`, + ); + structuredContent.geolocation = geolocation; + } + const viewport = this.#page?.viewport; if (viewport) { response.push(`Emulating viewport: ${JSON.stringify(viewport)}`); diff --git a/src/bin/chrome-devtools-cli-options.ts b/src/bin/chrome-devtools-cli-options.ts index 60418088a..55f4af014 100644 --- a/src/bin/chrome-devtools-cli-options.ts +++ b/src/bin/chrome-devtools-cli-options.ts @@ -142,7 +142,7 @@ export const commands: Commands = { name: 'geolocation', type: 'string', description: - 'Geolocation (`x`) to emulate. Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override.', + 'Geolocation (`,`) to emulate. Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override.', required: false, }, userAgent: { diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.ts index 5027c6cdb..66b6c353a 100644 --- a/src/tools/ToolDefinition.ts +++ b/src/tools/ToolDefinition.ts @@ -417,7 +417,7 @@ export function geolocationTransform(arg: string | undefined) { if (!arg) { return undefined; } - const [latitude, longitude] = arg.split('x').map(Number) as [number, number]; + const [latitude, longitude] = arg.split(',').map(Number) as [number, number]; return { latitude, longitude, diff --git a/src/tools/emulation.ts b/src/tools/emulation.ts index 2dbb026f2..7eebe9200 100644 --- a/src/tools/emulation.ts +++ b/src/tools/emulation.ts @@ -44,7 +44,7 @@ export const emulate = definePageTool({ .optional() .transform(geolocationTransform) .describe( - 'Geolocation (`x`) to emulate. Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override.', + 'Geolocation (`,`) to emulate. Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override.', ), userAgent: zod .string() @@ -67,8 +67,9 @@ export const emulate = definePageTool({ ), }, blockedByDialog: true, - handler: async (request, _response, context) => { + handler: async (request, response, context) => { const page = request.page; await context.emulate(request.params, page.pptrPage); + response.appendResponseLine('Emulation configured successfully'); }, }); diff --git a/tests/tools/emulation.test.ts b/tests/tools/emulation.test.ts index 721505fc2..b1a19a904 100644 --- a/tests/tools/emulation.test.ts +++ b/tests/tools/emulation.test.ts @@ -75,7 +75,7 @@ describe('emulation', () => { }); it('parses latitude and longitude', () => { - assert.deepStrictEqual(geolocationTransform('48.137154x11.576124'), { + assert.deepStrictEqual(geolocationTransform('48.137154,11.576124'), { latitude: 48.137154, longitude: 11.576124, });