Skip to content

feat(browser): add --width / --height / --full-page flags to screenshot#1339

Merged
jackwener merged 1 commit intojackwener:mainfrom
Benjamin-eecs:feat/screenshot-width-fullpage
May 6, 2026
Merged

feat(browser): add --width / --height / --full-page flags to screenshot#1339
jackwener merged 1 commit intojackwener:mainfrom
Benjamin-eecs:feat/screenshot-width-fullpage

Conversation

@Benjamin-eecs
Copy link
Copy Markdown
Contributor

@Benjamin-eecs Benjamin-eecs commented May 5, 2026

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:

opencli browser screenshot --full-page --width 1080 out.png

Behavior:

  • --width W overrides device-metrics width; height is unchanged.
  • --height H overrides height. Ignored under --full-page; the existing captureBeyondViewport path is preserved when only --full-page (with or without --height) is set.
  • --full-page alone keeps the existing captureBeyondViewport path; no behavior change for the prior page.screenshot({ fullPage: true }) API.
  • --full-page --width W reflows at W first, then re-overrides to (W, contentH) from the post-reflow content size, then captures.
  • Override is always cleared in finally, including when capture throws.
  • --width / --height reject 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

  • 🐛 Bug fix
  • ✨ New feature
  • 🌐 New site adapter
  • 📝 Documentation
  • ♻️ Refactor
  • 🔧 CI / build / tooling

Checklist

  • I ran the checks relevant to this PR
  • I updated tests or docs if needed
  • I included output or screenshots when useful

Documentation (if adding/modifying an adapter)

  • Added doc page under docs/adapters/ (if new adapter)
  • Updated docs/adapters/index.md table (if new adapter)
  • Updated sidebar in docs/.vitepress/config.mts (if new adapter)
  • Updated README.md / README.zh-CN.md when command discoverability changed
  • Used positional args for the command's primary subject unless a named flag is clearly better
  • Normalized expected adapter failures to CliError subclasses instead of raw Error

Screenshots / Output

Validation UX:

$ opencli browser screenshot --width abc out.png
error: option '--width <n>' argument 'abc' is invalid. --width must be a positive integer (got "abc")
$ opencli browser screenshot --width 1080.5 out.png
error: option '--width <n>' argument '1080.5' is invalid. --width must be a positive integer (got "1080.5")
$ opencli browser screenshot --width 1080px out.png
error: option '--width <n>' argument '1080px' is invalid. --width must be a positive integer (got "1080px")

Tests (extension/src/cdp.test.ts 7 cases, src/browser/page.test.ts 2 cases):

  • default: no override
  • --width only: single override (width, 0), no Page.getLayoutMetrics
  • --height only: single override (0, height)
  • --full-page only: existing single override using cssContentSize
  • --full-page --height: height ignored; existing measure-from-content path preserved
  • --full-page --width: two overrides (reflow then measure) with Page.getLayoutMetrics between
  • cleared in finally when Page.captureScreenshot throws
  • Page.screenshot({ fullPage, width }) forwards new fields; omitted when unset
Test Files  71 passed (71)
     Tests  1011 passed | 1 skipped (1012)

End-to-end via CDPBridge against headless Chrome at --remote-debugging-port=9222:

Command Result
baseline (no flags) 1280x633
--width 1080 1080x633
--full-page (example.com) 1280x633
--full-page --width 1080 (example.com) 1080x633
--full-page --width 1080 (news.ycombinator.com) 1080x1246
--full-page --height 600 (news.ycombinator.com) 1280x1246 (height ignored)

Copilot AI review requested due to automatic review settings May 5, 2026 19:21
@Benjamin-eecs Benjamin-eecs force-pushed the feat/screenshot-width-fullpage branch from 4aa1d7f to 4ce295a Compare May 5, 2026 19:22
@Benjamin-eecs Benjamin-eecs changed the title feat(browser): add --width / --height / --full-page flags to screenshot (closes #1334) feat(browser): add --width / --height / --full-page flags to screenshot May 5, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-page flags to opencli browser screenshot, plus new width/height fields 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 thread src/cli.ts
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 thread src/browser/cdp.ts Outdated
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.
@Benjamin-eecs Benjamin-eecs force-pushed the feat/screenshot-width-fullpage branch from 4ce295a to d53bd78 Compare May 5, 2026 19:30
@jackwener jackwener merged commit 4e92d71 into jackwener:main May 6, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: 精确截图能力 — setViewportSize + full-page screenshot

3 participants