forked from ChromeDevTools/chrome-devtools-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemulation.ts
More file actions
222 lines (200 loc) · 5.99 KB
/
emulation.ts
File metadata and controls
222 lines (200 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {PredefinedNetworkConditions} from 'puppeteer-core';
import type {CDPSession, Protocol, Viewport} from 'puppeteer-core';
import z from 'zod';
import {ToolCategories} from './categories.js';
import {defineTool} from './ToolDefinition.js';
const throttlingOptions: [string, ...string[]] = [
'No emulation',
...Object.keys(PredefinedNetworkConditions),
];
const deviceProfileOptions = ['iPhone-12-Pro', 'Pixel-7'] as const;
type DeviceProfileName = (typeof deviceProfileOptions)[number];
interface DeviceProfileDefinition {
metrics: Protocol.Emulation.SetDeviceMetricsOverrideRequest;
touch: Protocol.Emulation.SetTouchEmulationEnabledRequest;
userAgent: Protocol.Network.SetUserAgentOverrideRequest;
viewport: Viewport;
locale?: string;
timezoneId?: string;
}
const DEVICE_PROFILES: Record<DeviceProfileName, DeviceProfileDefinition> = {
'iPhone-12-Pro': {
metrics: {
width: 390,
height: 844,
deviceScaleFactor: 3,
mobile: true,
screenWidth: 390,
screenHeight: 844,
screenOrientation: {
type: 'portraitPrimary',
angle: 0,
},
positionX: 0,
positionY: 0,
scale: 1,
},
touch: {
enabled: true,
maxTouchPoints: 5,
},
viewport: {
width: 390,
height: 844,
deviceScaleFactor: 3,
isMobile: true,
hasTouch: true,
isLandscape: false,
},
userAgent: {
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
platform: 'iPhone',
acceptLanguage: 'en-US,en',
},
locale: 'en-US',
timezoneId: 'America/Los_Angeles',
},
'Pixel-7': {
metrics: {
width: 412,
height: 915,
deviceScaleFactor: 2.625,
mobile: true,
screenWidth: 412,
screenHeight: 915,
screenOrientation: {
type: 'portraitPrimary',
angle: 0,
},
positionX: 0,
positionY: 0,
scale: 1,
},
touch: {
enabled: true,
maxTouchPoints: 5,
},
viewport: {
width: 412,
height: 915,
deviceScaleFactor: 2.625,
isMobile: true,
hasTouch: true,
isLandscape: false,
},
userAgent: {
userAgent:
'Mozilla/5.0 (Linux; Android 13; Pixel 7 Build/TD1A.221105.001) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36',
platform: 'Android',
acceptLanguage: 'en-US,en',
},
locale: 'en-US',
timezoneId: 'America/Los_Angeles',
},
};
function getClient(page: unknown): CDPSession {
return (page as { _client(): CDPSession })._client();
}
export const emulateNetwork = defineTool({
name: 'emulate_network',
description: `Emulates network conditions such as throttling on the selected page.`,
annotations: {
category: ToolCategories.EMULATION,
readOnlyHint: false,
},
schema: {
throttlingOption: z
.enum(throttlingOptions)
.describe(
`The network throttling option to emulate. Available throttling options are: ${throttlingOptions.join(', ')}. Set to "No emulation" to disable.`,
),
},
handler: async (request, _response, context) => {
const page = context.getSelectedPage();
const conditions = request.params.throttlingOption;
if (conditions === 'No emulation') {
await page.emulateNetworkConditions(null);
context.setNetworkConditions(null);
return;
}
if (conditions in PredefinedNetworkConditions) {
const networkCondition =
PredefinedNetworkConditions[
conditions as keyof typeof PredefinedNetworkConditions
];
await page.emulateNetworkConditions(networkCondition);
context.setNetworkConditions(conditions);
}
},
});
export const emulateCpu = defineTool({
name: 'emulate_cpu',
description: `Emulates CPU throttling by slowing down the selected page's execution.`,
annotations: {
category: ToolCategories.EMULATION,
readOnlyHint: false,
},
schema: {
throttlingRate: z
.number()
.min(1)
.max(20)
.describe(
'The CPU throttling rate representing the slowdown factor 1-20x. Set the rate to 1 to disable throttling',
),
},
handler: async (request, _response, context) => {
const page = context.getSelectedPage();
const {throttlingRate} = request.params;
await page.emulateCPUThrottling(throttlingRate);
context.setCpuThrottlingRate(throttlingRate);
},
});
export const emulateDeviceProfile = defineTool({
name: 'emulate_device_profile',
description:
'Emulates a device profile by applying predefined viewport metrics, touch, user agent, locale, and timezone settings.',
annotations: {
category: ToolCategories.EMULATION,
readOnlyHint: false,
},
schema: {
profile: z
.enum(deviceProfileOptions)
.describe(
`The device profile preset to apply. Supported profiles: ${deviceProfileOptions.join(', ')}.`,
),
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
const profileName = request.params.profile;
const profile = DEVICE_PROFILES[profileName];
if (!profile) {
throw new Error(`Unknown device profile: ${profileName}`);
}
const client = getClient(page);
await client.send('Emulation.setDeviceMetricsOverride', profile.metrics);
await client.send('Network.setUserAgentOverride', profile.userAgent);
if (profile.locale) {
await client.send('Emulation.setLocaleOverride', {
locale: profile.locale,
});
}
if (profile.timezoneId) {
await client.send('Emulation.setTimezoneOverride', {
timezoneId: profile.timezoneId,
});
}
await page.setViewport(profile.viewport);
await client.send('Emulation.setTouchEmulationEnabled', profile.touch);
response.appendResponseLine(
`Applied device profile "${profileName}" to the selected page.`,
);
},
});