Skip to content

Chrome DevTools for coding agents

fix: warn when screenshot dimensions exceed 16384px limit (#2952) (#3291)

Yuri (IURII) Trukhin10455b7820b7
diff --git a/src/tools/screenshot.ts b/src/tools/screenshot.ts
index d8d310ea..811dfe32 100644
--- a/src/tools/screenshot.ts
+++ b/src/tools/screenshot.ts
@@ -18,6 +18,8 @@ import {definePageTool, type PageToolFactory} from './ToolDefinition.js';
type ScreenshotFormat = 'png' | 'jpeg' | 'webp';
+const MAX_CHROMIUM_SCREENSHOT_DIMENSION = 16384;
+
type SourceBox = BoundingBox & {
devicePixelRatio: number;
};
@@ -225,18 +227,26 @@ export const screenshot: PageToolFactory = definePageTool(
// The smaller scale factor wins so both bounds are respected while
// preserving aspect ratio.
let clip: ScreenshotClip | undefined;
- if (
+ const shouldInspectBox =
+ fullPage ||
+ element !== undefined ||
screenshotMaxWidth !== undefined ||
- screenshotMaxHeight !== undefined
+ screenshotMaxHeight !== undefined;
+
+ const box = shouldInspectBox
+ ? await getSourceBox(page, element, fullPage)
+ : undefined;
+
+ if (
+ box &&
+ (screenshotMaxWidth !== undefined ||
+ screenshotMaxHeight !== undefined)
) {
- const box = await getSourceBox(page, element, fullPage);
- if (box) {
- clip = computeDownscaleClip(
- box,
- screenshotMaxWidth,
- screenshotMaxHeight,
- );
- }
+ clip = computeDownscaleClip(
+ box,
+ screenshotMaxWidth,
+ screenshotMaxHeight,
+ );
}
if (clip) {
@@ -306,6 +316,22 @@ export const screenshot: PageToolFactory = definePageTool(
data: Buffer.from(screenshot).toString('base64'),
});
}
+
+ if (box) {
+ const effectiveScale = clip?.scale ?? 1;
+ const effectiveWidth =
+ box.width * box.devicePixelRatio * effectiveScale;
+ const effectiveHeight =
+ box.height * box.devicePixelRatio * effectiveScale;
+ if (
+ effectiveWidth > MAX_CHROMIUM_SCREENSHOT_DIMENSION ||
+ effectiveHeight > MAX_CHROMIUM_SCREENSHOT_DIMENSION
+ ) {
+ response.appendResponseLine(
+ `Warning: Screenshot dimensions exceed the maximum capturable dimension of ${MAX_CHROMIUM_SCREENSHOT_DIMENSION}px. Content beyond 16384px may repeat or be corrupted due to browser texture limits. Set --screenshotMaxHeight or --screenshotMaxWidth to downscale.`,
+ );
+ }
+ }
},
};
},
diff --git a/tests/tools/screenshot.test.ts b/tests/tools/screenshot.test.ts
index 6d48254a..ff9b4eee 100644
--- a/tests/tools/screenshot.test.ts
+++ b/tests/tools/screenshot.test.ts
@@ -160,7 +160,50 @@ describe('screenshot', () => {
quality: undefined,
optimizeForSpeed: true,
});
- sinon.assert.notCalled(pptrPage.screenshot);
+ });
+
+ it('warns when fullPage screenshot exceeds 16384px without downscaling', async () => {
+ const {page, context, response, args} = createHandlerMocks({});
+ const pptrPage = page.pptrPage;
+ pptrPage.evaluate.resolves({
+ width: 1200,
+ height: 20000,
+ devicePixelRatio: 1,
+ });
+ pptrPage.screenshot.resolves(new Uint8Array());
+
+ await screenshot(args).handler(
+ {params: {format: 'png', fullPage: true}, page},
+ response,
+ context,
+ );
+
+ sinon.assert.calledWithMatch(
+ response.appendResponseLine,
+ sinon.match(/exceed.*16384/i),
+ );
+ });
+
+ it('does not warn when fullPage screenshot is within 16384px', async () => {
+ const {page, context, response, args} = createHandlerMocks({});
+ const pptrPage = page.pptrPage;
+ pptrPage.evaluate.resolves({
+ width: 1200,
+ height: 5000,
+ devicePixelRatio: 1,
+ });
+ pptrPage.screenshot.resolves(new Uint8Array());
+
+ await screenshot(args).handler(
+ {params: {format: 'png', fullPage: true}, page},
+ response,
+ context,
+ );
+
+ sinon.assert.neverCalledWithMatch(
+ response.appendResponseLine,
+ sinon.match(/exceed.*16384/i),
+ );
});
});