diff --git a/README.md b/README.md index bac5231..75bae5b 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,30 @@ await browser.close() server.close() ``` +### Environment Variables + +#### `PLAYWRITER_AUTO_ENABLE` + +When set, the MCP will automatically create an initial tab when a Playwright client connects and no tabs are currently enabled. This is useful for fully automated workflows where you don't want to manually click the extension icon. + +```json +{ + "mcpServers": { + "playwriter": { + "command": "npx", + "args": ["playwriter@latest"], + "env": { + "PLAYWRITER_AUTO_ENABLE": "1" + } + } + } +} +``` + +With this setting, when your AI agent starts, it will immediately have a page to work with without requiring manual extension activation. + +**Note:** The auto-created tab starts at `about:blank`. Your agent can then navigate it to any URL. + ## Comparison ### vs Playwright MCP diff --git a/extension/src/background.ts b/extension/src/background.ts index 9f2db5d..f71b5da 100644 --- a/extension/src/background.ts +++ b/extension/src/background.ts @@ -560,6 +560,21 @@ function handleConnectionClose(reason: string, code: number): void { } async function ensureConnection(): Promise { + // If already connecting, wait for that connection to complete instead of creating a new one + // This prevents "Extension Replaced" errors when multiple callers race to connect + if (ws?.readyState === WebSocket.CONNECTING) { + logger.debug('Connection already in progress, waiting for it to complete') + const existingWs = ws + await new Promise((resolve) => { + const checkInterval = setInterval(() => { + if (existingWs.readyState !== WebSocket.CONNECTING) { + clearInterval(checkInterval) + resolve() + } + }, 100) + }) + } + if (ws?.readyState === WebSocket.OPEN) { logger.debug('Connection already exists, reusing') return