pub struct Browser { /* private fields */ }
Expand description
A browser instance.
Implementations§
Source§impl Browser
impl Browser
Sourcepub async fn new() -> Result<Self>
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(())
}
Sourcepub async fn new_with_head() -> Result<Self>
pub async fn new_with_head() -> Result<Self>
Create a new browser instance with a visible window.
Sourcepub async fn new_tab(&self) -> Result<Tab>
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(())
}
Sourcepub async fn close_init_tab(&self) -> Result<()>
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.
Sourcepub async fn capture_html(&self, html: &str, selector: &str) -> Result<String>
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 contentselector
: 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(())
}
Sourcepub async fn capture_html_with_options(
&self,
html: &str,
selector: &str,
options: CaptureOptions,
) -> Result<String>
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 contentselector
: The CSS selector of the element to captureoptions
: 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(())
}
Sourcepub fn close(&mut self) -> Result<()>
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
impl Browser
Sourcepub async fn instance() -> Arc<Browser>
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(())
}
Sourcepub fn close_instance() -> Option<()>
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§
impl Send for Browser
impl Sync for Browser
Auto Trait Implementations§
impl Freeze for Browser
impl RefUnwindSafe for Browser
impl Unpin for Browser
impl UnwindSafe for Browser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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