Skip to content

New "External Browser Mode" that connects to an existing browser via Chrome DevTools Protocol (CDP) instead of launching Playwright's bundled Chromium#20

Open
mattheworiordan wants to merge 4 commits intoSawyerHood:mainfrom
mattheworiordan:feat/chrome-for-testing-mode
Open

New "External Browser Mode" that connects to an existing browser via Chrome DevTools Protocol (CDP) instead of launching Playwright's bundled Chromium#20
mattheworiordan wants to merge 4 commits intoSawyerHood:mainfrom
mattheworiordan:feat/chrome-for-testing-mode

Conversation

@mattheworiordan
Copy link

Motivation

I use Chrome for Testing for local development and wanted dev-browser to:

  1. Use my specific browser - Chrome for Testing (not Playwright's Chromium) for consistency with manual testing
  2. Keep the browser open - After automation completes, I want to inspect results manually
  3. No extension required - Direct CDP connection without installing browser extensions
  4. Visible automation - Watch the automation run in a real browser window

Currently I'm using a patched version that works, but I'd love to contribute this upstream so others can benefit.

Changes

  • src/external-browser.ts - New serveWithExternalBrowser() function that:

    • Connects to an external browser via CDP
    • Auto-launches the browser if not running (when browserPath provided)
    • Does NOT close the browser on server stop (user manages lifecycle)
  • scripts/start-external-browser.ts - Startup script with env var configuration:

    • BROWSER_PATH - Path to browser executable
    • CDP_PORT - Browser's CDP port (default: 9223)
    • USER_DATA_DIR - Persistent profile directory
  • SKILL.md - Updated documentation with new mode

Usage

# With auto-launch
BROWSER_PATH="/Applications/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing" \
npx tsx scripts/start-external-browser.ts

# Or start browser manually first
"/path/to/chrome" --remote-debugging-port=9223 &
npx tsx scripts/start-external-browser.ts

Compatibility

  • Fully backwards compatible - existing standalone and extension modes unchanged
  • Same client API works with all modes
  • New mode is opt-in via separate startup script

Add serveWithExternalBrowser() that connects to an existing browser via CDP
instead of launching Playwright's Chromium. Key features:

  - Connect to any browser with CDP enabled (Chrome for Testing, Chrome Beta, etc.)
  - Auto-launch browser if not running (with BROWSER_PATH env var)
  - Browser stays open after server stops (user manages lifecycle)
  - No extension required - direct CDP connection

New files:
  - src/external-browser.ts - Core implementation
  - scripts/start-external-browser.ts - Startup script

Use case: Local development with visible browser automation where you want
to inspect results after automation completes.
When multiple AI agents run browser automation tasks in parallel,
they need separate HTTP API ports while potentially sharing the same
browser instance. This adds automatic port allocation to avoid conflicts.

Key changes:
- Add port-manager.ts for dynamic port allocation (range 9222-9300)
- Server tracking via ~/.dev-browser/active-servers.json
- PORT=XXXX output for agent discovery
- Config file support at ~/.dev-browser/config.json
- Update both standalone and external browser modes

Architecture:
  Agent 1 → server (port 9222) ┐
  Agent 2 → server (port 9224) ├→ Shared Browser (CDP 9223)
  Agent 3 → server (port 9226) ┘

See docs/CONCURRENCY.md for design decisions and usage examples.
Addresses concerns raised in PR SawyerHood#15 about single-point congestion.
When a dev-browser server crashes, its Chrome browser may still be
running on the CDP port. This adds smart cleanup to detect and
terminate orphaned browsers before launching new ones.

Key changes:
- Enhanced ServerInfo structure to track CDP port and mode
- Added detectOrphanedBrowsers() to find browsers with no registered server
- Added cleanupOrphanedBrowsers() to safely terminate orphans
- Standalone mode now cleans orphans on startup (before launching browser)
- External mode tracks CDP port but doesn't clean (browser is intentionally external)

This restores crash recovery functionality that was previously in
start-server.ts, but in a smarter way that respects multi-agent scenarios.
  - Add ~/.dev-browser/config.json for browser configuration
  - Auto-detect Chrome for Testing on macOS/Linux/Windows
  - Add --standalone flag to force Playwright mode
  - Skip npm install when dependencies unchanged (hash check)
  - Rename port-manager.ts to config.ts with browser config
  - Let browser use default profile unless userDataDir explicitly set
  - Simplify SKILL.md documentation with single startup flow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant