fix(browser): the shared bot browser never offers to translate, and its window check covers the focus

Chrome 157 no longer reads --disable-features=Translate, so on the virtual monitor every page in
another language got a translate bubble over it on the shared screen (seen on the game PC). The
profile's preferences now turn translation off before each start, keeping the other preferences.

The live screen test also checks that opening the window leaves the focus where it was.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-15 13:51:04 +09:00
parent f71c72d3a1
commit cb2dda25b6
3 changed files with 63 additions and 3 deletions

View File

@@ -365,7 +365,8 @@ background (headless), or with `features.browserShare` in a window on the virtua
worker. Without it (off, or not installed) nothing starts. A browser that does not start is a
warning on the dashboard and in the `browser` log tab; the services run on without it.
- Chrome runs with its own profile `browser/profile` (`--user-data-dir`), headless, no first-run,
sync, component updates, translation or background networking, and CDP on a free loopback port
sync, component updates, translation (also turned off in the profile's preferences before each
start, keeping the others, since Chrome no longer reads the switch) or background networking, and CDP on a free loopback port
(`--remote-debugging-port=0`; the port is read from the profile's `DevToolsActivePort`, which is
removed before each start, and `/json/version` must answer within 30 s). The worker gets
`JARVIS_BROWSER_CDP` = `http://127.0.0.1:<port>`.

View File

@@ -264,6 +264,7 @@ impl BotBrowser {
pub async fn start(exe: &Path, profile: &Path, screen: Option<crate::vdisplay::Rect>) -> Result<Self, String> {
let proxy = EgressProxy::start(is_public).await.map_err(|e| format!("proxy: {e}"))?;
std::fs::create_dir_all(profile).map_err(|e| e.to_string())?;
turn_off_translation(profile);
let port_file = profile.join("DevToolsActivePort");
let _ = std::fs::remove_file(&port_file);
let no_sandbox = cfg!(not(windows)) && std::env::var("JARVIS_CHROME_NO_SANDBOX").is_ok_and(|v| v == "1");
@@ -403,6 +404,25 @@ async fn kill_tree(child: &mut tokio::process::Child) {
let _ = child.kill().await;
}
/// Chrome no longer reads `--disable-features=Translate`: without this preference it offers to
/// translate every page in another language, a bubble that covers the page on the shared screen.
/// Written before Chrome starts; the profile's other preferences stay.
fn turn_off_translation(profile: &Path) {
let file = profile.join("Default").join("Preferences");
let mut prefs = std::fs::read_to_string(&file).ok()
.and_then(|t| serde_json::from_str::<serde_json::Value>(&t).ok())
.filter(serde_json::Value::is_object).unwrap_or_else(|| serde_json::json!({}));
if !prefs["translate"].is_object() {
prefs["translate"] = serde_json::json!({});
}
prefs["translate"]["enabled"] = false.into();
let written = std::fs::create_dir_all(file.parent().unwrap_or(profile))
.and_then(|_| std::fs::write(&file, prefs.to_string()));
if let Err(e) = written {
crate::warn_log!("browser", "could not turn off translation in the profile: {e}");
}
}
/// Where the bot browser's profile lives in the data folder.
pub fn profile_dir(data: &Path) -> PathBuf {
data.join("browser").join("profile")
@@ -497,6 +517,30 @@ mod tests {
assert_eq!(seen[1], format!("GET /a/b?c=1 HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nAccept: */*\r\nConnection: close\r\n\r\n"));
}
#[test]
fn the_profile_never_offers_to_translate_and_keeps_its_other_preferences() {
let dir = std::env::temp_dir().join(format!("jarvis-translate-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let prefs = |d: &Path| -> serde_json::Value {
serde_json::from_str(&std::fs::read_to_string(d.join("Default").join("Preferences")).unwrap()).unwrap()
};
turn_off_translation(&dir);
assert_eq!(prefs(&dir)["translate"]["enabled"], false, "a new profile");
std::fs::write(dir.join("Default").join("Preferences"),
r#"{"homepage":"x","translate":{"enabled":true,"blocked_languages":["ko"]}}"#).unwrap();
turn_off_translation(&dir);
let p = prefs(&dir);
assert_eq!(p["translate"]["enabled"], false, "a profile Chrome already used");
assert_eq!(p["homepage"], "x");
assert_eq!(p["translate"]["blocked_languages"][0], "ko");
std::fs::write(dir.join("Default").join("Preferences"), "not json").unwrap();
turn_off_translation(&dir);
assert_eq!(prefs(&dir)["translate"]["enabled"], false, "a damaged file");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn chrome_goes_through_the_proxy_with_its_own_profile() {
let a = chrome_args(Path::new("/d/browser/profile"), 4321, false, false);

View File

@@ -86,9 +86,20 @@ async fn the_browser_step_installs_a_chrome_that_starts() {
let _ = std::fs::remove_dir_all(&data);
}
/// The process whose window has the keyboard focus (Windows).
#[cfg(windows)]
fn foreground_pid() -> u32 {
use windows_sys::Win32::UI::WindowsAndMessaging::{GetForegroundWindow, GetWindowThreadProcessId};
let mut pid = 0;
// SAFETY: plain queries; `pid` outlives the call.
unsafe { GetWindowThreadProcessId(GetForegroundWindow(), &mut pid) };
pid
}
/// With a screen, Chrome starts without a window and the app opens exactly one, at the screen's
/// place and size; the page tab the bot opens joins it. Needs a display on Linux (`DISPLAY`); on
/// Windows the screen is the virtual monitor (its driver installed), on the desktop only meanwhile.
/// place and size, without taking the focus; the page tab the bot opens joins it. Needs a display
/// on Linux (`DISPLAY`); on Windows the screen is the virtual monitor (its driver installed), on the
/// desktop only meanwhile.
#[tokio::test]
async fn on_a_screen_the_bot_browser_has_one_window_at_the_monitor() {
let Ok(exe) = std::env::var("JARVIS_TEST_CHROME") else {
@@ -107,7 +118,11 @@ async fn on_a_screen_the_bot_browser_has_one_window_at_the_monitor() {
} else {
jarvis_app::vdisplay::Rect { x: 40, y: 30, w: 1280, h: 720 }
};
#[cfg(windows)]
let focused = foreground_pid();
let b = BotBrowser::start(std::path::Path::new(&exe), &profile, Some(screen)).await.expect("chrome starts");
#[cfg(windows)]
assert_eq!(foreground_pid(), focused, "opening the window leaves the focus where it was");
let client = reqwest::Client::builder().no_proxy().build().unwrap();
let pages = |l: serde_json::Value| l.as_array().unwrap().iter().filter(|t| t["type"] == "page")
.map(|t| t["id"].as_str().unwrap().to_owned()).collect::<Vec<_>>();