feat(browser): add --width / --height / --full-page flags to screenshot#1339
Merged
jackwener merged 1 commit intojackwener:mainfrom May 6, 2026
Merged
Conversation
4aa1d7f to
4ce295a
Compare
There was a problem hiding this comment.
Pull request overview
Adds CLI and bridge-layer support for overriding viewport dimensions when taking browser screenshots, enabling fixed-width and/or fixed-height renders (including “fixed width + full-page height”) via CDP device-metrics overrides.
Changes:
- Add
--width,--height, and--full-pageflags toopencli browser screenshot, plus newwidth/heightfields in the cross-layer command/types. - Implement screenshot dimension override logic (including “reflow at width, then measure content height” for
fullPage + width) in both the extension CDP path and the direct CDP bridge. - Add unit tests covering override behavior and cleanup on failure.
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/types.ts | Extends ScreenshotOptions with optional width/height viewport overrides. |
| src/cli.ts | Adds new screenshot flags and argument parsing/validation helper. |
| src/browser/page.ts | Forwards width/height through the daemon command payload for screenshots. |
| src/browser/page.test.ts | Adds tests verifying forwarding/omission behavior for screenshot options. |
| src/browser/daemon-client.ts | Extends DaemonCommand with width/height for screenshot action payloads. |
| src/browser/cdp.ts | Implements direct-CDP screenshot overrides and ensures override cleanup in finally. |
| extension/src/protocol.ts | Extends extension command protocol with width/height fields. |
| extension/src/cdp.ts | Implements extension-side screenshot overrides (including reflow-before-measure for fullPage + width). |
| extension/src/cdp.test.ts | Adds tests for override combinations and cleanup on screenshot failure. |
| extension/src/background.ts | Passes cmd.width/cmd.height through to the extension CDP screenshot executor. |
| extension/dist/background.js | Rebuilt dist output reflecting extension screenshot override changes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+459
to
+464
| function parseScreenshotDim(val: string, label: string): number { | ||
| const parsed = parseInt(val, 10); | ||
| if (Number.isNaN(parsed) || parsed <= 0) { | ||
| throw new InvalidArgumentError(`--${label} must be a positive integer (got "${val}")`); | ||
| } | ||
| return parsed; |
Comment on lines
+247
to
+251
| const overrideWidth = options.width && options.width > 0 ? Math.ceil(options.width) : undefined; | ||
| const overrideHeight = options.height && options.height > 0 ? Math.ceil(options.height) : undefined; | ||
| const fullPage = options.fullPage === true; | ||
| const needsOverride = overrideWidth !== undefined || overrideHeight !== undefined; | ||
|
|
Closes jackwener#1334. Exposes viewport overrides for `opencli browser screenshot` so an adapter or ad-hoc shell user can render a page at a fixed width and capture the full scrollable height. The ljg-card HTML to PNG pipeline use case. Behavior: - `--width W` only overrides device-metrics width; height is left unchanged. - `--height H` only overrides height (ignored under `--full-page`). - `--full-page` keeps the existing `captureBeyondViewport` shortcut. - `--full-page --width W` first reflows at W, then re-overrides to (W, contentH) so the captured image reflects the layout at the requested width. - Override is always cleared in `finally`, including on capture failure.
4ce295a to
d53bd78
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
The CDP layer already supports viewport overrides for screenshots; this exposes them at the CLI.
The ljg-card HTML to PNG use case (fixed 1080px width, full content height) becomes a one-liner:
Behavior:
--width Woverrides device-metrics width; height is unchanged.--height Hoverrides height. Ignored under--full-page; the existingcaptureBeyondViewportpath is preserved when only--full-page(with or without--height) is set.--full-pagealone keeps the existingcaptureBeyondViewportpath; no behavior change for the priorpage.screenshot({ fullPage: true })API.--full-page --width Wreflows at W first, then re-overrides to(W, contentH)from the post-reflow content size, then captures.finally, including when capture throws.--width/--heightreject non-integer input (1080px,1080.5,-1,0,abc) via a strict^\d+$check.Compatibility: new fields are optional, so old extension on a new CLI silently falls back to current behavior; new flags require the extension built from this PR. Manifest version bump left to a release PR (matches #1278 / #1259 / #1266 / #1320).
Related issue: closes #1334.
Type of Change
Checklist
Documentation (if adding/modifying an adapter)
docs/adapters/(if new adapter)docs/adapters/index.mdtable (if new adapter)docs/.vitepress/config.mts(if new adapter)README.md/README.zh-CN.mdwhen command discoverability changedCliErrorsubclasses instead of rawErrorScreenshots / Output
Validation UX:
Tests (
extension/src/cdp.test.ts7 cases,src/browser/page.test.ts2 cases):--widthonly: single override(width, 0), noPage.getLayoutMetrics--heightonly: single override(0, height)--full-pageonly: existing single override usingcssContentSize--full-page --height: height ignored; existing measure-from-content path preserved--full-page --width: two overrides (reflow then measure) withPage.getLayoutMetricsbetweenfinallywhenPage.captureScreenshotthrowsPage.screenshot({ fullPage, width })forwards new fields; omitted when unsetEnd-to-end via
CDPBridgeagainst headless Chrome at--remote-debugging-port=9222:--width 1080--full-page(example.com)--full-page --width 1080(example.com)--full-page --width 1080(news.ycombinator.com)--full-page --height 600(news.ycombinator.com)