|
2 | 2 | * @license |
3 | 3 | * Copyright 2025 Google LLC |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
5 | 6 | */ |
6 | 7 |
|
7 | 8 | import {zod, PredefinedNetworkConditions} from '../third_party/index.js'; |
@@ -55,10 +56,56 @@ export const emulate = defineTool({ |
55 | 56 | .describe( |
56 | 57 | 'Geolocation to emulate. Set to null to clear the geolocation override.', |
57 | 58 | ), |
| 59 | + userAgent: zod |
| 60 | + .string() |
| 61 | + .nullable() |
| 62 | + .optional() |
| 63 | + .describe( |
| 64 | + 'User agent to emulate. Set to null to clear the user agent override.', |
| 65 | + ), |
| 66 | + viewport: zod |
| 67 | + .object({ |
| 68 | + width: zod.number().int().min(0).describe('Page width in pixels.'), |
| 69 | + height: zod.number().int().min(0).describe('Page height in pixels.'), |
| 70 | + deviceScaleFactor: zod |
| 71 | + .number() |
| 72 | + .min(0) |
| 73 | + .optional() |
| 74 | + .describe('Specify device scale factor (can be thought of as dpr).'), |
| 75 | + isMobile: zod |
| 76 | + .boolean() |
| 77 | + .optional() |
| 78 | + .describe( |
| 79 | + 'Whether the meta viewport tag is taken into account. Defaults to false.', |
| 80 | + ), |
| 81 | + hasTouch: zod |
| 82 | + .boolean() |
| 83 | + .optional() |
| 84 | + .describe( |
| 85 | + 'Specifies if viewport supports touch events. This should be set to true for mobile devices.', |
| 86 | + ), |
| 87 | + isLandscape: zod |
| 88 | + .boolean() |
| 89 | + .optional() |
| 90 | + .describe( |
| 91 | + 'Specifies if viewport is in landscape mode. Defaults to false.', |
| 92 | + ), |
| 93 | + }) |
| 94 | + .nullable() |
| 95 | + .optional() |
| 96 | + .describe( |
| 97 | + 'Viewport to emulate. Set to null to reset to the default viewport.', |
| 98 | + ), |
58 | 99 | }, |
59 | 100 | handler: async (request, _response, context) => { |
60 | 101 | const page = context.getSelectedPage(); |
61 | | - const {networkConditions, cpuThrottlingRate, geolocation} = request.params; |
| 102 | + const { |
| 103 | + networkConditions, |
| 104 | + cpuThrottlingRate, |
| 105 | + geolocation, |
| 106 | + userAgent, |
| 107 | + viewport, |
| 108 | + } = request.params; |
62 | 109 |
|
63 | 110 | if (networkConditions) { |
64 | 111 | if (networkConditions === 'No emulation') { |
@@ -96,5 +143,35 @@ export const emulate = defineTool({ |
96 | 143 | context.setGeolocation(geolocation); |
97 | 144 | } |
98 | 145 | } |
| 146 | + |
| 147 | + if (userAgent !== undefined) { |
| 148 | + if (userAgent === null) { |
| 149 | + await page.setUserAgent({ |
| 150 | + userAgent: undefined, |
| 151 | + }); |
| 152 | + context.setUserAgent(null); |
| 153 | + } else { |
| 154 | + await page.setUserAgent({ |
| 155 | + userAgent, |
| 156 | + }); |
| 157 | + context.setUserAgent(userAgent); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if (viewport !== undefined) { |
| 162 | + if (viewport === null) { |
| 163 | + await page.setViewport(null); |
| 164 | + context.setViewport(null); |
| 165 | + } else { |
| 166 | + const defaults = { |
| 167 | + deviceScaleFactor: 1, |
| 168 | + isMobile: false, |
| 169 | + hasTouch: false, |
| 170 | + isLandscape: false, |
| 171 | + }; |
| 172 | + await page.setViewport({...defaults, ...viewport}); |
| 173 | + context.setViewport({...defaults, ...viewport}); |
| 174 | + } |
| 175 | + } |
99 | 176 | }, |
100 | 177 | }); |
0 commit comments