From 3607b2aa0fc6c64b4e1fde4faff273a7650aad04 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 10 Jul 2026 19:01:10 -0500 Subject: [PATCH] refac --- cptr/routers/browser.py | 6 ++-- cptr/utils/browser/viewer.py | 58 +++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/cptr/routers/browser.py b/cptr/routers/browser.py index 50c8e76..b5d2e59 100644 --- a/cptr/routers/browser.py +++ b/cptr/routers/browser.py @@ -246,10 +246,8 @@ async def create_session(request: Request): ) session.mode = "chrome" except Exception as exc: - if explicit_mode: - await manager.close(session.session_id, session.owner) - raise HTTPException(status_code=409, detail=str(exc)) from exc - session.status = "ready" + await manager.close(session.session_id, session.owner) + raise HTTPException(status_code=409, detail=str(exc)) from exc return _session_payload(session) diff --git a/cptr/utils/browser/viewer.py b/cptr/utils/browser/viewer.py index 6baa14a..b3257e6 100644 --- a/cptr/utils/browser/viewer.py +++ b/cptr/utils/browser/viewer.py @@ -87,27 +87,49 @@ async def resolve_cdp_endpoint(cdp_url: str, data_dir: Path | None = None) -> st parsed = urlsplit(base) if parsed.hostname not in {"localhost", "127.0.0.1", "::1"} or not parsed.port: raise RuntimeError("Could not connect to Chrome") from exc - if data_dir is None: + if data_dir is not None: + data_dirs = [data_dir] + else: home = Path.home() if sys.platform == "darwin": - data_dir = home / "Library/Application Support/Google/Chrome" + support = home / "Library/Application Support" + data_dirs = [ + support / "Google/Chrome", + support / "Google/Chrome Canary", + support / "Chromium", + support / "BraveSoftware/Brave-Browser", + support / "Microsoft Edge", + ] elif sys.platform == "win32" and os.environ.get("LOCALAPPDATA"): - data_dir = Path(os.environ["LOCALAPPDATA"]) / "Google/Chrome/User Data" + local = Path(os.environ["LOCALAPPDATA"]) + data_dirs = [ + local / "Google/Chrome/User Data", + local / "Google/Chrome SxS/User Data", + local / "Chromium/User Data", + local / "BraveSoftware/Brave-Browser/User Data", + local / "Microsoft/Edge/User Data", + ] else: - data_dir = ( - Path(os.environ.get("XDG_CONFIG_HOME", home / ".config")) / "google-chrome" - ) - try: - port, path = (data_dir / "DevToolsActivePort").read_text().splitlines()[:2] - except (OSError, ValueError) as file_exc: - raise RuntimeError("Could not connect to Chrome") from file_exc - if ( - not port.isdigit() - or int(port) != parsed.port - or not path.startswith("/devtools/browser/") - ): - raise RuntimeError("Could not connect to Chrome") from exc - return f"ws://127.0.0.1:{port}{path}" + config = Path(os.environ.get("XDG_CONFIG_HOME", home / ".config")) + data_dirs = [ + config / "google-chrome", + config / "google-chrome-unstable", + config / "chromium", + config / "BraveSoftware/Brave-Browser", + config / "microsoft-edge", + ] + for directory in data_dirs: + try: + port, path = (directory / "DevToolsActivePort").read_text().splitlines()[:2] + except (OSError, ValueError): + continue + if ( + port.isdigit() + and int(port) == parsed.port + and path.startswith("/devtools/browser/") + ): + return f"ws://127.0.0.1:{port}{path}" + raise RuntimeError("Could not connect to Chrome") from exc class CDPConnection: @@ -372,6 +394,7 @@ class ChromeViewerManager: if ready.get("result", {}).get("value") is True: break await asyncio.sleep(0.1) + await host.browser_cdp.send("Target.activateTarget", {"targetId": controller_id}) await controller_cdp.send( "Runtime.evaluate", { @@ -384,6 +407,7 @@ class ChromeViewerManager: ) if viewer.encoder_error: raise RuntimeError(viewer.encoder_error) + await host.browser_cdp.send("Target.activateTarget", {"targetId": target_id}) if session.url: await target_cdp.send("Page.navigate", {"url": session.url}) else: