Skip to content

Commit

Permalink
feat(browser): add support for the new headless mode in BrowserConfig (
Browse files Browse the repository at this point in the history
…#208)

* feat(browser): add support for the new headless mode in BrowserConfig

* miscellaneous enhancements following dda6ff

* further fixes
  • Loading branch information
starrify committed Apr 18, 2024
1 parent a798f02 commit bd62ee3
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,22 @@ async fn ws_url_from_output(
}
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum HeadlessMode {
/// The "headful" mode.
False,
/// The old headless mode.
#[default]
True,
/// The new headless mode. See also: https://developer.chrome.com/docs/chromium/new-headless
New,
}

#[derive(Debug, Clone)]
pub struct BrowserConfig {
/// Determines whether to run headless version of the browser. Defaults to
/// true.
headless: bool,
headless: HeadlessMode,
/// Determines whether to run the browser with a sandbox.
sandbox: bool,
/// Launch the browser with a specific window width and height.
Expand Down Expand Up @@ -619,7 +630,7 @@ pub struct BrowserConfig {

#[derive(Debug, Clone)]
pub struct BrowserConfigBuilder {
headless: bool,
headless: HeadlessMode,
sandbox: bool,
window_size: Option<(u32, u32)>,
port: u16,
Expand Down Expand Up @@ -652,7 +663,7 @@ impl BrowserConfig {
impl Default for BrowserConfigBuilder {
fn default() -> Self {
Self {
headless: true,
headless: HeadlessMode::True,
sandbox: true,
window_size: None,
port: 0,
Expand Down Expand Up @@ -686,7 +697,17 @@ impl BrowserConfigBuilder {
}

pub fn with_head(mut self) -> Self {
self.headless = false;
self.headless = HeadlessMode::False;
self
}

pub fn new_headless_mode(mut self) -> Self {
self.headless = HeadlessMode::New;
self
}

pub fn headless_mode(mut self, mode: HeadlessMode) -> Self {
self.headless = mode;
self
}

Expand Down Expand Up @@ -889,8 +910,16 @@ impl BrowserConfig {
cmd.args(["--no-sandbox", "--disable-setuid-sandbox"]);
}

if self.headless {
cmd.args(["--headless", "--hide-scrollbars", "--mute-audio"]);
match self.headless {
HeadlessMode::False => (),
HeadlessMode::True => {
cmd.args(["--headless", "--hide-scrollbars", "--mute-audio"]);
()

Check failure on line 917 in src/browser.rs

View workflow job for this annotation

GitHub Actions / Clippy

unneeded unit expression
}
HeadlessMode::New => {
cmd.args(["--headless=new", "--hide-scrollbars", "--mute-audio"]);
()

Check failure on line 921 in src/browser.rs

View workflow job for this annotation

GitHub Actions / Clippy

unneeded unit expression
}
}

if self.incognito {
Expand Down

0 comments on commit bd62ee3

Please sign in to comment.