@@ -410,8 +410,6 @@ const pngSize = (base64: string) => {
410410 return { width : header . readUInt32BE ( 16 ) , height : header . readUInt32BE ( 20 ) } ;
411411} ;
412412
413- type Viewport = { readonly width : number ; readonly height : number } ;
414-
415413/**
416414 * A page's display ratio and CSS viewport, read in an isolated world since page script can
417415 * reassign `devicePixelRatio`, `innerWidth`, and `innerHeight` in its own, and its page zoom.
@@ -438,55 +436,64 @@ const readPageMetrics = async (cdp: CDPSession) => {
438436 readonly width : number ;
439437 readonly height : number ;
440438 } ;
441- return {
442- ratio,
443- width,
444- height,
445- zoom,
446- offset : { x : cssVisualViewport . pageX * zoom , y : cssVisualViewport . pageY * zoom } ,
447- } ;
439+ return { ratio, width, height, zoom } ;
448440 }
449441 }
450442 throw new Error (
451443 "The page's zoom kept changing while the snapshot read its size. Try the snapshot again." ,
452444 ) ;
453445} ;
454446
455- // Scaled captures repaint live screencasts, so callers pause them. Clips use document offsets.
447+ // Never pass a clip scale: Chromium mirrors the top-left of the page into a `backdrop-filter`
448+ // in any capture it shrinks.
456449export const captureViewport = async (
457- page : Page ,
458450 cdp : CDPSession ,
459- options : {
460- readonly format : "png" | "jpeg" ;
461- readonly quality ?: number ;
462- readonly scale : number ;
463- readonly viewport ?: Viewport ;
464- readonly offset ?: { readonly x : number ; readonly y : number } | undefined ;
465- } ,
451+ options : { readonly format : "png" | "jpeg" ; readonly quality ?: number } ,
466452) => {
467- let clip ;
468- if ( options . scale < 1 ) {
469- const viewport = options . viewport ?? page . viewportSize ( ) ?? { width : 1280 , height : 800 } ;
470- let offset = options . offset ;
471- if ( ! offset ) {
472- const { cssVisualViewport } = await cdp . send ( "Page.getLayoutMetrics" ) ;
473- const zoom = cssVisualViewport . zoom ?? 1 ;
474- offset = { x : cssVisualViewport . pageX * zoom , y : cssVisualViewport . pageY * zoom } ;
475- }
476- clip = {
477- ...offset ,
478- ...viewport ,
479- scale : options . scale ,
480- } ;
481- }
482453 const { data } = await cdp . send ( "Page.captureScreenshot" , {
483454 format : options . format ,
484455 ...( options . quality === undefined ? { } : { quality : options . quality } ) ,
485- ...( clip ? { clip } : { } ) ,
486456 } ) ;
487457 return data ;
488458} ;
489459
460+ /** Runs in the page's own isolated world, as the server has no image library. */
461+ const SHRINK_PNG = `async function (png, width, height) {
462+ const bytes = Uint8Array.from(atob(png), (char) => char.charCodeAt(0));
463+ const bitmap = await createImageBitmap(new Blob([bytes], { type: "image/png" }));
464+ // Namespaced, as an XML document's createElement makes a plain element with no getContext.
465+ const canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
466+ canvas.width = width;
467+ canvas.height = height;
468+ const context = canvas.getContext("2d");
469+ context.imageSmoothingQuality = "high";
470+ context.drawImage(bitmap, 0, 0, width, height);
471+ bitmap.close();
472+ return canvas.toDataURL("image/png").slice("data:image/png;base64,".length);
473+ }` ;
474+
475+ const shrinkPng = async ( cdp : CDPSession , png : string , scale : number ) => {
476+ const size = pngSize ( png ) ;
477+ const { frameTree } = await cdp . send ( "Page.getFrameTree" ) ;
478+ const { executionContextId } = await cdp . send ( "Page.createIsolatedWorld" , {
479+ frameId : frameTree . frame . id ,
480+ worldName : "t3-preview-screenshot" ,
481+ } ) ;
482+ const { result, exceptionDetails } = await cdp . send ( "Runtime.callFunctionOn" , {
483+ functionDeclaration : SHRINK_PNG ,
484+ executionContextId,
485+ arguments : [
486+ { value : png } ,
487+ { value : Math . round ( size . width * scale ) } ,
488+ { value : Math . round ( size . height * scale ) } ,
489+ ] ,
490+ awaitPromise : true ,
491+ returnByValue : true ,
492+ } ) ;
493+ if ( exceptionDetails ) throw new Error ( "The snapshot could not shrink its screenshot." ) ;
494+ return result . value as string ;
495+ } ;
496+
490497type SnapshotInput = {
491498 readonly page : Page ;
492499 readonly cdp : CDPSession ;
@@ -497,17 +504,16 @@ type SnapshotInput = {
497504 readonly actionTimeline : PreviewAutomationSnapshot [ "actionTimeline" ] ;
498505} ;
499506
500- /** The scale a page renders at and its viewport in device-independent pixels, a clip's units . */
507+ /** The scale a page renders at and its viewport in device-independent pixels. */
501508const renderingOf = async ( input : SnapshotInput ) => {
502509 if ( input . renderScale !== "display" ) {
503510 const viewport = input . page . viewportSize ( ) ?? { width : 1280 , height : 800 } ;
504- return { renderScale : input . renderScale , viewport, offset : undefined } ;
511+ return { renderScale : input . renderScale , viewport } ;
505512 }
506513 // The display ratio a desktop page reports includes its page zoom, and its sizes are CSS pixels.
507514 const page = await readPageMetrics ( input . cdp ) ;
508515 return {
509516 renderScale : page . ratio / page . zoom ,
510- offset : page . offset ,
511517 viewport : {
512518 width : Math . round ( page . width * page . zoom ) ,
513519 height : Math . round ( page . height * page . zoom ) ,
@@ -519,7 +525,7 @@ const capture = async (
519525 input : SnapshotInput ,
520526 deadline : number ,
521527) : Promise < PreviewAutomationSnapshot > => {
522- const { renderScale, viewport, offset } = await renderingOf ( input ) ;
528+ const { renderScale, viewport } = await renderingOf ( input ) ;
523529 // A late metrics reply must not begin a capture after viewers have resumed.
524530 if ( Date . now ( ) >= deadline ) throw tooBusy ( ) ;
525531 const scale = Math . min ( 1 , MAX_SCREENSHOT_WIDTH / ( viewport . width * renderScale ) ) ;
@@ -536,7 +542,9 @@ const capture = async (
536542 "url" | "title" | "loading" | "visibleText" | "interactiveElements"
537543 >
538544 > ,
539- captureViewport ( input . page , input . cdp , { format : "png" , scale, viewport, offset } ) ,
545+ captureViewport ( input . cdp , { format : "png" } ) . then ( ( png ) =>
546+ scale < 1 ? shrinkPng ( input . cdp , png , scale ) : png ,
547+ ) ,
540548 ] ) ;
541549 void pageAndScreenshot . catch ( constVoid ) ;
542550 // Leaves time for the second read; a frame that never loads holds the tree until then.
0 commit comments