render: PNG screenshots via CLI and CDP

End-to-end rasterization behind the render feature (which now enables
obscura-render/paint):

- obscura-render: screenshot_png(dom, viewport) paints and encodes to PNG
  via tiny-skia's encode_png (no extra dep).
- obscura-browser: Page::screenshot renders the live DOM at a viewport.
- CLI: obscura fetch --screenshot out.png writes the settled page to a PNG.
- CDP: Page.captureScreenshot returns the PNG base64-encoded, so
  Puppeteer/Playwright page.screenshot() works. captureSnapshot still
  errors (it is a DOM/layer snapshot, not an image).

Verified: both paths produce correct 1280x720 RGBA PNGs with elements
laid out and colored as the CSS specifies (block stacking, backgrounds,
white page). Default build unchanged; obstacle course 33/33.

Text, borders, and images are not yet painted; the box layout and
background colors are rendered.
This commit is contained in:
SGavrl committed 2026-07-01 21:34:15 +00:00
1 parent 4ca85e87d8
commit cdd52ade43
9 files changed
+92 -12

No files matched your search

+25 -5
View File
@@ -524,11 +524,31 @@ pub async fn handle(
.to_string(),
)
}
"captureScreenshot" | "captureSnapshot" => {
// Same story as printToPDF: rasterising a page needs a layout and
// paint pipeline that Obscura intentionally does not have. Reply
// with a clear error so clients can fail fast instead of waiting
// on the generic "Unknown Page method" reply.
"captureScreenshot" => {
#[cfg(feature = "render")]
{
let page = ctx.get_session_page(session_id).ok_or("No page for session")?;
match page.screenshot((1280.0, 720.0)) {
Some(png) => {
use base64::Engine as _;
let data = base64::engine::general_purpose::STANDARD.encode(&png);
Ok(json!({ "data": data }))
}
None => Err(
"Page.captureScreenshot failed: the page has no DOM to render"
.to_string(),
),
}
}
#[cfg(not(feature = "render"))]
Err(
"Page.captureScreenshot requires a build with the render feature"
.to_string(),
)
}
"captureSnapshot" => {
// A DOM/layer-tree snapshot (not a raster image). Distinct from
// captureScreenshot; keep the clear error so clients fail fast.
Err(format!(
"Page.{method} is not supported by Obscura: no layout or paint engine. \
For visual snapshots, drive a real headless Chromium for the \