feat(browser): clamp screenshots under 5MB
This commit is contained in:
@@ -81,6 +81,19 @@ async function fetchJson<T>(url: string, timeoutMs = 1500): Promise<T> {
|
||||
export async function captureScreenshotPng(opts: {
|
||||
wsUrl: string;
|
||||
fullPage?: boolean;
|
||||
}): Promise<Buffer> {
|
||||
return await captureScreenshot({
|
||||
wsUrl: opts.wsUrl,
|
||||
fullPage: opts.fullPage,
|
||||
format: "png",
|
||||
});
|
||||
}
|
||||
|
||||
export async function captureScreenshot(opts: {
|
||||
wsUrl: string;
|
||||
fullPage?: boolean;
|
||||
format?: "png" | "jpeg";
|
||||
quality?: number; // jpeg only (0..100)
|
||||
}): Promise<Buffer> {
|
||||
const ws = new WebSocket(opts.wsUrl, { handshakeTimeout: 5000 });
|
||||
const { send, closeWithError } = createCdpSender(ws);
|
||||
@@ -110,8 +123,15 @@ export async function captureScreenshotPng(opts: {
|
||||
}
|
||||
}
|
||||
|
||||
const format = opts.format ?? "png";
|
||||
const quality =
|
||||
format === "jpeg"
|
||||
? Math.max(0, Math.min(100, Math.round(opts.quality ?? 85)))
|
||||
: undefined;
|
||||
|
||||
const result = (await send("Page.captureScreenshot", {
|
||||
format: "png",
|
||||
format,
|
||||
...(quality !== undefined ? { quality } : {}),
|
||||
fromSurface: true,
|
||||
captureBeyondViewport: true,
|
||||
...(clip ? { clip } : {}),
|
||||
|
||||
Reference in New Issue
Block a user