cdp-html-shot

Crates.iocdp-html-shot
lib.rscdp-html-shot
version0.1.15
sourcesrc
created_at2024-10-30 16:58:07.256576
updated_at2024-11-04 14:29:28.18799
descriptionA Rust library for capturing HTML screenshots using CDP.
homepage
repositoryhttps://github.com/araea/cdp-html-shot
max_upload_size
id1428839
size135,379
Nawyjx (araea)

documentation

https://docs.rs/cdp-html-shot

README

cdp-html-shot

github crates.io docs.rs

A Rust library for capturing HTML screenshots using CDP.

  • Auto cleanup
  • Asynchronous API (Tokio)
  • HTML screenshot captures

Usage

[dependencies]
cdp-html-shot = "0.1"

Example

Capture HTML screenshot

use base64::Engine;
use anyhow::Result;
use cdp_html_shot::Browser;

#[tokio::main]
async fn main() -> Result<()> {
    const HTML: &str = r#"
        <html lang="en-US">
        <body>
        <h1>My test page</h1>
        <p>Hello, Rust!</p>
        </body>
        </html>
    "#;
    
    let browser = Browser::new().await?;
    let base64 = browser.capture_html(HTML, "html").await?;

    let img_data = base64::prelude::BASE64_STANDARD.decode(base64)?;
    std::fs::write("test0.jpeg", img_data)?;

    Ok(())
}

Fine control

use base64::Engine;
use anyhow::Result;
use cdp_html_shot::Browser;

#[tokio::main]
async fn main() -> Result<()> {
    let browser = Browser::new().await?;
    let tab = browser.new_tab().await?;

    tab.set_content("<h1>Hello world!</h1>").await?;

    let element = tab.find_element("h1").await?;
    let base64 = element.screenshot().await?;
    tab.close().await?;

    let img_data = base64::prelude::BASE64_STANDARD.decode(base64)?;
    std::fs::write("test0.jpeg", img_data)?;

    Ok(())
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Commit count: 21

cargo fmt