Skip to main content

scrapling_browser/
constants.rs

1//! Chromium launch arguments and resource-type constants.
2//!
3//! This module defines the static data that controls how the Chromium browser process
4//! is configured at launch time. There are four categories:
5//!
6//! - [`EXTRA_RESOURCES`] -- the resource types (e.g. `"font"`, `"image"`) that are
7//!   blocked when [`BrowserConfig::disable_resources`] is `true`.
8//!
9//! - [`DEFAULT_ARGS`] -- baseline Chromium CLI flags applied to every launch. These
10//!   disable unnecessary features and improve startup speed.
11//!
12//! - [`STEALTH_ARGS`] -- a large set of flags that remove automation indicators and
13//!   reduce the browser's fingerprint surface. Only applied in stealth mode.
14//!
15//! - [`HARMFUL_ARGS`] -- flags that *reveal* the browser is automated. These are
16//!   stripped from the final argument list by [`filter_harmful_args`], even if the
17//!   user accidentally includes them in `extra_flags`.
18//!
19//! The two helper functions [`build_args`] and [`filter_harmful_args`] compose and
20//! sanitise the argument list for use in [`engine::build_launch_options`].
21
22/// Resource types to block when `disable_resources` is enabled.
23/// These are Playwright resource type strings matched against each outgoing request
24/// in [`intercept::should_block_resource`].
25pub const EXTRA_RESOURCES: &[&str] = &[
26    "font",
27    "image",
28    "media",
29    "beacon",
30    "object",
31    "imageset",
32    "texttrack",
33    "websocket",
34    "csp_report",
35    "stylesheet",
36];
37
38/// Chromium flags that reveal automation and should be stripped from launch args.
39///
40/// Bot-detection systems look for these flags to determine whether a browser was
41/// launched programmatically. The [`filter_harmful_args`] function removes any
42/// argument that starts with one of these prefixes.
43pub const HARMFUL_ARGS: &[&str] = &[
44    "--enable-automation",
45    "--disable-popup-blocking",
46    "--disable-component-update",
47    "--disable-default-apps",
48    "--disable-extensions",
49];
50
51/// Default Chromium launch flags for speed and basic stealth.
52///
53/// These are applied to every browser launch (both standard and stealth modes).
54/// They disable crash reporting, info bars, first-run wizards, and other features
55/// that are irrelevant in an automation context and can slow down startup.
56pub const DEFAULT_ARGS: &[&str] = &[
57    "--no-pings",
58    "--no-first-run",
59    "--disable-infobars",
60    "--disable-breakpad",
61    "--no-service-autorun",
62    "--homepage=about:blank",
63    "--password-store=basic",
64    "--disable-hang-monitor",
65    "--no-default-browser-check",
66    "--disable-session-crashed-bubble",
67    "--disable-search-engine-choice-screen",
68];
69
70/// Anti-detection Chromium flags for evading bot detection systems.
71///
72/// This is a comprehensive list of flags that hide automation indicators, disable
73/// features that leak information (e.g. crash reporters, sync, translate), and
74/// configure Blink to report plausible device capabilities. Only applied when the
75/// session is in stealth mode.
76pub const STEALTH_ARGS: &[&str] = &[
77    "--test-type",
78    "--lang=en-US",
79    "--mute-audio",
80    "--disable-sync",
81    "--hide-scrollbars",
82    "--disable-logging",
83    "--start-maximized",
84    "--enable-async-dns",
85    "--accept-lang=en-US",
86    "--use-mock-keychain",
87    "--disable-translate",
88    "--disable-voice-input",
89    "--window-position=0,0",
90    "--disable-wake-on-wifi",
91    "--ignore-gpu-blocklist",
92    "--enable-tcp-fast-open",
93    "--enable-web-bluetooth",
94    "--disable-cloud-import",
95    "--disable-print-preview",
96    "--disable-dev-shm-usage",
97    "--metrics-recording-only",
98    "--disable-crash-reporter",
99    "--disable-partial-raster",
100    "--disable-gesture-typing",
101    "--disable-checker-imaging",
102    "--disable-prompt-on-repost",
103    "--force-color-profile=srgb",
104    "--font-render-hinting=none",
105    "--aggressive-cache-discard",
106    "--disable-cookie-encryption",
107    "--disable-domain-reliability",
108    "--disable-threaded-animation",
109    "--disable-threaded-scrolling",
110    "--enable-simple-cache-backend",
111    "--disable-background-networking",
112    "--enable-surface-synchronization",
113    "--disable-image-animation-resync",
114    "--disable-renderer-backgrounding",
115    "--disable-ipc-flooding-protection",
116    "--prerender-from-omnibox=disabled",
117    "--safebrowsing-disable-auto-update",
118    "--disable-offer-upload-credit-cards",
119    "--disable-background-timer-throttling",
120    "--disable-new-content-rendering-timeout",
121    "--run-all-compositor-stages-before-draw",
122    "--disable-client-side-phishing-detection",
123    "--disable-backgrounding-occluded-windows",
124    "--disable-layer-tree-host-memory-pressure",
125    "--autoplay-policy=user-gesture-required",
126    "--disable-offer-store-unmasked-wallet-cards",
127    "--disable-blink-features=AutomationControlled",
128    "--disable-component-extensions-with-background-pages",
129    "--enable-features=NetworkService,NetworkServiceInProcess,TrustTokens,TrustTokensAlwaysAllowIssuance",
130    "--blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4",
131    "--disable-features=AudioServiceOutOfProcess,TranslateUI,BlinkGenPropertyTrees",
132];
133
134/// Builds the Chromium launch argument list, optionally including stealth flags.
135///
136/// When `stealth` is `true`, the [`STEALTH_ARGS`] are appended after the
137/// [`DEFAULT_ARGS`]. The result should be further processed by
138/// [`filter_harmful_args`] before being passed to Playwright.
139pub fn build_args(stealth: bool) -> Vec<String> {
140    let mut args: Vec<String> = DEFAULT_ARGS.iter().map(|s| s.to_string()).collect();
141    if stealth {
142        args.extend(STEALTH_ARGS.iter().map(|s| s.to_string()));
143    }
144    args
145}
146
147/// Removes automation-revealing flags from the argument list.
148///
149/// Iterates over the provided args and drops any that start with a prefix in
150/// [`HARMFUL_ARGS`]. This is a safety net -- even if a user adds `--enable-automation`
151/// in `extra_flags`, it will be silently removed here.
152pub fn filter_harmful_args(args: &[String]) -> Vec<String> {
153    args.iter()
154        .filter(|a| !HARMFUL_ARGS.iter().any(|h| a.starts_with(h)))
155        .cloned()
156        .collect()
157}