diff --git a/scripts/m0/m0lib/video.py b/scripts/m0/m0lib/video.py index 8c6ead6..48c76c8 100644 --- a/scripts/m0/m0lib/video.py +++ b/scripts/m0/m0lib/video.py @@ -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: diff --git a/tests/test_m0_kit.py b/tests/test_m0_kit.py index 5bd25a7..9230bcb 100644 --- a/tests/test_m0_kit.py +++ b/tests/test_m0_kit.py @@ -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"