Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

feat(cli): add screenshot_full dump mode

ldm0be5449435a6f
diff --git a/README.md b/README.md
index 41f1ff3ea..00630259a 100644
--- a/README.md
+++ b/README.md
@@ -106,10 +106,11 @@ moli fetch \
https://example.com
```
-For visual output, enable on-demand layout and write either a viewport PNG screenshot or a paginated PDF:
+For visual output, enable on-demand layout and write a viewport PNG, a full-document PNG, or a paginated PDF:
```bash
moli fetch --layout --dump screenshot https://example.com > page.png
+moli fetch --layout --dump screenshot_full https://example.com > full-page.png
moli fetch --layout --dump pdf https://example.com > page.pdf
```
diff --git a/docs/README.de.md b/docs/README.de.md
index e48e06595..176715d67 100644
--- a/docs/README.de.md
+++ b/docs/README.de.md
@@ -103,10 +103,11 @@ moli fetch \
https://example.com
```
-Für visuelle Ausgaben aktivierst du das On-Demand-Layout und erzeugst wahlweise einen PNG-Screenshot des Viewports oder direkt ein mehrseitiges PDF:
+Für visuelle Ausgaben aktivierst du das On-Demand-Layout und erzeugst einen PNG-Screenshot des Viewports, einen PNG-Screenshot des vollständigen Dokuments oder ein mehrseitiges PDF:
```bash
moli fetch --layout --dump screenshot https://example.com > page.png
+moli fetch --layout --dump screenshot_full https://example.com > full-page.png
moli fetch --layout --dump pdf https://example.com > page.pdf
```
diff --git a/docs/README.es.md b/docs/README.es.md
index 863e0c80a..7d17c59e6 100644
--- a/docs/README.es.md
+++ b/docs/README.es.md
@@ -103,10 +103,11 @@ moli fetch \
https://example.com
```
-Para obtener una salida visual, activa el layout bajo demanda y genera directamente una captura PNG del viewport o un PDF paginado:
+Para obtener una salida visual, activa el layout bajo demanda y genera una captura PNG del viewport, una captura PNG del documento completo o un PDF paginado:
```bash
moli fetch --layout --dump screenshot https://example.com > page.png
+moli fetch --layout --dump screenshot_full https://example.com > full-page.png
moli fetch --layout --dump pdf https://example.com > page.pdf
```
diff --git a/docs/README.fr.md b/docs/README.fr.md
index e4df829b2..165232fe4 100644
--- a/docs/README.fr.md
+++ b/docs/README.fr.md
@@ -103,10 +103,11 @@ moli fetch \
https://example.com
```
-Pour une sortie visuelle, activez la mise en page à la demande afin de générer directement une capture PNG du viewport ou un PDF paginé :
+Pour une sortie visuelle, activez la mise en page à la demande afin de générer une capture PNG du viewport, une capture PNG du document complet ou un PDF paginé :
```bash
moli fetch --layout --dump screenshot https://example.com > page.png
+moli fetch --layout --dump screenshot_full https://example.com > full-page.png
moli fetch --layout --dump pdf https://example.com > page.pdf
```
diff --git a/docs/README.ja.md b/docs/README.ja.md
index 28ea96ea4..fa61b7d68 100644
--- a/docs/README.ja.md
+++ b/docs/README.ja.md
@@ -102,10 +102,11 @@ moli fetch \
https://example.com
```
-画面出力を行う場合は、レイアウト計算を有効にすると、表示領域の PNG 画像(スクリーンショット)や、ページ分割した PDF を直接生成できます。
+画面出力を行う場合は、レイアウト計算を有効にすると、表示領域の PNG 画像、ページ全体の PNG 画像、またはページ分割した PDF を直接生成できます。
```bash
moli fetch --layout --dump screenshot https://example.com > page.png
+moli fetch --layout --dump screenshot_full https://example.com > full-page.png
moli fetch --layout --dump pdf https://example.com > page.pdf
```
diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md
index 51e22cd24..96ef800ca 100644
--- a/docs/README.zh-CN.md
+++ b/docs/README.zh-CN.md
@@ -102,10 +102,11 @@ moli fetch \
https://example.com
```
-如果需要视觉输出,可以启用按需布局,直接生成视口 PNG 截图或分页 PDF:
+如果需要视觉输出,可以直接生成截图、长截图或 PDF:
```bash
moli fetch --layout --dump screenshot https://example.com > page.png
+moli fetch --layout --dump screenshot_full https://example.com > full-page.png
moli fetch --layout --dump pdf https://example.com > page.pdf
```
diff --git a/moli/src/cli.rs b/moli/src/cli.rs
index 5c228f1c5..8ef16706a 100644
--- a/moli/src/cli.rs
+++ b/moli/src/cli.rs
@@ -45,6 +45,7 @@ const DUMP_MODES: &[&str] = &[
"html",
"markdown",
"screenshot",
+ "screenshot_full",
"pdf",
"semantic_tree",
"semantic_tree_text",
@@ -73,8 +74,9 @@ pub enum Commands {
#[derive(Debug, Clone, PartialEq, Eq, Args)]
pub struct FetchArgs {
- /// Select the fetch output format. `screenshot` writes a viewport PNG and
- /// `pdf` writes a paginated PDF directly to stdout; both require layout.
+ /// Select the fetch output format. `screenshot` writes a viewport PNG,
+ /// `screenshot_full` writes a full-document PNG, and `pdf` writes a
+ /// paginated PDF directly to stdout; all three require layout.
#[arg(long, value_enum)]
pub dump: Option<DumpFormat>,
@@ -460,6 +462,7 @@ pub enum DumpFormat {
Html,
Markdown,
Screenshot,
+ ScreenshotFull,
Pdf,
SemanticTree,
SemanticTreeText,
diff --git a/moli/src/fetch_dump.rs b/moli/src/fetch_dump.rs
index 6fd4af832..afd443bac 100644
--- a/moli/src/fetch_dump.rs
+++ b/moli/src/fetch_dump.rs
@@ -7,7 +7,7 @@ use moli_core::page::{
};
use moli_core::runtime::RawDocument;
#[cfg(test)]
-use moli_core::{runtime::Browser, runtime::BrowserConfig};
+use moli_core::{LayoutPolicy, runtime::Browser, runtime::BrowserConfig};
use serde_json::{Map, Value, json};
use crate::{
@@ -28,7 +28,18 @@ pub async fn render_page_output_async(
command: &FetchCommandConfig,
) -> Result<Vec<u8>> {
match command.dump_mode.unwrap_or(DumpFormat::Html) {
- DumpFormat::Screenshot => render_screenshot_dump_async(page).await,
+ DumpFormat::Screenshot => {
+ render_screenshot_dump_async(page, RendererScreenshotRegion::Viewport, "screenshot")
+ .await
+ }
+ DumpFormat::ScreenshotFull => {
+ render_screenshot_dump_async(
+ page,
+ RendererScreenshotRegion::FullDocument,
+ "screenshot_full",
+ )
+ .await
+ }
DumpFormat::Pdf => render_pdf_dump_async(page).await,
_ => render_page_dump_async(page, command)
.await
@@ -70,6 +81,7 @@ pub fn render_raw_document_dump(
}
DumpFormat::Markdown
| DumpFormat::Screenshot
+ | DumpFormat::ScreenshotFull
| DumpFormat::Pdf
| DumpFormat::SemanticTree
| DumpFormat::SemanticTreeText => {
@@ -128,7 +140,7 @@ async fn render_page_dump_with_trace_config_async(
}
DumpFormat::Html => render_html_dump_async(page, strip, with_base, with_frames).await,
DumpFormat::Markdown => render_markdown_dump_async(page, strip, with_frames).await,
- DumpFormat::Screenshot | DumpFormat::Pdf => {
+ DumpFormat::Screenshot | DumpFormat::ScreenshotFull | DumpFormat::Pdf => {
bail!("binary dump formats are only supported by the fetch CLI output path")
}
DumpFormat::SemanticTree => render_semantic_tree_dump_async(page).await,
@@ -136,13 +148,14 @@ async fn render_page_dump_with_trace_config_async(
}
}
-async fn render_screenshot_dump_async(page: &mut Page) -> Result<Vec<u8>> {
- let image = capture_page_raster(
- page,
- RendererCaptureScreenshotRequest::viewport_png(),
- "screenshot",
- )
- .await?;
+async fn render_screenshot_dump_async(
+ page: &mut Page,
+ region: RendererScreenshotRegion,
+ dump_mode: &str,
+) -> Result<Vec<u8>> {
+ let mut request = RendererCaptureScreenshotRequest::viewport_png();
+ request.region = region;
+ let image = capture_page_raster(page, request, dump_mode).await?;
if image.mime_type != "image/png" {
bail!(
"screenshot renderer returned `{}` instead of PNG",
@@ -467,7 +480,14 @@ mod tests {
use tokio::{net::TcpListener, task::JoinHandle};
async fn load_page(html: &str) -> Result<(Browser, Page, JoinHandle<()>)> {
- let browser = Browser::new(BrowserConfig::default())?;
+ load_page_with_config(html, BrowserConfig::default()).await
+ }
+
+ async fn load_page_with_config(
+ html: &str,
+ config: BrowserConfig,
+ ) -> Result<(Browser, Page, JoinHandle<()>)> {
+ let browser = Browser::new(config)?;
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
let body = Arc::new(html.to_owned());
@@ -486,6 +506,54 @@ mod tests {
Ok((browser, page, http_server))
}
+ fn png_dimensions(bytes: &[u8]) -> (u32, u32) {
+ assert!(bytes.starts_with(b"\x89PNG\r\n\x1a\n"));
+ assert_eq!(&bytes[12..16], b"IHDR");
+ (
+ u32::from_be_bytes(bytes[16..20].try_into().unwrap()),
+ u32::from_be_bytes(bytes[20..24].try_into().unwrap()),
+ )
+ }
+
+ #[tokio::test]
+ async fn render_full_page_screenshot_extends_beyond_viewport() -> Result<()> {
+ let config = BrowserConfig::default().with_layout_policy(LayoutPolicy::OnDemand);
+ let (_browser, mut page, http_server) = load_page_with_config(
+ concat!(
+ "<!doctype html><style>html,body{margin:0}",
+ "main{height:1300px;background:linear-gradient(red,blue)}</style>",
+ "<main></main>",
+ ),
+ config,
+ )
+ .await?;
+
+ let viewport = render_page_output_async(
+ &mut page,
+ &FetchCommandConfig {
+ dump_mode: Some(DumpFormat::Screenshot),
+ ..FetchCommandConfig::default()
+ },
+ )
+ .await?;
+ let full_page = render_page_output_async(
+ &mut page,
+ &FetchCommandConfig {
+ dump_mode: Some(DumpFormat::ScreenshotFull),
+ ..FetchCommandConfig::default()
+ },
+ )
+ .await?;
+
+ let viewport_dimensions = png_dimensions(&viewport);
+ let full_page_dimensions = png_dimensions(&full_page);
+ assert_eq!(full_page_dimensions.0, viewport_dimensions.0);
+ assert!(full_page_dimensions.1 > viewport_dimensions.1);
+
+ http_server.abort();
+ Ok(())
+ }
+
#[tokio::test]
async fn render_page_dump_default_html_uses_renderer_live_serialize() -> Result<()> {
let (_browser, mut page, http_server) =
diff --git a/moli/tests/cli.rs b/moli/tests/cli.rs
index 109747179..056e625c5 100644
--- a/moli/tests/cli.rs
+++ b/moli/tests/cli.rs
@@ -199,6 +199,7 @@ fn rejects_removed_wpt_dump_mode() {
fn parses_binary_dump_modes_with_inferred_fetch_command() {
for (value, expected) in [
("screenshot", DumpFormat::Screenshot),
+ ("screenshot_full", DumpFormat::ScreenshotFull),
("pdf", DumpFormat::Pdf),
] {
let cli = Cli::try_parse_from(normalize_args_for_compat([
diff --git a/moli/tests/fetch_cli.rs b/moli/tests/fetch_cli.rs
index 1b40d8692..d6b12ab14 100644
--- a/moli/tests/fetch_cli.rs
+++ b/moli/tests/fetch_cli.rs
@@ -803,6 +803,20 @@ fn cli_dump_screenshot_writes_png_bytes() -> Result<()> {
Ok(())
}
+#[test]
+fn cli_dump_full_screenshot_writes_png_bytes() -> Result<()> {
+ let output = run_fetch_cli_with_dump_and_args("about:blank", "screenshot_full", &["--layout"])?;
+ assert!(
+ output.status.success(),
+ "moli fetch failed: stdout_bytes={}\nstderr={}",
+ output.stdout.len(),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert!(output.stdout.starts_with(b"\x89PNG\r\n\x1a\n"));
+ assert!(output.stdout.ends_with(b"IEND\xaeB`\x82"));
+ Ok(())
+}
+
#[test]
fn cli_dump_pdf_writes_pdf_bytes() -> Result<()> {
let output = run_fetch_cli_with_dump_and_args("about:blank", "pdf", &["--layout"])?;
@@ -825,7 +839,7 @@ fn cli_dump_pdf_writes_pdf_bytes() -> Result<()> {
#[test]
fn binary_dump_modes_require_layout() -> Result<()> {
- for dump in ["screenshot", "pdf"] {
+ for dump in ["screenshot", "screenshot_full", "pdf"] {
let output = run_fetch_cli_with_dump_and_args("about:blank", dump, &[])?;
assert!(
!output.status.success(),