chrome-cdp-cli 2.0.4 → 2.0.5

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.
@@ -161,7 +161,8 @@ class ArgumentParser {
161
161
  { name: 'quiet', short: 'q', type: 'boolean', description: 'Enable quiet mode', default: false },
162
162
  { name: 'timeout', short: 't', type: 'number', description: 'Command timeout in milliseconds', default: 30000 },
163
163
  { name: 'debug', short: 'd', type: 'boolean', description: 'Enable debug logging', default: false },
164
- { name: 'config', short: 'c', type: 'string', description: 'Configuration file path' }
164
+ { name: 'config', short: 'c', type: 'string', description: 'Configuration file path' },
165
+ { name: 'target-index', type: 'number', description: 'Target page index (1-based, excludes DevTools windows)' }
165
166
  ];
166
167
  while (i < args.length) {
167
168
  const arg = args[i];
@@ -94,7 +94,12 @@ class CLIApplication {
94
94
  await this.ensureProxyReady();
95
95
  if (this.needsConnection(command.name)) {
96
96
  this.logger.debug('Command needs connection, ensuring connection...');
97
- await this.ensureConnection(command);
97
+ try {
98
+ await this.ensureConnection(command);
99
+ }
100
+ catch (connectionError) {
101
+ this.logger.debug('Connection failed, will be handled by command router:', connectionError);
102
+ }
98
103
  }
99
104
  this.logger.debug('Executing command via CLI interface...');
100
105
  const result = await this.cli.execute(command);
@@ -128,6 +133,26 @@ class CLIApplication {
128
133
  ];
129
134
  return !noConnectionCommands.includes(commandName);
130
135
  }
136
+ isDevToolsWindow(target) {
137
+ const url = target.url.toLowerCase();
138
+ const title = target.title.toLowerCase();
139
+ return url.startsWith('chrome-devtools://') ||
140
+ url.startsWith('devtools://') ||
141
+ title.includes('devtools') ||
142
+ title.includes('chrome devtools');
143
+ }
144
+ displayAvailableTargets(targets) {
145
+ console.log('\nAvailable Chrome pages (excluding DevTools windows):');
146
+ targets.forEach((target, index) => {
147
+ const displayUrl = target.url.length > 60 ? target.url.substring(0, 57) + '...' : target.url;
148
+ console.log(` [${index + 1}] ${target.title || '(Untitled)'}`);
149
+ console.log(` ${displayUrl}`);
150
+ });
151
+ console.log('\nOptions:');
152
+ console.log(' 1. Use --target-index <number> to select a specific page');
153
+ console.log(' Example: chrome-cdp-cli --target-index 1 eval "document.title"');
154
+ console.log(' 2. Close other pages until only one page remains\n');
155
+ }
131
156
  async ensureConnection(command) {
132
157
  if (this.client) {
133
158
  return;
@@ -138,14 +163,35 @@ class CLIApplication {
138
163
  throw new Error(`No Chrome targets found at ${command.config.host}:${command.config.port}. ` +
139
164
  'Make sure Chrome is running with --remote-debugging-port=9222');
140
165
  }
141
- const pageTarget = targets.find(target => target.type === 'page');
142
- if (!pageTarget) {
143
- throw new Error('No page targets available. Open a tab in Chrome.');
166
+ const pageTargets = targets.filter(target => target.type === 'page');
167
+ const nonDevToolsTargets = pageTargets.filter(target => !this.isDevToolsWindow(target));
168
+ if (nonDevToolsTargets.length === 0) {
169
+ throw new Error('No page targets available (excluding DevTools windows). Open a tab in Chrome.');
144
170
  }
145
- this.client = await this.connectionManager.connectToTarget(pageTarget);
171
+ let selectedTarget;
172
+ if (command.config.targetIndex !== undefined) {
173
+ const index = command.config.targetIndex - 1;
174
+ if (index < 0 || index >= nonDevToolsTargets.length) {
175
+ this.displayAvailableTargets(nonDevToolsTargets);
176
+ throw new Error(`Invalid target index: ${command.config.targetIndex}. ` +
177
+ `Please choose a number between 1 and ${nonDevToolsTargets.length}.`);
178
+ }
179
+ selectedTarget = nonDevToolsTargets[index];
180
+ }
181
+ else {
182
+ if (nonDevToolsTargets.length === 1) {
183
+ selectedTarget = nonDevToolsTargets[0];
184
+ }
185
+ else {
186
+ this.displayAvailableTargets(nonDevToolsTargets);
187
+ throw new Error(`Multiple Chrome pages found (${nonDevToolsTargets.length}). ` +
188
+ 'Please specify --target-index <number> to select a page, or close other pages until only one remains.');
189
+ }
190
+ }
191
+ this.client = await this.connectionManager.connectToTarget(selectedTarget);
146
192
  this.cli.setClient(this.client);
147
193
  if (command.config.verbose) {
148
- this.logger.info(`Connected to Chrome target: ${pageTarget.title} (${pageTarget.url})`);
194
+ this.logger.info(`Connected to Chrome target: ${selectedTarget.title} (${selectedTarget.url})`);
149
195
  }
150
196
  }
151
197
  catch (error) {
@@ -208,6 +254,11 @@ class CLIApplication {
208
254
  else if (arg === '--debug' || arg === '-d') {
209
255
  options.debug = true;
210
256
  }
257
+ else if (arg === '--target-index') {
258
+ if (i + 1 < args.length) {
259
+ options.targetIndex = parseInt(args[i + 1], 10);
260
+ }
261
+ }
211
262
  }
212
263
  return options;
213
264
  }
@@ -72,7 +72,8 @@ class EnhancedCLIInterface {
72
72
  verbose: globalOptions.verbose || CLIInterface_1.DEFAULT_CLI_CONFIG.verbose,
73
73
  quiet: globalOptions.quiet || CLIInterface_1.DEFAULT_CLI_CONFIG.quiet,
74
74
  timeout: globalOptions.timeout || CLIInterface_1.DEFAULT_CLI_CONFIG.timeout,
75
- debug: globalOptions.debug || CLIInterface_1.DEFAULT_CLI_CONFIG.debug
75
+ debug: globalOptions.debug || CLIInterface_1.DEFAULT_CLI_CONFIG.debug,
76
+ targetIndex: globalOptions['target-index'] !== undefined ? globalOptions['target-index'] : undefined
76
77
  };
77
78
  }
78
79
  buildCommandArguments(parseResult) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chrome-cdp-cli",
3
- "version": "2.0.4",
3
+ "version": "2.0.5",
4
4
  "description": "Browser automation CLI via Chrome DevTools Protocol. Designed for developers and AI assistants - combines dedicated commands for common tasks with flexible JavaScript execution for complex scenarios. Features: element interaction, screenshots, DOM snapshots, console/network monitoring. Built-in IDE integration for Cursor and Claude.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",