fix(m0): keep navigator.webdriver off so YouTube does not stop the video a minute in
With DevTools on (port or pipe), Chrome tells pages a program drives it, and YouTube then shows "문제가 발생했습니다" after about a minute of video, which spoilt every window on the gaming PC. Reproduced on Linux: the port alone, with no command sent, breaks playback at ~40 s; without it, or with --disable-blink-features=AutomationControlled, it plays on. The browser is now started with that switch, and one that still reports webdriver is refused before Jarvis is started. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -347,6 +347,16 @@ def find_browser() -> str | None:
|
||||
return next((shutil.which(n) for n in ("google-chrome", "chromium", "chromium-browser") if shutil.which(n)), None)
|
||||
|
||||
|
||||
def _says_automated(cdp: Cdp) -> bool:
|
||||
"""Whether pages see navigator.webdriver; asked again while the first page is still loading."""
|
||||
for _ in range(10):
|
||||
try:
|
||||
return cdp.evaluate("navigator.webdriver") is True
|
||||
except RuntimeError:
|
||||
time.sleep(0.5)
|
||||
return False
|
||||
|
||||
|
||||
def open_video_page(work: Path, url: str = VIDEO_URL, extra_args: tuple[str, ...] = ()) -> VideoPage | None:
|
||||
"""Open ``url`` in a new throwaway profile with DevTools on a free port. None without a browser."""
|
||||
exe = find_browser()
|
||||
@@ -356,7 +366,11 @@ def open_video_page(work: Path, url: str = VIDEO_URL, extra_args: tuple[str, ...
|
||||
shutil.rmtree(profile, ignore_errors=True)
|
||||
profile.mkdir(parents=True)
|
||||
# Kept running and drawing even if another window covers part of it; muted (nobody listens).
|
||||
# DevTools makes Chrome set navigator.webdriver, and YouTube then stops the video about a minute in
|
||||
# with "문제가 발생했습니다" (every window on the gaming PC, 2026-11-11); AutomationControlled off
|
||||
# keeps it false.
|
||||
proc = subprocess.Popen([exe, f"--user-data-dir={profile}", "--remote-debugging-port=0", "--no-first-run",
|
||||
"--disable-blink-features=AutomationControlled",
|
||||
"--no-default-browser-check", "--autoplay-policy=no-user-gesture-required",
|
||||
"--mute-audio", "--start-maximized", "--disable-backgrounding-occluded-windows",
|
||||
"--disable-renderer-backgrounding", "--disable-background-timer-throttling",
|
||||
@@ -371,7 +385,12 @@ def open_video_page(work: Path, url: str = VIDEO_URL, extra_args: tuple[str, ...
|
||||
page = next((p for p in (pages or []) if p.get("type") == "page" and "youtube" in p.get("url", "")),
|
||||
None) if code == 200 else None
|
||||
if page:
|
||||
return VideoPage(Cdp(page["webSocketDebuggerUrl"]), proc, profile, url)
|
||||
vp = VideoPage(Cdp(page["webSocketDebuggerUrl"]), proc, profile, url)
|
||||
if _says_automated(vp.cdp):
|
||||
vp.close()
|
||||
raise RuntimeError("the browser tells pages a program drives it (navigator.webdriver), "
|
||||
"so YouTube would stop the video about a minute in")
|
||||
return vp
|
||||
time.sleep(1)
|
||||
break
|
||||
if proc.poll() is not None:
|
||||
|
||||
@@ -1554,6 +1554,49 @@ def test_before_a_window_a_stopped_or_broken_player_is_started_again(monkeypatch
|
||||
assert not page.snapshot().get("lost")
|
||||
|
||||
|
||||
def test_the_video_browser_does_not_tell_pages_a_program_drives_it(tmp_path, monkeypatch):
|
||||
"""With DevTools on, Chrome sets navigator.webdriver, and YouTube then stops the video about a
|
||||
minute in with its error screen (seen on the gaming PC and reproduced on Linux)."""
|
||||
from m0lib import video
|
||||
if not video.find_browser():
|
||||
pytest.skip("no Chrome or Edge on this machine")
|
||||
|
||||
class Page(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/html")
|
||||
self.end_headers()
|
||||
self.wfile.write(b"<title>watch</title>")
|
||||
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
srv = ThreadingHTTPServer(("127.0.0.1", 0), Page)
|
||||
threading.Thread(target=srv.serve_forever, daemon=True).start()
|
||||
# The page is found by "youtube" in its address; headless keeps the test off the screen.
|
||||
url = f"http://127.0.0.1:{srv.server_port}/youtube"
|
||||
page = None
|
||||
try:
|
||||
page = video.open_video_page(tmp_path, url=url, extra_args=("--headless=new",))
|
||||
assert page.cdp.evaluate("navigator.webdriver") is False
|
||||
page.close()
|
||||
page = None
|
||||
# A browser that still says so (one that ignores the switch) is refused at once and closed,
|
||||
# before Jarvis is started for nothing.
|
||||
class Says(video.Cdp):
|
||||
def evaluate(self, expression):
|
||||
return True if expression == "navigator.webdriver" else super().evaluate(expression)
|
||||
|
||||
monkeypatch.setattr(video, "Cdp", Says)
|
||||
with pytest.raises(RuntimeError, match="webdriver"):
|
||||
video.open_video_page(tmp_path, url=url, extra_args=("--headless=new",))
|
||||
assert not (tmp_path / "cache" / "video" / "browser-profile").exists()
|
||||
finally:
|
||||
if page:
|
||||
page.close()
|
||||
srv.shutdown()
|
||||
|
||||
|
||||
def test_a_page_script_that_fails_while_the_page_loads_reads_as_a_lost_page():
|
||||
from m0lib import video
|
||||
|
||||
|
||||
Reference in New Issue
Block a user