mirror of
https://github.com/ChromeDevTools/chrome-devtools-mcp.git
synced 2026-09-14 19:45:30 +08:00
fix: improve geolocation emulation (#2036)
- add a message about successful configuration - add a message about the currently emulated geolocation - switch to comma separate format instead of `x` separator. Tested with https://www.audero.it/demo/geolocation-api-demo.html
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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)}`);
|
||||
|
||||
@@ -142,7 +142,7 @@ export const commands: Commands = {
|
||||
name: 'geolocation',
|
||||
type: 'string',
|
||||
description:
|
||||
'Geolocation (`<latitude>x<longitude>`) to emulate. Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override.',
|
||||
'Geolocation (`<latitude>,<longitude>`) to emulate. Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override.',
|
||||
required: false,
|
||||
},
|
||||
userAgent: {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -44,7 +44,7 @@ export const emulate = definePageTool({
|
||||
.optional()
|
||||
.transform(geolocationTransform)
|
||||
.describe(
|
||||
'Geolocation (`<latitude>x<longitude>`) to emulate. Latitude between -90 and 90. Longitude between -180 and 180. Omit to clear the geolocation override.',
|
||||
'Geolocation (`<latitude>,<longitude>`) 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');
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user