add --color-scheme flag for persistent dark/light mode (#528)
Fixes #519. Playwright defaults `colorScheme` to `light` on all new contexts, overriding the browser/OS dark mode setting. This is especially disruptive in CDP mode, where every reconnection resets the scheme. The `set media dark` command also didn't persist its choice to new tabs or pages. - Add `--color-scheme <dark|light|no-preference>` flag, config key (`colorScheme`), and env var (`AGENT_BROWSER_COLOR_SCHEME`) - Store the preference in `BrowserManager` and automatically apply it to all new contexts (via Playwright's context option) and all new pages (via `page.emulateMedia` in `setupPageTracking`) - `set media dark/light` now also persists its choice for subsequent pages and tabs
This commit is contained in:
@@ -33,6 +33,7 @@ pub struct Config {
|
||||
pub auto_connect: Option<bool>,
|
||||
pub headers: Option<String>,
|
||||
pub annotate: Option<bool>,
|
||||
pub color_scheme: Option<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -66,6 +67,7 @@ impl Config {
|
||||
auto_connect: other.auto_connect.or(self.auto_connect),
|
||||
headers: other.headers.or(self.headers),
|
||||
annotate: other.annotate.or(self.annotate),
|
||||
color_scheme: other.color_scheme.or(self.color_scheme),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,6 +131,7 @@ fn extract_config_path(args: &[String]) -> Option<Option<String>> {
|
||||
"--provider",
|
||||
"--device",
|
||||
"--session-name",
|
||||
"--color-scheme",
|
||||
];
|
||||
let mut i = 0;
|
||||
while i < args.len() {
|
||||
@@ -199,6 +202,7 @@ pub struct Flags {
|
||||
pub auto_connect: bool,
|
||||
pub session_name: Option<String>,
|
||||
pub annotate: bool,
|
||||
pub color_scheme: Option<String>,
|
||||
|
||||
// Track which launch-time options were explicitly passed via CLI
|
||||
// (as opposed to being set only via environment variables)
|
||||
@@ -278,6 +282,8 @@ pub fn parse_flags(args: &[String]) -> Flags {
|
||||
.or(config.session_name),
|
||||
annotate: env_var_is_truthy("AGENT_BROWSER_ANNOTATE")
|
||||
|| config.annotate.unwrap_or(false),
|
||||
color_scheme: env::var("AGENT_BROWSER_COLOR_SCHEME").ok()
|
||||
.or(config.color_scheme),
|
||||
cli_executable_path: false,
|
||||
cli_extensions: false,
|
||||
cli_profile: false,
|
||||
@@ -425,6 +431,12 @@ pub fn parse_flags(args: &[String]) -> Flags {
|
||||
flags.annotate = val;
|
||||
if consumed { i += 1; }
|
||||
}
|
||||
"--color-scheme" => {
|
||||
if let Some(s) = args.get(i + 1) {
|
||||
flags.color_scheme = Some(s.clone());
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
"--config" => {
|
||||
// Already handled by load_config(); skip the value
|
||||
i += 1;
|
||||
@@ -468,6 +480,7 @@ pub fn clean_args(args: &[String]) -> Vec<String> {
|
||||
"--provider",
|
||||
"--device",
|
||||
"--session-name",
|
||||
"--color-scheme",
|
||||
"--config",
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user