diff --git a/README.md b/README.md index 73abd7d..055208a 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ npm install -g @jackwener/opencli@latest ## 📋 Prerequisites Browser commands need: -1. **Chrome** running with the target site logged in +1. **Chrome** running **and logged into the target site** (e.g. bilibili.com, zhihu.com, xiaohongshu.com) 2. **[Playwright MCP Bridge](https://chromewebstore.google.com/detail/playwright-mcp-bridge/mmlmfjhmonkocbjadbfplnigmagldckm)** extension installed 3. Configure `PLAYWRIGHT_MCP_EXTENSION_TOKEN` (from the extension settings page) as an environment variable, or in your MCP config: @@ -75,6 +75,8 @@ Browser commands need: Public API commands (`hackernews`, `github search`, `v2ex`) need no browser at all. +> **⚠️ Important**: Browser commands reuse your Chrome login session. You must be logged into the target website in Chrome before running commands. If you get empty data or errors, check your login status first. + ## 📦 Built-in Commands | Site | Commands | Mode | diff --git a/README.zh-CN.md b/README.zh-CN.md index 3412b03..4fdeeca 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -55,7 +55,7 @@ npm install -g @jackwener/opencli@latest ## 📋 前置要求 浏览器命令需要: -1. **Chrome** 浏览器正在运行,且已登录目标网站 +1. **Chrome** 浏览器正在运行,且**已登录目标网站**(如 bilibili.com、zhihu.com、xiaohongshu.com) 2. 安装 **[Playwright MCP Bridge](https://chromewebstore.google.com/detail/playwright-mcp-bridge/mmlmfjhmonkocbjadbfplnigmagldckm)** 扩展 3. 配置 `PLAYWRIGHT_MCP_EXTENSION_TOKEN`(从扩展设置页获取),设为环境变量或写入 MCP 配置: @@ -75,6 +75,8 @@ npm install -g @jackwener/opencli@latest 公共 API 命令(`hackernews`、`github search`、`v2ex`)无需浏览器。 +> **⚠️ 重要**:浏览器命令复用你的 Chrome 登录状态。运行命令前,你必须已在 Chrome 中登录目标网站。如果获取到空数据或报错,请先检查登录状态。 + ## 📦 内置命令 | 站点 | 命令 | 模式 | diff --git a/SKILL.md b/SKILL.md index f560c50..1be51fd 100644 --- a/SKILL.md +++ b/SKILL.md @@ -28,10 +28,12 @@ npm update -g @jackwener/opencli ## Prerequisites Browser commands require: -1. Chrome browser running (logged into target sites) +1. Chrome browser running **(logged into target sites)** 2. [Playwright MCP Bridge](https://chromewebstore.google.com/detail/playwright-mcp-bridge/mmlmfjhmonkocbjadbfplnigmagldckm) extension 3. Configure `PLAYWRIGHT_MCP_EXTENSION_TOKEN` env var (from extension settings) or in MCP config +> **Note**: You must be logged into the target website in Chrome before running commands. Tabs opened during command execution are auto-closed afterwards. + Public API commands (`hackernews`, `github search`, `v2ex`) need no browser. ## Commands Reference diff --git a/src/browser.ts b/src/browser.ts index 9c0155a..310b0c2 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -199,19 +199,19 @@ export class PlaywrightMCP { async close(): Promise { try { - // Close extension tab(s) opened during this session + // Close tabs opened during this session (site tabs + extension tabs) if (this._page && this._proc && !this._proc.killed) { try { const tabs = await this._page.tabs(); const tabStr = typeof tabs === 'string' ? tabs : JSON.stringify(tabs); - // Find and close chrome-extension:// tabs (opened by MCP Bridge) - const tabMatches = tabStr.match(/Tab (\d+).*?chrome-extension:\/\//g); - if (tabMatches) { - for (const match of tabMatches) { - const idx = match.match(/Tab (\d+)/)?.[1]; - if (idx) { - try { await this._page.closeTab(parseInt(idx)); } catch {} - } + const allTabs = tabStr.match(/Tab (\d+)/g) || []; + const currentTabCount = allTabs.length; + + // Close tabs in reverse order to avoid index shifting issues + // Keep the original tabs that existed before the command started + if (currentTabCount > this._initialTabCount && this._initialTabCount > 0) { + for (let i = currentTabCount - 1; i >= this._initialTabCount; i--) { + try { await this._page.closeTab(i); } catch {} } } } catch {}