-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat(screenshot): add CLI options to cap screenshot size at the source #1823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
antoinekm
wants to merge
1
commit into
ChromeDevTools:main
Choose a base branch
from
antoinekm:feat/screenshot-size-cli-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+423
−98
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,101 +5,236 @@ | |
| */ | ||
|
|
||
| import {zod} from '../third_party/index.js'; | ||
| import type {ElementHandle, Page} from '../third_party/index.js'; | ||
| import type { | ||
| BoundingBox, | ||
| ElementHandle, | ||
| Page, | ||
| ScreenshotClip, | ||
| } from '../third_party/index.js'; | ||
|
|
||
| import {ToolCategory} from './categories.js'; | ||
| import {definePageTool} from './ToolDefinition.js'; | ||
|
|
||
| export const screenshot = definePageTool({ | ||
| name: 'take_screenshot', | ||
| description: `Take a screenshot of the page or element.`, | ||
| annotations: { | ||
| category: ToolCategory.DEBUGGING, | ||
| // Not read-only due to filePath param. | ||
| readOnlyHint: false, | ||
| }, | ||
| schema: { | ||
| format: zod | ||
| .enum(['png', 'jpeg', 'webp']) | ||
| .default('png') | ||
| .describe('Type of format to save the screenshot as. Default is "png"'), | ||
| quality: zod | ||
| .number() | ||
| .min(0) | ||
| .max(100) | ||
| .optional() | ||
| .describe( | ||
| 'Compression quality for JPEG and WebP formats (0-100). Higher values mean better quality but larger file sizes. Ignored for PNG format.', | ||
| ), | ||
| uid: zod | ||
| .string() | ||
| .optional() | ||
| .describe( | ||
| 'The uid of an element on the page from the page content snapshot. If omitted, takes a page screenshot.', | ||
| ), | ||
| fullPage: zod | ||
| .boolean() | ||
| .optional() | ||
| .describe( | ||
| 'If set to true takes a screenshot of the full page instead of the currently visible viewport. Incompatible with uid.', | ||
| type ScreenshotFormat = 'png' | 'jpeg' | 'webp'; | ||
|
|
||
| function isScreenshotFormat(value: unknown): value is ScreenshotFormat { | ||
| return value === 'png' || value === 'jpeg' || value === 'webp'; | ||
| } | ||
|
|
||
| function isPositiveFiniteNumber(value: number | undefined): value is number { | ||
| return value !== undefined && Number.isFinite(value) && value > 0; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's do number validation when parsing CLI args with yargs? I think here we should expect an integer number or undefined so we do not need to check again if the CLI args were parse. |
||
| } | ||
|
|
||
| async function getSourceBox( | ||
| page: Page, | ||
| element: ElementHandle | undefined, | ||
| fullPage: boolean, | ||
| ): Promise<BoundingBox | undefined> { | ||
| if (element) { | ||
| const box = await element.boundingBox(); | ||
| return box ?? undefined; | ||
| } | ||
| if (fullPage) { | ||
| const dims = await page.evaluate(() => ({ | ||
| width: Math.max( | ||
| document.documentElement.scrollWidth, | ||
| document.body?.scrollWidth ?? 0, | ||
| ), | ||
| filePath: zod | ||
| .string() | ||
| .optional() | ||
| .describe( | ||
| 'The absolute path, or a path relative to the current working directory, to save the screenshot to instead of attaching it to the response.', | ||
| height: Math.max( | ||
| document.documentElement.scrollHeight, | ||
| document.body?.scrollHeight ?? 0, | ||
| ), | ||
| }, | ||
| handler: async (request, response, context) => { | ||
| if (request.params.uid && request.params.fullPage) { | ||
| throw new Error('Providing both "uid" and "fullPage" is not allowed.'); | ||
| })); | ||
| if (dims.width <= 0 || dims.height <= 0) { | ||
| return undefined; | ||
| } | ||
| return {x: 0, y: 0, width: dims.width, height: dims.height}; | ||
| } | ||
| const viewport = page.viewport(); | ||
| if (!viewport) { | ||
| return undefined; | ||
| } | ||
| return {x: 0, y: 0, width: viewport.width, height: viewport.height}; | ||
| } | ||
|
|
||
| let pageOrHandle: Page | ElementHandle; | ||
| if (request.params.uid) { | ||
| pageOrHandle = await request.page.getElementByUid(request.params.uid); | ||
| } else { | ||
| pageOrHandle = request.page.pptrPage; | ||
| } | ||
| function computeDownscaleClip( | ||
| box: BoundingBox, | ||
| maxWidth: number | undefined, | ||
| maxHeight: number | undefined, | ||
| ): ScreenshotClip | undefined { | ||
| const widthScale = isPositiveFiniteNumber(maxWidth) | ||
| ? Math.min(1, maxWidth / box.width) | ||
| : 1; | ||
| const heightScale = isPositiveFiniteNumber(maxHeight) | ||
| ? Math.min(1, maxHeight / box.height) | ||
| : 1; | ||
| const scale = Math.min(widthScale, heightScale); | ||
| if (scale >= 1) { | ||
| return undefined; | ||
| } | ||
| // Skip degenerate sub-pixel results. | ||
| if (Math.round(box.width * scale) < 1 || Math.round(box.height * scale) < 1) { | ||
| return undefined; | ||
| } | ||
| return { | ||
| x: box.x, | ||
| y: box.y, | ||
| width: box.width, | ||
| height: box.height, | ||
| scale, | ||
| }; | ||
| } | ||
|
|
||
| const format = request.params.format; | ||
| const quality = format === 'png' ? undefined : request.params.quality; | ||
|
|
||
| const screenshot = await pageOrHandle.screenshot({ | ||
| type: format, | ||
| fullPage: request.params.fullPage, | ||
| quality, | ||
| optimizeForSpeed: true, // Bonus: optimize encoding for speed | ||
| }); | ||
|
|
||
| if (request.params.uid) { | ||
| response.appendResponseLine( | ||
| `Took a screenshot of node with uid "${request.params.uid}".`, | ||
| ); | ||
| } else if (request.params.fullPage) { | ||
| response.appendResponseLine( | ||
| 'Took a screenshot of the full current page.', | ||
| ); | ||
| } else { | ||
| response.appendResponseLine( | ||
| "Took a screenshot of the current page's viewport.", | ||
| ); | ||
| } | ||
| export const screenshot = definePageTool(args => { | ||
| const { | ||
| screenshotFormat, | ||
| screenshotQuality, | ||
| screenshotMaxWidth, | ||
| screenshotMaxHeight, | ||
| } = args ?? {}; | ||
|
|
||
| if (request.params.filePath) { | ||
| const file = await context.saveFile(screenshot, request.params.filePath); | ||
| response.appendResponseLine(`Saved screenshot to ${file.filename}.`); | ||
| } else if (screenshot.length >= 2_000_000) { | ||
| const {filepath} = await context.saveTemporaryFile( | ||
| screenshot, | ||
| `screenshot.${request.params.format}`, | ||
| ); | ||
| response.appendResponseLine(`Saved screenshot to ${filepath}.`); | ||
| } else { | ||
| response.attachImage({ | ||
| mimeType: `image/${request.params.format}`, | ||
| data: Buffer.from(screenshot).toString('base64'), | ||
| }); | ||
| } | ||
| }, | ||
| const defaultFormat: ScreenshotFormat = isScreenshotFormat(screenshotFormat) | ||
| ? screenshotFormat | ||
| : 'png'; | ||
| const defaultQuality = isPositiveFiniteNumber(screenshotQuality) | ||
| ? screenshotQuality | ||
| : undefined; | ||
| const maxWidth = isPositiveFiniteNumber(screenshotMaxWidth) | ||
| ? screenshotMaxWidth | ||
| : undefined; | ||
| const maxHeight = isPositiveFiniteNumber(screenshotMaxHeight) | ||
| ? screenshotMaxHeight | ||
| : undefined; | ||
|
|
||
| return { | ||
| name: 'take_screenshot', | ||
| description: `Take a screenshot of the page or element.`, | ||
| annotations: { | ||
| category: ToolCategory.DEBUGGING, | ||
| // Not read-only due to filePath param. | ||
| readOnlyHint: false, | ||
| }, | ||
| schema: { | ||
| format: zod | ||
| .enum(['png', 'jpeg', 'webp']) | ||
| .default(defaultFormat) | ||
| .describe( | ||
| `Type of format to save the screenshot as. Default is "${defaultFormat}"`, | ||
| ), | ||
| quality: zod | ||
| .number() | ||
| .min(0) | ||
| .max(100) | ||
| .optional() | ||
| .describe( | ||
| 'Compression quality for JPEG and WebP formats (0-100). Higher values mean better quality but larger file sizes. Ignored for PNG format.', | ||
| ), | ||
| uid: zod | ||
| .string() | ||
| .optional() | ||
| .describe( | ||
| 'The uid of an element on the page from the page content snapshot. If omitted, takes a page screenshot.', | ||
| ), | ||
| fullPage: zod | ||
| .boolean() | ||
| .optional() | ||
| .describe( | ||
| 'If set to true takes a screenshot of the full page instead of the currently visible viewport. Incompatible with uid.', | ||
| ), | ||
| filePath: zod | ||
| .string() | ||
| .optional() | ||
| .describe( | ||
| 'The absolute path, or a path relative to the current working directory, to save the screenshot to instead of attaching it to the response.', | ||
| ), | ||
| }, | ||
| handler: async (request, response, context) => { | ||
| if (request.params.uid && request.params.fullPage) { | ||
| throw new Error('Providing both "uid" and "fullPage" is not allowed.'); | ||
| } | ||
|
|
||
| const page = request.page.pptrPage; | ||
| const element = request.params.uid | ||
| ? await request.page.getElementByUid(request.params.uid) | ||
| : undefined; | ||
|
|
||
| const format = request.params.format; | ||
| const quality = | ||
| format === 'png' | ||
| ? undefined | ||
| : (request.params.quality ?? defaultQuality); | ||
| const fullPage = request.params.fullPage ?? false; | ||
|
|
||
| // Compute downscale clip when maxWidth/maxHeight is set and the source | ||
| // exceeds either bound. The smaller scale factor wins so both bounds | ||
| // are respected while preserving aspect ratio. | ||
| let clip: ScreenshotClip | undefined; | ||
| if (maxWidth !== undefined || maxHeight !== undefined) { | ||
| const box = await getSourceBox(page, element, fullPage); | ||
| if (box) { | ||
| clip = computeDownscaleClip(box, maxWidth, maxHeight); | ||
| } | ||
| } | ||
|
|
||
| let screenshot: Uint8Array; | ||
| if (clip) { | ||
| // page.screenshot with clip lets the CDP scale param downscale the | ||
| // capture for viewport, full-page and element shots alike. We rely on | ||
| // Puppeteer's default of captureBeyondViewport=true when a clip is | ||
| // present so element/full-page captures below the fold still work. | ||
| screenshot = await page.screenshot({ | ||
| type: format, | ||
| quality, | ||
| optimizeForSpeed: true, | ||
| clip, | ||
| }); | ||
| } else if (element) { | ||
| screenshot = await element.screenshot({ | ||
| type: format, | ||
| quality, | ||
| optimizeForSpeed: true, | ||
| }); | ||
| } else { | ||
| screenshot = await page.screenshot({ | ||
| type: format, | ||
| fullPage, | ||
| quality, | ||
| optimizeForSpeed: true, | ||
| }); | ||
| } | ||
|
|
||
| if (request.params.uid) { | ||
| response.appendResponseLine( | ||
| `Took a screenshot of node with uid "${request.params.uid}".`, | ||
| ); | ||
| } else if (fullPage) { | ||
| response.appendResponseLine( | ||
| 'Took a screenshot of the full current page.', | ||
| ); | ||
| } else { | ||
| response.appendResponseLine( | ||
| "Took a screenshot of the current page's viewport.", | ||
| ); | ||
| } | ||
|
|
||
| if (request.params.filePath) { | ||
| const file = await context.saveFile( | ||
| screenshot, | ||
| request.params.filePath, | ||
| ); | ||
| response.appendResponseLine(`Saved screenshot to ${file.filename}.`); | ||
| } else if (screenshot.length >= 2_000_000) { | ||
| const {filepath} = await context.saveTemporaryFile( | ||
| screenshot, | ||
| `screenshot.${request.params.format}`, | ||
| ); | ||
| response.appendResponseLine(`Saved screenshot to ${filepath}.`); | ||
| } else { | ||
| response.attachImage({ | ||
| mimeType: `image/${request.params.format}`, | ||
| data: Buffer.from(screenshot).toString('base64'), | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need isScreenshotFormat? MCP schema validates the format.