pub struct BrowserConfig {
pub process_envs: Option<HashMap<String, String>>,
pub user_data_dir: Option<PathBuf>,
pub request_intercept: bool,
pub cache_enabled: bool,
/* private fields */
}Fields§
§process_envs: Option<HashMap<String, String>>Environment variables to set for the Chromium process. Passes value through to std::process::Command::envs.
user_data_dir: Option<PathBuf>Data dir for user data
request_intercept: boolWhether to enable request interception
cache_enabled: boolWhether to enable cache
Implementations§
Source§impl BrowserConfig
impl BrowserConfig
Sourcepub fn builder() -> BrowserConfigBuilder
pub fn builder() -> BrowserConfigBuilder
Examples found in repository?
src/browser.rs (line 14)
13 pub async fn new(headless: bool) -> BrowserResult<Self> {
14 let mut config = BrowserConfig::builder();
15
16 if !headless {
17 config = config.with_head();
18 }
19
20 let (browser, mut handler) = Browser::launch(config.build().unwrap())
21 .await
22 .map_err(|e| BrowserError::Launch(e.to_string()))?;
23
24 // In the task spawn section of all browser methods:
25 let handle = tokio::task::spawn(async move {
26 while let Some(event) = handler.next().await {
27 match event {
28 Ok(_) => {}
29 Err(e) => {
30 let error_str = e.to_string();
31 // Filter out common harmless errors
32 if !error_str.contains("data did not match any variant")
33 && !error_str.contains("ResetWithoutClosingHandshake")
34 && !error_str.contains("Connection reset")
35 {
36 eprintln!("Browser event error: {:?}", e);
37 }
38 }
39 }
40 }
41 });
42
43 Ok(Self {
44 browser,
45 _handler: handle,
46 })
47 }
48 pub async fn new_stealth_browser(headless: bool) -> BrowserResult<Self> {
49 let args = vec![
50 "--disable-blink-features=AutomationControlled",
51 "--disable-infobars",
52 "--disable-extensions",
53 "--disable-plugins",
54 "--disable-web-security",
55 "--no-first-run",
56 "--disable-default-apps",
57 "--password-store=basic",
58 "--use-mock-keychain",
59 "--window-size=1366,768",
60 "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
61 "--lang=en-US",
62 "--accept-lang=en-US,en;q=0.9",
63 "--memory-pressure-off",
64 "--max_old_space_size=2048",
65 "--js-flags=--max-old-space-size=2048",
66 "--disable-background-timer-throttling",
67 "--disable-renderer-backgrounding",
68 "--disable-backgrounding-occluded-windows",
69 "--disable-dev-shm-usage",
70 "--no-sandbox",
71 "--disable-gpu",
72 "--disable-software-rasterizer",
73 "--disable-hang-monitor",
74 "--disable-prompt-on-repost",
75 "--disable-sync",
76 "--disable-translate",
77 "--disable-domain-reliability",
78 "--disable-component-extensions-with-background-pages",
79 "--disable-background-networking",
80 "--disable-client-side-phishing-detection",
81 "--disable-popup-blocking",
82 "--disable-notifications",
83 "--disable-desktop-notifications",
84 "--no-crash-upload",
85 "--no-default-browser-check",
86 "--no-pings",
87
88 "--disable-features=TranslateUI,VizDisplayCompositor",
89 "--disable-ipc-flooding-protection",
90 "--aggressive-cache-discard",
91 "--disable-backing-store-limit",
92
93 "--ignore-certificate-errors",
94 "--ignore-ssl-errors",
95 "--ignore-certificate-errors-spki-list",
96 "--disable-features=VizDisplayCompositor",
97 ];
98
99 let user_data_path = format!(
100 "/tmp/browser_profile_{}_{}",
101 std::process::id(),
102 std::time::SystemTime::now()
103 .duration_since(std::time::UNIX_EPOCH)
104 .unwrap()
105 .as_secs()
106 );
107
108 let mut config = BrowserConfig::builder()
109 .args(args)
110 .user_data_dir(&user_data_path)
111 .request_timeout(Duration::from_secs(30));
112
113 if !headless {
114 config = config.with_head();
115 }
116
117 let (browser, mut handler) = Browser::launch(config.build().unwrap())
118 .await
119 .map_err(|e| BrowserError::Launch(e.to_string()))?;
120 let handle = tokio::task::spawn(async move {
121 let mut consecutive_errors = 0;
122 const MAX_CONSECUTIVE_ERRORS: u32 = 5;
123
124 while let Some(event) = handler.next().await {
125 match event {
126 Ok(_) => {
127 consecutive_errors = 0;
128 }
129 Err(e) => {
130 let error_str = e.to_string();
131 if !error_str.contains("data did not match any variant")
132 && !error_str.contains("ResetWithoutClosingHandshake")
133 && !error_str.contains("Connection reset")
134 && !error_str.contains("EOF")
135 {
136 consecutive_errors += 1;
137
138 if consecutive_errors >= MAX_CONSECUTIVE_ERRORS {
139 eprintln!(
140 "Browser unstable - {} consecutive errors",
141 consecutive_errors
142 );
143 break;
144 }
145 }
146 }
147 }
148 }
149 });
150
151 Ok(Self {
152 browser,
153 _handler: handle,
154 })
155 }pub fn with_executable(path: impl AsRef<Path>) -> BrowserConfig
Trait Implementations§
Source§impl Clone for BrowserConfig
impl Clone for BrowserConfig
Source§fn clone(&self) -> BrowserConfig
fn clone(&self) -> BrowserConfig
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for BrowserConfig
impl RefUnwindSafe for BrowserConfig
impl Send for BrowserConfig
impl Sync for BrowserConfig
impl Unpin for BrowserConfig
impl UnwindSafe for BrowserConfig
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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 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>
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