Browser

Struct Browser 

Source
pub struct Browser { /* private fields */ }
Expand description

A browser instance.

Implementations§

Source§

impl Browser

Source

pub async fn new() -> Result<Self>

Create a new browser instance with default configuration (headless).

§Example
use cdp_html_shot::Browser;
use anyhow::Result;

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

pub async fn new_with_head() -> Result<Self>

Create a new browser instance with a visible window.

Source

pub async fn new_tab(&self) -> Result<Tab>

Create a new tab.

§Example
use cdp_html_shot::Browser;
use anyhow::Result;

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

pub async fn close_init_tab(&self) -> Result<()>

Close the initial tab created when the browser starts.

§Warning

Only in headless mode, otherwise it will close the entire browser.

Source

pub async fn capture_html(&self, html: &str, selector: &str) -> Result<String>

Basic version: Capture a screenshot of an HTML element.

Returns the base64-encoded image data (JPEG format).

If you need more control over the capture process, use capture_html_with_options.

§Arguments
  • html: The HTML content
  • selector: The CSS selector of the element to capture
§Example
use cdp_html_shot::Browser;
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    let browser = Browser::new().await?;
    let base64 = browser.capture_html("<h1>Hello world!</h1>", "h1").await?;
    Ok(())
}
Source

pub async fn capture_html_with_options( &self, html: &str, selector: &str, options: CaptureOptions, ) -> Result<String>

Advanced version: Capture a screenshot of an HTML element with additional options.

§Arguments
  • html: The HTML content
  • selector: The CSS selector of the element to capture
  • options: Configuration options for the capture
§Example
use cdp_html_shot::{Browser, CaptureOptions};
use anyhow::Result;

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

    let base64 = browser
        .capture_html_with_options(
            "<h1>Hello world!</h1>",
            "h1",
            options
        ).await?;
    Ok(())
}
Source

pub fn close(&mut self) -> Result<()>

Close the browser.

This will kill the browser process and clean up temporary files.

Normally, this method does not need to be called manually, because it will be called automatically when the Browser instance is destroyed.

§Example
use cdp_html_shot::Browser;
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    let mut browser = Browser::new().await?;
    browser.close()?;
    Ok(())
}
Source§

impl Browser

Source

pub async fn instance() -> Arc<Browser>

Get the global Browser instance.

Creates a new one if it doesn’t exist.

This method is thread-safe and ensures only one browser instance is created.

The browser will be automatically closed when all references are dropped or when the program exits.

§Example
use cdp_html_shot::Browser;
use anyhow::Result;

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

    Browser::close_instance();
    Ok(())
}
Source

pub fn close_instance() -> Option<()>

Close the global Browser instance.

Please ensure that this method is called before the program exits, and there should be no Browser instances in use at this time.

Trait Implementations§

Source§

impl Debug for Browser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Browser

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Browser

Source§

impl Sync for Browser

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V