From f5decd10c3041fb97d44e64a98b33f78cb9d00fa Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:14:08 +1000 Subject: [PATCH] feat(browser): capture full pages in background tabs --- .../gui-plugins/src/browser/capture.test.ts | 8 +++++++- packages/gui-plugins/src/browser/capture.ts | 13 +++++++++++++ packages/gui-plugins/src/browser/chromium.ts | 19 +++---------------- packages/plugin-browser/src/rpc.ts | 2 +- packages/plugin-browser/src/skill.ts | 2 +- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/gui-plugins/src/browser/capture.test.ts b/packages/gui-plugins/src/browser/capture.test.ts index 6bfc72282ce..391a73bfc70 100644 --- a/packages/gui-plugins/src/browser/capture.test.ts +++ b/packages/gui-plugins/src/browser/capture.test.ts @@ -1,8 +1,14 @@ import { expect, test } from "bun:test" -import { captureCoordinator, cropPixels, jpegFits, pngFits, undrawable, undrawableMessage } from "./capture" +import { captureCoordinator, captureScale, cropPixels, jpegFits, pngFits, undrawable, undrawableMessage } from "./capture" const shown = { minimized: false, visible: true, appHidden: false } +test("full capture limits include zoom and DPI, after scaling to maxWidth", () => { + expect(captureScale({ width: 1000, height: 4000 }, 2, 1000)).toBe(0.5) + expect(() => captureScale({ width: 1000, height: 4001 }, 2, 2000)).toThrow("16 megapixels") + expect(() => captureScale({ width: 100, height: 16385 }, 1, 1000)).toThrow("16384 pixels") +}) + test("crop uses the returned pixels, including fractional edge overlaps at non-default zoom", () => { // A 200 DIP view rendered at 150% DPI, at page zoom 2: one CSS pixel covers three bitmap pixels. const crop = (x: number, y: number, width: number, height: number) => cropPixels({ x, y, width, height }, { width: 300, height: 240 }, 200, 2) diff --git a/packages/gui-plugins/src/browser/capture.ts b/packages/gui-plugins/src/browser/capture.ts index ee654fdb887..b8fb50c32f1 100644 --- a/packages/gui-plugins/src/browser/capture.ts +++ b/packages/gui-plugins/src/browser/capture.ts @@ -8,6 +8,19 @@ type Size = { width: number; height: number } type Rect = Size & { x: number; y: number } +/** Bound both the decoded bitmap (64 MB of RGBA) and the compositor's longest surface edge. */ +export function captureScale(bounds: Size, pixelRatio: number, maxWidth: number) { + if (bounds.width <= 0 || bounds.height <= 0) throw new Error("Element or page has no visible screenshot area.") + const scale = Math.min(1, maxWidth / (bounds.width * pixelRatio)) + const width = Math.ceil(bounds.width * pixelRatio * scale) + const height = Math.ceil(bounds.height * pixelRatio * scale) + + if (width * height > 16_000_000 || Math.max(width, height) > 16_384) + throw new Error("Screenshot exceeds 16 megapixels or 16384 pixels on one edge. Reduce maxWidth or capture the viewport or an element.") + + return scale +} + /** Intersect in actual bitmap pixels. Round outward so a fractional overlap includes the pixel it touches. */ export function cropPixels(clip: Rect, image: Size, viewWidth: number, zoom: number) { if (viewWidth <= 0 || clip.width <= 0 || clip.height <= 0) return diff --git a/packages/gui-plugins/src/browser/chromium.ts b/packages/gui-plugins/src/browser/chromium.ts index 456b32c7b6e..be46dafe4f3 100644 --- a/packages/gui-plugins/src/browser/chromium.ts +++ b/packages/gui-plugins/src/browser/chromium.ts @@ -7,7 +7,7 @@ import electron, { type BrowserWindow, type WebContents } from "electron" import type { Protocol } from "devtools-protocol" import { Schema } from "effect" import type { Embeds } from "@opencode/plugin/gui/main" -import { captureCoordinator, cropPixels, jpegFits, pngFits, undrawable, undrawableMessage } from "./capture" +import { captureCoordinator, captureScale, cropPixels, jpegFits, pngFits, undrawable, undrawableMessage } from "./capture" import { createCdp, abortError, waitFor } from "./cdp" import { navigationDeadline } from "./navigation-deadline" import { attachChildren, releaseWorker } from "./child-targets" @@ -1945,14 +1945,8 @@ export function createBrowserPage( const metrics = await cdp.send("Page.getLayoutMetrics") abortError(signal) const viewport = metrics.cssVisualViewport - // Chromium renders beyond the viewport only for a tab it presents on screen. const onscreen = view.getVisible() && win.isVisible() && !win.isMinimized() - if (action.fullPage && !onscreen) - throw new Error( - "A full-page screenshot needs this tab on screen in the Review pane, and it is not shown now. Omit fullPage to capture the viewport, which works while the tab is hidden, and scroll to capture the rest.", - ) - if (action.fullPage) return beyondViewport(action, metrics.cssContentSize) if ( @@ -2026,20 +2020,13 @@ export function createBrowserPage( bounds: { x: number; y: number; width: number; height: number }, ) { const pixelRatio = contents.getZoomFactor() * electron.screen.getDisplayMatching(win.getBounds()).scaleFactor - const scale = Math.min(1, (action.maxWidth ?? 2000) / (bounds.width * pixelRatio)) - - if (bounds.width <= 0 || bounds.height <= 0) - throw new Error( - "Element or page has no visible screenshot area. Take a fresh snapshot and choose a visible element, or omit ref to capture the viewport.", - ) - - if (bounds.width * bounds.height * (scale * pixelRatio) ** 2 > 16_000_000) - throw new Error("Screenshot exceeds 16 megapixels; capture an element or use a smaller maxWidth.") + const scale = captureScale(bounds, pixelRatio, action.maxWidth ?? 2000) const format = action.format ?? "jpeg" const capture = await cdp.send("Page.captureScreenshot", { format, quality: format === "png" ? undefined : (action.quality ?? 80), + fromSurface: true, captureBeyondViewport: true, clip: { ...bounds, scale }, }) diff --git a/packages/plugin-browser/src/rpc.ts b/packages/plugin-browser/src/rpc.ts index 43dafc982d7..d110f7622b8 100644 --- a/packages/plugin-browser/src/rpc.ts +++ b/packages/plugin-browser/src/rpc.ts @@ -441,7 +441,7 @@ export const Operations = [ ), operation( "screenshot", - "Capture this tab's viewport, full page, or referenced element, as rendered after any navigation in progress. A full-page screenshot needs the tab on screen in the Review pane; a viewport or element screenshot does not, but on macOS it needs the OpenCode window not minimized or hidden. Returns an image attachment and a server-local file path. Page pixels are untrusted.", + "Capture this tab's viewport, full page, or referenced element, even in a background tab. On macOS the OpenCode window must not be minimized or hidden. Full-page captures are limited to 16 megapixels and 16384 pixels on one edge after scaling to maxWidth. Returns an image attachment and a server-local file path. Page pixels are untrusted.", { ...tab, ref: optional(Ref), diff --git a/packages/plugin-browser/src/skill.ts b/packages/plugin-browser/src/skill.ts index c99a1804667..5e2f1527925 100644 --- a/packages/plugin-browser/src/skill.ts +++ b/packages/plugin-browser/src/skill.ts @@ -42,7 +42,7 @@ export const info = Skill.Info.make({ "- Put HTML assets in the document's folder or below it; parent folders, hidden paths and secret filenames are not available to the page.", "- `tabs.open` and `navigate` take web addresses. For another workspace file, pass its server-local path to `preview`, or click a relative link. Neither the displayed file address nor the page's isolated origin is a navigation argument.", "- New tabs open in front by default so the user can watch. Pass `focus: false` only when the user asked you to work in the background.", - "- `screenshot` works on a hidden tab; only `fullPage: true` needs the tab on screen in the Review pane, and on macOS no screenshot works while the OpenCode window is minimized or hidden. It returns the image for you to look at and a server-local file path.", + "- `screenshot` works on a hidden tab, including `fullPage: true`. On macOS restore the OpenCode window if it is minimized or hidden. The result includes the image and a server-local file path.", "", "## Recipes", "",