-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathemulation.ts
More file actions
177 lines (168 loc) · 4.91 KB
/
emulation.ts
File metadata and controls
177 lines (168 loc) · 4.91 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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*
*/
import {zod, PredefinedNetworkConditions} from '../third_party/index.js';
import {ToolCategory} from './categories.js';
import {defineTool} from './ToolDefinition.js';
const throttlingOptions: [string, ...string[]] = [
'No emulation',
'Offline',
...Object.keys(PredefinedNetworkConditions),
];
export const emulate = defineTool({
name: 'emulate',
description: `Emulates various features on the selected page.`,
annotations: {
category: ToolCategory.EMULATION,
readOnlyHint: false,
},
schema: {
networkConditions: zod
.enum(throttlingOptions)
.optional()
.describe(
`Throttle network. Set to "No emulation" to disable. If omitted, conditions remain unchanged.`,
),
cpuThrottlingRate: zod
.number()
.min(1)
.max(20)
.optional()
.describe(
'Represents the CPU slowdown factor. Set the rate to 1 to disable throttling. If omitted, throttling remains unchanged.',
),
geolocation: zod
.object({
latitude: zod
.number()
.min(-90)
.max(90)
.describe('Latitude between -90 and 90.'),
longitude: zod
.number()
.min(-180)
.max(180)
.describe('Longitude between -180 and 180.'),
})
.nullable()
.optional()
.describe(
'Geolocation to emulate. Set to null to clear the geolocation override.',
),
userAgent: zod
.string()
.nullable()
.optional()
.describe(
'User agent to emulate. Set to null to clear the user agent override.',
),
viewport: zod
.object({
width: zod.number().int().min(0).describe('Page width in pixels.'),
height: zod.number().int().min(0).describe('Page height in pixels.'),
deviceScaleFactor: zod
.number()
.min(0)
.optional()
.describe('Specify device scale factor (can be thought of as dpr).'),
isMobile: zod
.boolean()
.optional()
.describe(
'Whether the meta viewport tag is taken into account. Defaults to false.',
),
hasTouch: zod
.boolean()
.optional()
.describe(
'Specifies if viewport supports touch events. This should be set to true for mobile devices.',
),
isLandscape: zod
.boolean()
.optional()
.describe(
'Specifies if viewport is in landscape mode. Defaults to false.',
),
})
.nullable()
.optional()
.describe(
'Viewport to emulate. Set to null to reset to the default viewport.',
),
},
handler: async (request, _response, context) => {
const page = context.getSelectedPage();
const {
networkConditions,
cpuThrottlingRate,
geolocation,
userAgent,
viewport,
} = request.params;
if (networkConditions) {
if (networkConditions === 'No emulation') {
await page.emulateNetworkConditions(null);
context.setNetworkConditions(null);
} else if (networkConditions === 'Offline') {
await page.emulateNetworkConditions({
offline: true,
download: 0,
upload: 0,
latency: 0,
});
context.setNetworkConditions('Offline');
} else if (networkConditions in PredefinedNetworkConditions) {
const networkCondition =
PredefinedNetworkConditions[
networkConditions as keyof typeof PredefinedNetworkConditions
];
await page.emulateNetworkConditions(networkCondition);
context.setNetworkConditions(networkConditions);
}
}
if (cpuThrottlingRate) {
await page.emulateCPUThrottling(cpuThrottlingRate);
context.setCpuThrottlingRate(cpuThrottlingRate);
}
if (geolocation !== undefined) {
if (geolocation === null) {
await page.setGeolocation({latitude: 0, longitude: 0});
context.setGeolocation(null);
} else {
await page.setGeolocation(geolocation);
context.setGeolocation(geolocation);
}
}
if (userAgent !== undefined) {
if (userAgent === null) {
await page.setUserAgent({
userAgent: undefined,
});
context.setUserAgent(null);
} else {
await page.setUserAgent({
userAgent,
});
context.setUserAgent(userAgent);
}
}
if (viewport !== undefined) {
if (viewport === null) {
await page.setViewport(null);
context.setViewport(null);
} else {
const defaults = {
deviceScaleFactor: 1,
isMobile: false,
hasTouch: false,
isLandscape: false,
};
await page.setViewport({...defaults, ...viewport});
context.setViewport({...defaults, ...viewport});
}
}
},
});