@jackwener/opencli 0.4.6 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/browser.ts CHANGED
@@ -1,114 +1,16 @@
1
1
  /**
2
- * Browser interaction via Chrome DevTools Protocol.
3
- * Connects to an existing Chrome browser through CDP auto-discovery or extension bridge.
2
+ * Browser interaction via Playwright MCP Bridge extension.
3
+ * Connects to an existing Chrome browser through the extension.
4
4
  */
5
5
 
6
6
  import { spawn, execSync, type ChildProcess } from 'node:child_process';
7
7
  import { createHash } from 'node:crypto';
8
- import * as http from 'node:http';
9
- import * as net from 'node:net';
10
8
  import { fileURLToPath } from 'node:url';
11
9
  import * as fs from 'node:fs';
12
10
  import * as os from 'node:os';
13
11
  import * as path from 'node:path';
14
12
  import { formatSnapshot } from './snapshotFormatter.js';
15
13
 
16
- /**
17
- * Chrome 144+ auto-discovery: read DevToolsActivePort file to get CDP endpoint.
18
- *
19
- * Starting with Chrome 144, users can enable remote debugging from
20
- * chrome://inspect#remote-debugging without any command-line flags.
21
- * Chrome writes the active port and browser GUID to a DevToolsActivePort file
22
- * in the user data directory, which we read to construct the WebSocket endpoint.
23
- *
24
- * Priority: OPENCLI_CDP_ENDPOINT env > DevToolsActivePort auto-discovery > --extension fallback
25
- */
26
-
27
- /** Quick TCP port probe to verify Chrome is actually listening */
28
- function isPortReachable(port: number, host = '127.0.0.1', timeoutMs = 800): Promise<boolean> {
29
- return new Promise(resolve => {
30
- const sock = net.createConnection({ port, host });
31
- sock.setTimeout(timeoutMs);
32
- sock.on('connect', () => { sock.destroy(); resolve(true); });
33
- sock.on('error', () => resolve(false));
34
- sock.on('timeout', () => { sock.destroy(); resolve(false); });
35
- });
36
- }
37
-
38
- /**
39
- * Verify the CDP HTTP JSON API is functional.
40
- * Chrome's chrome://inspect#remote-debugging mode writes DevToolsActivePort
41
- * but doesn't expose the full CDP HTTP API (/json/version), which means
42
- * Playwright's connectOverCDP won't work properly (init succeeds but
43
- * all tool calls hang silently).
44
- */
45
- export function isCdpApiAvailable(port: number, host = '127.0.0.1', timeoutMs = 2000): Promise<boolean> {
46
- return new Promise(resolve => {
47
- const req = http.get(`http://${host}:${port}/json/version`, { timeout: timeoutMs }, (res) => {
48
- let body = '';
49
- res.on('data', (chunk: Buffer) => { body += chunk.toString(); });
50
- res.on('end', () => {
51
- try {
52
- const data = JSON.parse(body);
53
- // A valid CDP endpoint returns { Browser, ... } with a webSocketDebuggerUrl
54
- resolve(!!data && typeof data === 'object' && !!data.Browser);
55
- } catch {
56
- resolve(false);
57
- }
58
- });
59
- });
60
- req.on('error', () => resolve(false));
61
- req.on('timeout', () => { req.destroy(); resolve(false); });
62
- });
63
- }
64
-
65
- export async function discoverChromeEndpoint(): Promise<string | null> {
66
- const candidates: string[] = [];
67
-
68
- // User-specified Chrome data dir takes highest priority
69
- if (process.env.CHROME_USER_DATA_DIR) {
70
- candidates.push(path.join(process.env.CHROME_USER_DATA_DIR, 'DevToolsActivePort'));
71
- }
72
-
73
- // Standard Chrome/Edge user data dirs per platform
74
- if (process.platform === 'win32') {
75
- const localAppData = process.env.LOCALAPPDATA ?? path.join(os.homedir(), 'AppData', 'Local');
76
- candidates.push(path.join(localAppData, 'Google', 'Chrome', 'User Data', 'DevToolsActivePort'));
77
- candidates.push(path.join(localAppData, 'Microsoft', 'Edge', 'User Data', 'DevToolsActivePort'));
78
- } else if (process.platform === 'darwin') {
79
- candidates.push(path.join(os.homedir(), 'Library', 'Application Support', 'Google', 'Chrome', 'DevToolsActivePort'));
80
- candidates.push(path.join(os.homedir(), 'Library', 'Application Support', 'Microsoft Edge', 'DevToolsActivePort'));
81
- } else {
82
- candidates.push(path.join(os.homedir(), '.config', 'google-chrome', 'DevToolsActivePort'));
83
- candidates.push(path.join(os.homedir(), '.config', 'chromium', 'DevToolsActivePort'));
84
- candidates.push(path.join(os.homedir(), '.config', 'microsoft-edge', 'DevToolsActivePort'));
85
- }
86
-
87
- for (const filePath of candidates) {
88
- try {
89
- const content = fs.readFileSync(filePath, 'utf-8').trim();
90
- const lines = content.split('\n');
91
- if (lines.length >= 2) {
92
- const port = parseInt(lines[0], 10);
93
- const browserPath = lines[1]; // e.g. /devtools/browser/<GUID>
94
- if (port > 0 && browserPath.startsWith('/devtools/browser/')) {
95
- const endpoint = `ws://127.0.0.1:${port}${browserPath}`;
96
- // Verify the port is actually reachable (Chrome may have closed, leaving a stale file)
97
- if (await isPortReachable(port)) {
98
- // Verify CDP HTTP API is functional — chrome://inspect#remote-debugging
99
- // writes DevToolsActivePort but doesn't expose the full CDP API,
100
- // causing Playwright connectOverCDP to hang on all tool calls.
101
- if (await isCdpApiAvailable(port)) {
102
- return endpoint;
103
- }
104
- }
105
- }
106
- }
107
- } catch {}
108
- }
109
- return null;
110
- }
111
-
112
14
  // Read version from package.json (single source of truth)
113
15
  const __browser_dirname = path.dirname(fileURLToPath(import.meta.url));
114
16
  const PKG_VERSION = (() => { try { return JSON.parse(fs.readFileSync(path.resolve(__browser_dirname, '..', 'package.json'), 'utf-8')).version; } catch { return '0.0.0'; } })();
@@ -116,17 +18,14 @@ const PKG_VERSION = (() => { try { return JSON.parse(fs.readFileSync(path.resolv
116
18
  const CONNECT_TIMEOUT = parseInt(process.env.OPENCLI_BROWSER_CONNECT_TIMEOUT ?? '30', 10);
117
19
  const STDERR_BUFFER_LIMIT = 16 * 1024;
118
20
  const INITIAL_TABS_TIMEOUT_MS = 1500;
119
- const CDP_READINESS_PROBE_TIMEOUT_MS = 5000;
120
21
  const TAB_CLEANUP_TIMEOUT_MS = 2000;
121
22
  let _cachedMcpServerPath: string | null | undefined;
122
23
 
123
- type ConnectFailureKind = 'missing-token' | 'extension-timeout' | 'extension-not-installed' | 'cdp-timeout' | 'mcp-init' | 'process-exit' | 'unknown';
24
+ type ConnectFailureKind = 'missing-token' | 'extension-timeout' | 'extension-not-installed' | 'mcp-init' | 'process-exit' | 'unknown';
124
25
  type PlaywrightMCPState = 'idle' | 'connecting' | 'connected' | 'closing' | 'closed';
125
- type PlaywrightMCPMode = 'extension' | 'cdp' | null;
126
26
 
127
27
  type ConnectFailureInput = {
128
28
  kind: ConnectFailureKind;
129
- mode: 'extension' | 'cdp';
130
29
  timeout: number;
131
30
  hasExtensionToken: boolean;
132
31
  tokenFingerprint?: string | null;
@@ -145,41 +44,31 @@ export function formatBrowserConnectError(input: ConnectFailureInput): Error {
145
44
  const suffix = stderr ? `\n\nMCP stderr:\n${stderr}` : '';
146
45
  const tokenHint = input.tokenFingerprint ? ` Token fingerprint: ${input.tokenFingerprint}.` : '';
147
46
 
148
- if (input.mode === 'extension') {
149
- if (input.kind === 'missing-token') {
150
- return new Error(
151
- 'Failed to connect to Playwright MCP Bridge: PLAYWRIGHT_MCP_EXTENSION_TOKEN is not set.\n\n' +
152
- 'Without this token, Chrome will show a manual approval dialog for every new MCP connection. ' +
153
- 'Copy the token from the Playwright MCP Bridge extension and set it in BOTH your shell environment and MCP client config.' +
154
- suffix,
155
- );
156
- }
157
-
158
- if (input.kind === 'extension-not-installed') {
159
- return new Error(
160
- 'Failed to connect to Playwright MCP Bridge: the browser extension did not attach.\n\n' +
161
- 'Make sure Chrome is running and the "Playwright MCP Bridge" extension is installed and enabled. ' +
162
- 'If Chrome shows an approval dialog, click Allow.' +
163
- suffix,
164
- );
165
- }
47
+ if (input.kind === 'missing-token') {
48
+ return new Error(
49
+ 'Failed to connect to Playwright MCP Bridge: PLAYWRIGHT_MCP_EXTENSION_TOKEN is not set.\n\n' +
50
+ 'Without this token, Chrome will show a manual approval dialog for every new MCP connection. ' +
51
+ 'Copy the token from the Playwright MCP Bridge extension and set it in BOTH your shell environment and MCP client config.' +
52
+ suffix,
53
+ );
54
+ }
166
55
 
167
- if (input.kind === 'extension-timeout') {
168
- const likelyCause = input.hasExtensionToken
169
- ? `The most likely cause is that PLAYWRIGHT_MCP_EXTENSION_TOKEN does not match the token currently shown by the browser extension.${tokenHint} Re-copy the token from the extension and update BOTH your shell environment and MCP client config.`
170
- : 'PLAYWRIGHT_MCP_EXTENSION_TOKEN is not configured, so the extension may be waiting for manual approval.';
171
- return new Error(
172
- `Timed out connecting to Playwright MCP Bridge (${input.timeout}s).\n\n` +
173
- `${likelyCause} If a browser prompt is visible, click Allow. You can also switch to Chrome remote debugging mode with OPENCLI_USE_CDP=1 as a fallback.` +
174
- suffix,
175
- );
176
- }
56
+ if (input.kind === 'extension-not-installed') {
57
+ return new Error(
58
+ 'Failed to connect to Playwright MCP Bridge: the browser extension did not attach.\n\n' +
59
+ 'Make sure Chrome is running and the "Playwright MCP Bridge" extension is installed and enabled. ' +
60
+ 'If Chrome shows an approval dialog, click Allow.' +
61
+ suffix,
62
+ );
177
63
  }
178
64
 
179
- if (input.mode === 'cdp' && input.kind === 'cdp-timeout') {
65
+ if (input.kind === 'extension-timeout') {
66
+ const likelyCause = input.hasExtensionToken
67
+ ? `The most likely cause is that PLAYWRIGHT_MCP_EXTENSION_TOKEN does not match the token currently shown by the browser extension.${tokenHint} Re-copy the token from the extension and update BOTH your shell environment and MCP client config.`
68
+ : 'PLAYWRIGHT_MCP_EXTENSION_TOKEN is not configured, so the extension may be waiting for manual approval.';
180
69
  return new Error(
181
- `Timed out connecting to browser via CDP (${input.timeout}s).\n\n` +
182
- 'Make sure Chrome is running and remote debugging is enabled at chrome://inspect#remote-debugging, or set OPENCLI_CDP_ENDPOINT explicitly.' +
70
+ `Timed out connecting to Playwright MCP Bridge (${input.timeout}s).\n\n` +
71
+ `${likelyCause} If a browser prompt is visible, click Allow.` +
183
72
  suffix,
184
73
  );
185
74
  }
@@ -199,7 +88,6 @@ export function formatBrowserConnectError(input: ConnectFailureInput): Error {
199
88
  }
200
89
 
201
90
  function inferConnectFailureKind(args: {
202
- mode: 'extension' | 'cdp';
203
91
  hasExtensionToken: boolean;
204
92
  stderr: string;
205
93
  rawMessage?: string;
@@ -207,7 +95,7 @@ function inferConnectFailureKind(args: {
207
95
  }): ConnectFailureKind {
208
96
  const haystack = `${args.rawMessage ?? ''}\n${args.stderr}`.toLowerCase();
209
97
 
210
- if (args.mode === 'extension' && !args.hasExtensionToken)
98
+ if (!args.hasExtensionToken)
211
99
  return 'missing-token';
212
100
  if (haystack.includes('extension connection timeout') || haystack.includes('playwright mcp bridge'))
213
101
  return 'extension-not-installed';
@@ -215,11 +103,7 @@ function inferConnectFailureKind(args: {
215
103
  return 'mcp-init';
216
104
  if (args.exited)
217
105
  return 'process-exit';
218
- if (args.mode === 'extension')
219
- return 'extension-timeout';
220
- if (args.mode === 'cdp')
221
- return 'cdp-timeout';
222
- return 'unknown';
106
+ return 'extension-timeout';
223
107
  }
224
108
 
225
109
  // JSON-RPC helpers
@@ -461,7 +345,6 @@ export class PlaywrightMCP {
461
345
  private _initialTabIdentities: string[] = [];
462
346
  private _closingPromise: Promise<void> | null = null;
463
347
  private _state: PlaywrightMCPState = 'idle';
464
- private _mode: PlaywrightMCPMode = null;
465
348
 
466
349
  private _page: Page | null = null;
467
350
 
@@ -496,7 +379,6 @@ export class PlaywrightMCP {
496
379
  this._page = null;
497
380
  this._proc = null;
498
381
  this._buffer = '';
499
- this._mode = null;
500
382
  this._initialTabIdentities = [];
501
383
  this._rejectPendingRequests(new Error('Playwright MCP connect failed'));
502
384
  PlaywrightMCP._activeInsts.delete(this);
@@ -505,7 +387,7 @@ export class PlaywrightMCP {
505
387
  }
506
388
  }
507
389
 
508
- async connect(opts: { timeout?: number; forceExtension?: boolean } = {}): Promise<Page> {
390
+ async connect(opts: { timeout?: number } = {}): Promise<Page> {
509
391
  if (this._state === 'connected' && this._page) return this._page;
510
392
  if (this._state === 'connecting') throw new Error('Playwright MCP is already connecting');
511
393
  if (this._state === 'closing') throw new Error('Playwright MCP is closing');
@@ -519,26 +401,9 @@ export class PlaywrightMCP {
519
401
  this._state = 'connecting';
520
402
  const timeout = opts.timeout ?? CONNECT_TIMEOUT;
521
403
 
522
- // Connection priority:
523
- // 1. OPENCLI_CDP_ENDPOINT env var → explicit CDP endpoint
524
- // 2. OPENCLI_USE_CDP=1 → auto-discover via DevToolsActivePort
525
- // 3. Default → --extension mode (Playwright MCP Bridge)
526
- // Some anti-bot sites (e.g. BOSS Zhipin) detect CDP — use forceExtension to bypass.
527
- const forceExt = opts.forceExtension || process.env.OPENCLI_FORCE_EXTENSION === '1';
528
- let cdpEndpoint: string | null = null;
529
- if (!forceExt) {
530
- if (process.env.OPENCLI_CDP_ENDPOINT) {
531
- cdpEndpoint = process.env.OPENCLI_CDP_ENDPOINT;
532
- } else if (process.env.OPENCLI_USE_CDP === '1') {
533
- cdpEndpoint = await discoverChromeEndpoint();
534
- }
535
- }
536
-
537
404
  return new Promise<Page>((resolve, reject) => {
538
405
  const isDebug = process.env.DEBUG?.includes('opencli:mcp');
539
406
  const debugLog = (msg: string) => isDebug && console.error(`[opencli:mcp] ${msg}`);
540
- const mode: 'extension' | 'cdp' = cdpEndpoint ? 'cdp' : 'extension';
541
- this._mode = mode;
542
407
  const extensionToken = process.env.PLAYWRIGHT_MCP_EXTENSION_TOKEN;
543
408
  const tokenFingerprint = getTokenFingerprint(extensionToken);
544
409
  let stderrBuffer = '';
@@ -552,7 +417,6 @@ export class PlaywrightMCP {
552
417
  this._resetAfterFailedConnect();
553
418
  reject(formatBrowserConnectError({
554
419
  kind,
555
- mode,
556
420
  timeout,
557
421
  hasExtensionToken: !!extensionToken,
558
422
  tokenFingerprint,
@@ -573,23 +437,14 @@ export class PlaywrightMCP {
573
437
  const timer = setTimeout(() => {
574
438
  debugLog('Connection timed out');
575
439
  settleError(inferConnectFailureKind({
576
- mode,
577
440
  hasExtensionToken: !!extensionToken,
578
441
  stderr: stderrBuffer,
579
442
  }));
580
443
  }, timeout * 1000);
581
444
 
582
- const mcpArgs: string[] = [mcpPath];
583
- if (cdpEndpoint) {
584
- mcpArgs.push('--cdp-endpoint', cdpEndpoint);
585
- } else {
586
- mcpArgs.push('--extension');
587
- }
445
+ const mcpArgs: string[] = [mcpPath, '--extension'];
588
446
  if (process.env.OPENCLI_VERBOSE) {
589
- console.error(`[opencli] CDP mode: ${cdpEndpoint ? `auto-discovered ${cdpEndpoint}` : 'fallback to --extension'}`);
590
- if (mode === 'extension') {
591
- console.error(`[opencli] Extension token: ${extensionToken ? `configured (fingerprint ${tokenFingerprint})` : 'missing'}`);
592
- }
447
+ console.error(`[opencli] Extension token: ${extensionToken ? `configured (fingerprint ${tokenFingerprint})` : 'missing'}`);
593
448
  }
594
449
  if (process.env.OPENCLI_BROWSER_EXECUTABLE_PATH) {
595
450
  mcpArgs.push('--executablePath', process.env.OPENCLI_BROWSER_EXECUTABLE_PATH);
@@ -645,7 +500,6 @@ export class PlaywrightMCP {
645
500
  this._rejectPendingRequests(new Error(`Playwright MCP process exited before response${code == null ? '' : ` (code ${code})`}`));
646
501
  if (!settled) {
647
502
  settleError(inferConnectFailureKind({
648
- mode,
649
503
  hasExtensionToken: !!extensionToken,
650
504
  stderr: stderrBuffer,
651
505
  exited: true,
@@ -663,7 +517,6 @@ export class PlaywrightMCP {
663
517
  debugLog('Got initialize response');
664
518
  if (resp.error) {
665
519
  settleError(inferConnectFailureKind({
666
- mode,
667
520
  hasExtensionToken: !!extensionToken,
668
521
  stderr: stderrBuffer,
669
522
  rawMessage: `MCP init failed: ${resp.error.message}`,
@@ -675,29 +528,7 @@ export class PlaywrightMCP {
675
528
  debugLog(`SEND: ${initializedMsg.trim()}`);
676
529
  this._proc?.stdin?.write(initializedMsg);
677
530
 
678
- if (mode === 'cdp') {
679
- // CDP readiness probe: verify tool calls actually work.
680
- // Some CDP endpoints (e.g. chrome://inspect mode) accept WebSocket
681
- // connections and respond to MCP init but silently drop tool calls.
682
- debugLog('CDP readiness probe (tabs)...');
683
- withTimeout(page.tabs(), CDP_READINESS_PROBE_TIMEOUT_MS, 'CDP readiness probe timed out')
684
- .then(() => {
685
- debugLog('CDP readiness probe succeeded');
686
- settleSuccess(page);
687
- })
688
- .catch((err) => {
689
- debugLog(`CDP readiness probe failed: ${err.message}`);
690
- settleError('cdp-timeout', {
691
- rawMessage: 'CDP endpoint connected but tool calls are unresponsive. ' +
692
- 'This usually means Chrome was opened with chrome://inspect#remote-debugging ' +
693
- 'which is not fully compatible. Launch Chrome with --remote-debugging-port=9222 instead, ' +
694
- 'or use the Playwright MCP Bridge extension (default mode).',
695
- });
696
- });
697
- return;
698
- }
699
-
700
- // Extension mode uses tabs as a readiness probe and for tab cleanup bookkeeping.
531
+ // Use tabs as a readiness probe and for tab cleanup bookkeeping.
701
532
  debugLog('Fetching initial tabs count...');
702
533
  withTimeout(page.tabs(), INITIAL_TABS_TIMEOUT_MS, 'Timed out fetching initial tabs').then((tabs: any) => {
703
534
  debugLog(`Tabs response: ${typeof tabs === 'string' ? tabs : JSON.stringify(tabs)}`);
@@ -714,6 +545,7 @@ export class PlaywrightMCP {
714
545
  });
715
546
  }
716
547
 
548
+
717
549
  async close(): Promise<void> {
718
550
  if (this._closingPromise) return this._closingPromise;
719
551
  if (this._state === 'closed') return;
@@ -721,7 +553,7 @@ export class PlaywrightMCP {
721
553
  this._closingPromise = (async () => {
722
554
  try {
723
555
  // Extension mode opens bridge/session tabs that we can clean up best-effort.
724
- if (this._mode === 'extension' && this._page && this._proc && !this._proc.killed) {
556
+ if (this._page && this._proc && !this._proc.killed) {
725
557
  try {
726
558
  const tabs = await withTimeout(this._page.tabs(), TAB_CLEANUP_TIMEOUT_MS, 'Timed out fetching tabs during cleanup');
727
559
  const tabEntries = extractTabEntries(tabs);
@@ -751,7 +583,6 @@ export class PlaywrightMCP {
751
583
  this._rejectPendingRequests(new Error('Playwright MCP session closed'));
752
584
  this._page = null;
753
585
  this._proc = null;
754
- this._mode = null;
755
586
  this._state = 'closed';
756
587
  PlaywrightMCP._activeInsts.delete(this);
757
588
  }
@@ -779,12 +610,23 @@ function extractTabEntries(raw: any): Array<{ index: number; identity: string }>
779
610
  .map(line => line.trim())
780
611
  .filter(Boolean)
781
612
  .map(line => {
782
- const match = line.match(/Tab\s+(\d+)\s*(.*)$/);
783
- if (!match) return null;
784
- return {
785
- index: parseInt(match[1], 10),
786
- identity: match[2].trim() || `tab-${match[1]}`,
787
- };
613
+ // Match actual Playwright MCP format: "- 0: (current) [title](url)" or "- 1: [title](url)"
614
+ const mcpMatch = line.match(/^-\s+(\d+):\s*(.*)$/);
615
+ if (mcpMatch) {
616
+ return {
617
+ index: parseInt(mcpMatch[1], 10),
618
+ identity: mcpMatch[2].trim() || `tab-${mcpMatch[1]}`,
619
+ };
620
+ }
621
+ // Legacy format: "Tab 0 ..."
622
+ const legacyMatch = line.match(/Tab\s+(\d+)\s*(.*)$/);
623
+ if (legacyMatch) {
624
+ return {
625
+ index: parseInt(legacyMatch[1], 10),
626
+ identity: legacyMatch[2].trim() || `tab-${legacyMatch[1]}`,
627
+ };
628
+ }
629
+ return null;
788
630
  })
789
631
  .filter((entry): entry is { index: number; identity: string } => entry !== null);
790
632
  }
@@ -844,7 +686,6 @@ export const __test__ = {
844
686
  diffTabIndexes,
845
687
  appendLimited,
846
688
  withTimeout,
847
- isCdpApiAvailable,
848
689
  };
849
690
 
850
691
  function findMcpServerPath(): string | null {
@@ -69,7 +69,7 @@ cli({
69
69
  description: 'BOSS直聘搜索职位',
70
70
  domain: 'www.zhipin.com',
71
71
  strategy: Strategy.COOKIE,
72
- forceExtension: true, // BOSS Zhipin detects CDP mode — must use extension bridge
72
+
73
73
  browser: true,
74
74
  args: [
75
75
  { name: 'query', required: true, help: 'Search keyword (e.g. AI agent, 前端)' },
@@ -11,7 +11,7 @@ cli({
11
11
  domain: 'www.v2ex.com',
12
12
  strategy: Strategy.COOKIE,
13
13
  browser: true,
14
- forceExtension: true,
14
+
15
15
  args: [],
16
16
  columns: ['status', 'message'],
17
17
  func: async (page: IPage | null) => {
@@ -11,7 +11,7 @@ cli({
11
11
  domain: 'www.v2ex.com',
12
12
  strategy: Strategy.COOKIE,
13
13
  browser: true,
14
- forceExtension: true,
14
+
15
15
  args: [],
16
16
  columns: ['username', 'balance', 'unread_notifications', 'daily_reward_ready'],
17
17
  func: async (page: IPage | null) => {
@@ -11,7 +11,7 @@ cli({
11
11
  domain: 'www.v2ex.com',
12
12
  strategy: Strategy.COOKIE,
13
13
  browser: true,
14
- forceExtension: true,
14
+
15
15
  args: [
16
16
  { name: 'limit', type: 'int', default: 20, help: 'Number of notifications' }
17
17
  ],
@@ -92,18 +92,12 @@ describe('doctor report rendering', () => {
92
92
  envFingerprint: 'fp1',
93
93
  shellFiles: [{ path: '/tmp/.zshrc', exists: true, token: 'abc123', fingerprint: 'fp1' }],
94
94
  configs: [{ path: '/tmp/mcp.json', exists: true, format: 'json', token: 'abc123', fingerprint: 'fp1', writable: true }],
95
- remoteDebuggingEnabled: true,
96
- remoteDebuggingEndpoint: 'ws://127.0.0.1:9222/devtools/browser/test',
97
- cdpEnabled: false,
98
- cdpToken: null,
99
- cdpFingerprint: null,
100
95
  recommendedToken: 'abc123',
101
96
  recommendedFingerprint: 'fp1',
102
97
  warnings: [],
103
98
  issues: [],
104
99
  });
105
100
 
106
- expect(text).toContain('[OK] Chrome remote debugging: enabled');
107
101
  expect(text).toContain('[OK] Environment token: configured (fp1)');
108
102
  expect(text).toContain('[OK] MCP config /tmp/mcp.json: configured (fp1)');
109
103
  });
@@ -114,18 +108,12 @@ describe('doctor report rendering', () => {
114
108
  envFingerprint: 'fp1',
115
109
  shellFiles: [{ path: '/tmp/.zshrc', exists: true, token: 'def456', fingerprint: 'fp2' }],
116
110
  configs: [{ path: '/tmp/mcp.json', exists: true, format: 'json', token: 'abc123', fingerprint: 'fp1', writable: true }],
117
- remoteDebuggingEnabled: false,
118
- remoteDebuggingEndpoint: null,
119
- cdpEnabled: false,
120
- cdpToken: null,
121
- cdpFingerprint: null,
122
111
  recommendedToken: 'abc123',
123
112
  recommendedFingerprint: 'fp1',
124
- warnings: ['Chrome remote debugging appears to be disabled or Chrome is not currently exposing a DevTools endpoint.'],
113
+ warnings: [],
125
114
  issues: ['Detected inconsistent Playwright MCP tokens across env/config files.'],
126
115
  });
127
116
 
128
- expect(text).toContain('[WARN] Chrome remote debugging: disabled');
129
117
  expect(text).toContain('[MISMATCH] Environment token: configured (fp1)');
130
118
  expect(text).toContain('[MISMATCH] Shell file /tmp/.zshrc: configured (fp2)');
131
119
  expect(text).toContain('[MISMATCH] Recommended token fingerprint: fp1');
package/src/doctor.ts CHANGED
@@ -4,7 +4,7 @@ import * as path from 'node:path';
4
4
  import { createInterface } from 'node:readline/promises';
5
5
  import { stdin as input, stdout as output } from 'node:process';
6
6
  import type { IPage } from './types.js';
7
- import { PlaywrightMCP, discoverChromeEndpoint, getTokenFingerprint } from './browser.js';
7
+ import { PlaywrightMCP, getTokenFingerprint } from './browser.js';
8
8
  import { browserSession } from './runtime.js';
9
9
 
10
10
  const PLAYWRIGHT_SERVER_NAME = 'playwright';
@@ -45,11 +45,6 @@ export type DoctorReport = {
45
45
  envFingerprint: string | null;
46
46
  shellFiles: ShellFileStatus[];
47
47
  configs: McpConfigStatus[];
48
- remoteDebuggingEnabled: boolean;
49
- remoteDebuggingEndpoint: string | null;
50
- cdpEnabled: boolean;
51
- cdpToken: string | null;
52
- cdpFingerprint: string | null;
53
48
  recommendedToken: string | null;
54
49
  recommendedFingerprint: string | null;
55
50
  warnings: string[];
@@ -225,45 +220,10 @@ function readConfigStatus(filePath: string): McpConfigStatus {
225
220
  }
226
221
  }
227
222
 
228
- async function extractTokenViaCdp(): Promise<string | null> {
229
- if (!(process.env.OPENCLI_USE_CDP === '1' || process.env.OPENCLI_CDP_ENDPOINT))
230
- return null;
231
- const candidates = [
232
- `chrome-extension://${PLAYWRIGHT_EXTENSION_ID}/options.html`,
233
- `chrome-extension://${PLAYWRIGHT_EXTENSION_ID}/popup.html`,
234
- `chrome-extension://${PLAYWRIGHT_EXTENSION_ID}/connect.html`,
235
- `chrome-extension://${PLAYWRIGHT_EXTENSION_ID}/index.html`,
236
- ];
237
- const result = await browserSession(PlaywrightMCP, async (page: IPage) => {
238
- for (const url of candidates) {
239
- try {
240
- await page.goto(url);
241
- await page.wait(1);
242
- const token = await page.evaluate(`() => {
243
- const values = new Set();
244
- const push = (value) => {
245
- if (!value || typeof value !== 'string') return;
246
- for (const match of value.matchAll(/[A-Za-z0-9_-]{24,}/g)) values.add(match[0]);
247
- };
248
- document.querySelectorAll('input, textarea, code, pre, span, div').forEach((el) => {
249
- push(el.value);
250
- push(el.textContent || '');
251
- push(el.getAttribute && el.getAttribute('value'));
252
- });
253
- return Array.from(values);
254
- }`);
255
- const matches = Array.isArray(token) ? token.filter((v: string) => v.length >= 24) : [];
256
- if (matches.length > 0) return matches.sort((a: string, b: string) => b.length - a.length)[0];
257
- } catch {}
258
- }
259
- return null;
260
- });
261
- return typeof result === 'string' && result ? result : null;
262
- }
223
+
263
224
 
264
225
  export async function runBrowserDoctor(opts: DoctorOptions = {}): Promise<DoctorReport> {
265
226
  const envToken = process.env[PLAYWRIGHT_TOKEN_ENV] ?? null;
266
- const remoteDebuggingEndpoint = await discoverChromeEndpoint().catch(() => null);
267
227
  const shellPath = opts.shellRc ?? getDefaultShellRcPath();
268
228
  const shellFiles: ShellFileStatus[] = [shellPath].map((filePath) => {
269
229
  if (!fileExists(filePath)) return { path: filePath, exists: false, token: null, fingerprint: null };
@@ -273,17 +233,15 @@ export async function runBrowserDoctor(opts: DoctorOptions = {}): Promise<Doctor
273
233
  });
274
234
  const configPaths = opts.configPaths?.length ? opts.configPaths : getDefaultMcpConfigPaths();
275
235
  const configs = configPaths.map(readConfigStatus);
276
- const cdpToken = !opts.token && !envToken ? await extractTokenViaCdp().catch(() => null) : null;
277
236
 
278
237
  const allTokens = [
279
238
  opts.token ?? null,
280
239
  envToken,
281
240
  ...shellFiles.map(s => s.token),
282
241
  ...configs.map(c => c.token),
283
- cdpToken,
284
242
  ].filter((v): v is string => !!v);
285
243
  const uniqueTokens = [...new Set(allTokens)];
286
- const recommendedToken = opts.token ?? envToken ?? (uniqueTokens.length === 1 ? uniqueTokens[0] : cdpToken) ?? null;
244
+ const recommendedToken = opts.token ?? envToken ?? (uniqueTokens.length === 1 ? uniqueTokens[0] : null) ?? null;
287
245
 
288
246
  const report: DoctorReport = {
289
247
  cliVersion: opts.cliVersion,
@@ -291,11 +249,6 @@ export async function runBrowserDoctor(opts: DoctorOptions = {}): Promise<Doctor
291
249
  envFingerprint: getTokenFingerprint(envToken ?? undefined),
292
250
  shellFiles,
293
251
  configs,
294
- remoteDebuggingEnabled: !!remoteDebuggingEndpoint,
295
- remoteDebuggingEndpoint,
296
- cdpEnabled: process.env.OPENCLI_USE_CDP === '1' || !!process.env.OPENCLI_CDP_ENDPOINT,
297
- cdpToken,
298
- cdpFingerprint: getTokenFingerprint(cdpToken ?? undefined),
299
252
  recommendedToken,
300
253
  recommendedFingerprint: getTokenFingerprint(recommendedToken ?? undefined),
301
254
  warnings: [],
@@ -306,13 +259,11 @@ export async function runBrowserDoctor(opts: DoctorOptions = {}): Promise<Doctor
306
259
  if (!shellFiles.some(s => s.token)) report.issues.push('Shell startup file does not export PLAYWRIGHT_MCP_EXTENSION_TOKEN.');
307
260
  if (!configs.some(c => c.token)) report.issues.push('No scanned MCP config currently contains a Playwright extension token.');
308
261
  if (uniqueTokens.length > 1) report.issues.push('Detected inconsistent Playwright MCP tokens across env/config files.');
309
- if (!report.remoteDebuggingEnabled) report.warnings.push('Chrome remote debugging appears to be disabled or Chrome is not currently exposing a DevTools endpoint.');
310
262
  for (const config of configs) {
311
263
  if (config.parseError) report.warnings.push(`Could not parse ${config.path}: ${config.parseError}`);
312
264
  }
313
265
  if (!recommendedToken) {
314
- if (report.cdpEnabled) report.warnings.push('CDP is enabled, but no token could be extracted automatically from the extension UI.');
315
- else report.warnings.push('No token source found. Enable OPENCLI_USE_CDP=1 to allow a best-effort token read from the extension page.');
266
+ report.warnings.push('No token source found.');
316
267
  }
317
268
  return report;
318
269
  }
@@ -326,8 +277,6 @@ export function renderBrowserDoctorReport(report: DoctorReport): string {
326
277
  const uniqueFingerprints = [...new Set(tokenFingerprints)];
327
278
  const hasMismatch = uniqueFingerprints.length > 1;
328
279
  const lines = [`opencli v${report.cliVersion ?? 'unknown'} doctor`, ''];
329
- lines.push(statusLine(report.remoteDebuggingEnabled ? 'OK' : 'WARN', `Chrome remote debugging: ${report.remoteDebuggingEnabled ? 'enabled' : 'disabled'}`));
330
- if (report.remoteDebuggingEndpoint) lines.push(` ${report.remoteDebuggingEndpoint}`);
331
280
 
332
281
  const envStatus: ReportStatus = !report.envToken ? 'MISSING' : hasMismatch ? 'MISMATCH' : 'OK';
333
282
  lines.push(statusLine(envStatus, `Environment token: ${tokenSummary(report.envToken, report.envFingerprint)}`));
@@ -354,10 +303,6 @@ export function renderBrowserDoctorReport(report: DoctorReport): string {
354
303
  lines.push(statusLine('MISSING', 'MCP config: no existing config files found in scanned locations'));
355
304
  }
356
305
  if (missingConfigCount > 0) lines.push(` Other scanned config locations not present: ${missingConfigCount}`);
357
- if (report.cdpEnabled) {
358
- const cdpStatus: ReportStatus = report.cdpToken ? 'OK' : 'WARN';
359
- lines.push(statusLine(cdpStatus, `CDP token probe: ${tokenSummary(report.cdpToken, report.cdpFingerprint)}`));
360
- }
361
306
  lines.push('');
362
307
  lines.push(statusLine(
363
308
  hasMismatch ? 'MISMATCH' : report.recommendedToken ? 'OK' : 'WARN',
@@ -391,7 +336,7 @@ function writeFileWithMkdir(filePath: string, content: string): void {
391
336
 
392
337
  export async function applyBrowserDoctorFix(report: DoctorReport, opts: DoctorOptions = {}): Promise<string[]> {
393
338
  const token = opts.token ?? report.recommendedToken;
394
- if (!token) throw new Error('No Playwright MCP token is available to write. Provide --token or enable CDP token probing first.');
339
+ if (!token) throw new Error('No Playwright MCP token is available to write. Provide --token first.');
395
340
 
396
341
  const plannedWrites: string[] = [];
397
342
  const shellPath = opts.shellRc ?? report.shellFiles[0]?.path ?? getDefaultShellRcPath();
package/src/main.ts CHANGED
@@ -144,7 +144,7 @@ for (const [, cmd] of registry) {
144
144
  if (actionOpts.verbose) process.env.OPENCLI_VERBOSE = '1';
145
145
  let result: any;
146
146
  if (cmd.browser) {
147
- result = await browserSession(PlaywrightMCP, async (page) => runWithTimeout(executeCommand(cmd, page, kwargs, actionOpts.verbose), { timeout: cmd.timeoutSeconds ?? DEFAULT_BROWSER_COMMAND_TIMEOUT, label: fullName(cmd) }), { forceExtension: cmd.forceExtension });
147
+ result = await browserSession(PlaywrightMCP, async (page) => runWithTimeout(executeCommand(cmd, page, kwargs, actionOpts.verbose), { timeout: cmd.timeoutSeconds ?? DEFAULT_BROWSER_COMMAND_TIMEOUT, label: fullName(cmd) }));
148
148
  } else { result = await executeCommand(cmd, null, kwargs, actionOpts.verbose); }
149
149
  if (actionOpts.verbose && (!result || (Array.isArray(result) && result.length === 0))) {
150
150
  console.error(chalk.yellow(`[Verbose] Warning: Command returned an empty result. If the website structural API changed or requires authentication, check the network or update the adapter.`));