chrome-cdp-cli 1.0.0
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/LICENSE +21 -0
- package/README.md +411 -0
- package/dist/cli/CLIApplication.js +92 -0
- package/dist/cli/CLIInterface.js +327 -0
- package/dist/cli/CommandRegistry.js +33 -0
- package/dist/cli/CommandRouter.js +256 -0
- package/dist/cli/index.js +12 -0
- package/dist/client/CDPClient.js +159 -0
- package/dist/client/index.js +17 -0
- package/dist/connection/ConnectionManager.js +164 -0
- package/dist/connection/index.js +5 -0
- package/dist/handlers/EvaluateScriptHandler.js +205 -0
- package/dist/handlers/index.js +17 -0
- package/dist/index.js +44 -0
- package/dist/interfaces/CDPClient.js +2 -0
- package/dist/interfaces/CLIInterface.js +11 -0
- package/dist/interfaces/CommandHandler.js +2 -0
- package/dist/interfaces/ConnectionManager.js +2 -0
- package/dist/interfaces/index.js +20 -0
- package/dist/types/index.js +2 -0
- package/dist/utils/constants.js +31 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/logger.js +44 -0
- package/package.json +81 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Chrome DevTools CLI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
# Chrome DevTools CLI
|
|
2
|
+
|
|
3
|
+
A powerful command-line tool for controlling Chrome browser instances via the Chrome DevTools Protocol (CDP). This tool provides programmatic access to browser automation, debugging, and inspection capabilities without requiring a graphical interface.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔗 **Connection Management**: Connect to local or remote Chrome instances
|
|
8
|
+
- 📄 **Page Management**: Navigate, create, close, and manage browser tabs
|
|
9
|
+
- ⚡ **JavaScript Execution**: Execute JavaScript code in browser context with full async support
|
|
10
|
+
- 📸 **Visual Capture**: Take screenshots and capture HTML content
|
|
11
|
+
- 🖱️ **Element Interaction**: Click, hover, fill forms, and interact with page elements
|
|
12
|
+
- 📊 **Monitoring**: Monitor console messages and network requests in real-time
|
|
13
|
+
- 🚀 **Performance Analysis**: Profile page performance and analyze metrics
|
|
14
|
+
- 📱 **Device Emulation**: Simulate different devices and network conditions
|
|
15
|
+
- 🔧 **Flexible Output**: Support for JSON and human-readable text output formats
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
### From npm (Recommended)
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install -g chrome-cdp-cli
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Using npx (No Installation Required)
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Run directly with npx
|
|
29
|
+
npx chrome-cdp-cli eval "document.title"
|
|
30
|
+
|
|
31
|
+
# All commands work with npx
|
|
32
|
+
npx chrome-cdp-cli eval "Math.PI * 2"
|
|
33
|
+
npx chrome-cdp-cli eval --file script.js
|
|
34
|
+
npx chrome-cdp-cli --help
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### From Source
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
git clone https://github.com/nickxiao42/chrome-devtools-cli.git
|
|
41
|
+
cd chrome-devtools-cli
|
|
42
|
+
npm install
|
|
43
|
+
npm run build
|
|
44
|
+
npm link
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Prerequisites
|
|
48
|
+
|
|
49
|
+
- **Node.js**: Version 18.0.0 or higher
|
|
50
|
+
- **Chrome Browser**: Any recent version with DevTools support
|
|
51
|
+
- **Chrome DevTools**: Must be enabled with remote debugging
|
|
52
|
+
|
|
53
|
+
### Starting Chrome with DevTools
|
|
54
|
+
|
|
55
|
+
Before using the CLI, start Chrome with remote debugging enabled:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# Default port (9222)
|
|
59
|
+
chrome --remote-debugging-port=9222
|
|
60
|
+
|
|
61
|
+
# Custom port
|
|
62
|
+
chrome --remote-debugging-port=9223
|
|
63
|
+
|
|
64
|
+
# Headless mode
|
|
65
|
+
chrome --headless --remote-debugging-port=9222
|
|
66
|
+
|
|
67
|
+
# With additional flags for automation
|
|
68
|
+
chrome --remote-debugging-port=9222 --no-first-run --no-default-browser-check
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Quick Start
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# Connect and execute JavaScript
|
|
75
|
+
chrome-cli eval "document.title"
|
|
76
|
+
|
|
77
|
+
# Or use with npx (no installation needed)
|
|
78
|
+
npx chrome-cdp-cli eval "document.title"
|
|
79
|
+
|
|
80
|
+
# Navigate to a website
|
|
81
|
+
chrome-cli navigate_page "https://example.com"
|
|
82
|
+
|
|
83
|
+
# Take a screenshot
|
|
84
|
+
chrome-cli take_screenshot --output screenshot.png
|
|
85
|
+
|
|
86
|
+
# Click an element
|
|
87
|
+
chrome-cli click "#submit-button"
|
|
88
|
+
|
|
89
|
+
# Fill a form field
|
|
90
|
+
chrome-cli fill "#email" "user@example.com"
|
|
91
|
+
|
|
92
|
+
# Get help for all commands
|
|
93
|
+
chrome-cli --help
|
|
94
|
+
|
|
95
|
+
# Get help for a specific command
|
|
96
|
+
chrome-cli eval --help
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Command Reference
|
|
100
|
+
|
|
101
|
+
### Connection Options
|
|
102
|
+
|
|
103
|
+
All commands support these connection options:
|
|
104
|
+
|
|
105
|
+
- `--host <host>`: Chrome host (default: localhost)
|
|
106
|
+
- `--port <port>`: DevTools port (default: 9222)
|
|
107
|
+
- `--timeout <ms>`: Command timeout in milliseconds (default: 30000)
|
|
108
|
+
|
|
109
|
+
### Output Options
|
|
110
|
+
|
|
111
|
+
- `--format <format>`: Output format - 'json' or 'text' (default: text)
|
|
112
|
+
- `--quiet`: Suppress non-essential output
|
|
113
|
+
- `--verbose`: Enable detailed logging
|
|
114
|
+
|
|
115
|
+
### Core Commands
|
|
116
|
+
|
|
117
|
+
#### JavaScript Execution
|
|
118
|
+
```bash
|
|
119
|
+
# Execute JavaScript expression
|
|
120
|
+
chrome-cli eval "console.log('Hello World')"
|
|
121
|
+
|
|
122
|
+
# Execute from file
|
|
123
|
+
chrome-cli eval --file script.js
|
|
124
|
+
|
|
125
|
+
# Execute with timeout
|
|
126
|
+
chrome-cli eval "await new Promise(r => setTimeout(r, 5000))" --timeout 10000
|
|
127
|
+
|
|
128
|
+
# Using npx (no installation required)
|
|
129
|
+
npx chrome-cdp-cli eval "document.title"
|
|
130
|
+
npx chrome-cdp-cli eval --file script.js
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### Page Management
|
|
134
|
+
```bash
|
|
135
|
+
# Navigate to URL
|
|
136
|
+
chrome-cli navigate_page "https://example.com"
|
|
137
|
+
|
|
138
|
+
# Create new tab
|
|
139
|
+
chrome-cli new_page
|
|
140
|
+
|
|
141
|
+
# List all pages
|
|
142
|
+
chrome-cli list_pages
|
|
143
|
+
|
|
144
|
+
# Close current page
|
|
145
|
+
chrome-cli close_page
|
|
146
|
+
|
|
147
|
+
# Switch to specific page
|
|
148
|
+
chrome-cli select_page --id "page-id"
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### Element Interaction
|
|
152
|
+
```bash
|
|
153
|
+
# Click element
|
|
154
|
+
chrome-cli click "#button"
|
|
155
|
+
|
|
156
|
+
# Fill input field
|
|
157
|
+
chrome-cli fill "#email" "user@example.com"
|
|
158
|
+
|
|
159
|
+
# Hover over element
|
|
160
|
+
chrome-cli hover ".menu-item"
|
|
161
|
+
|
|
162
|
+
# Wait for element
|
|
163
|
+
chrome-cli wait_for "#loading" --timeout 5000
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### Visual Capture
|
|
167
|
+
```bash
|
|
168
|
+
# Take screenshot
|
|
169
|
+
chrome-cli take_screenshot --output screenshot.png
|
|
170
|
+
|
|
171
|
+
# Full page screenshot
|
|
172
|
+
chrome-cli take_snapshot --output fullpage.png
|
|
173
|
+
|
|
174
|
+
# Custom dimensions
|
|
175
|
+
chrome-cli take_screenshot --width 1920 --height 1080 --output custom.png
|
|
176
|
+
|
|
177
|
+
# Get HTML content
|
|
178
|
+
chrome-cli get_html --output page.html
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Configuration
|
|
182
|
+
|
|
183
|
+
### Configuration File
|
|
184
|
+
|
|
185
|
+
Create a `.chrome-cli.json` file in your project root or home directory:
|
|
186
|
+
|
|
187
|
+
```json
|
|
188
|
+
{
|
|
189
|
+
"host": "localhost",
|
|
190
|
+
"port": 9222,
|
|
191
|
+
"timeout": 30000,
|
|
192
|
+
"outputFormat": "text",
|
|
193
|
+
"verbose": false,
|
|
194
|
+
"quiet": false
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Environment Variables
|
|
199
|
+
|
|
200
|
+
- `CHROME_CLI_HOST`: Default Chrome host
|
|
201
|
+
- `CHROME_CLI_PORT`: Default DevTools port
|
|
202
|
+
- `CHROME_CLI_TIMEOUT`: Default command timeout
|
|
203
|
+
- `CHROME_CLI_FORMAT`: Default output format
|
|
204
|
+
|
|
205
|
+
## Development
|
|
206
|
+
|
|
207
|
+
### Setup
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
# Clone repository
|
|
211
|
+
git clone https://github.com/nickxiao42/chrome-devtools-cli.git
|
|
212
|
+
cd chrome-devtools-cli
|
|
213
|
+
|
|
214
|
+
# Install dependencies
|
|
215
|
+
npm install
|
|
216
|
+
|
|
217
|
+
# Run in development mode
|
|
218
|
+
npm run dev -- eval "console.log('Development mode')"
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Build Scripts
|
|
222
|
+
|
|
223
|
+
```bash
|
|
224
|
+
# Development build (with source maps and declarations)
|
|
225
|
+
npm run build
|
|
226
|
+
|
|
227
|
+
# Production build (optimized, no source maps)
|
|
228
|
+
npm run build:prod
|
|
229
|
+
|
|
230
|
+
# Watch mode for development
|
|
231
|
+
npm run build:watch
|
|
232
|
+
|
|
233
|
+
# Clean build artifacts
|
|
234
|
+
npm run clean
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Testing
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
# Run all tests
|
|
241
|
+
npm test
|
|
242
|
+
|
|
243
|
+
# Run tests in watch mode
|
|
244
|
+
npm run test:watch
|
|
245
|
+
|
|
246
|
+
# Generate coverage report
|
|
247
|
+
npm run test:coverage
|
|
248
|
+
|
|
249
|
+
# Run tests for CI (no watch, with coverage)
|
|
250
|
+
npm run test:ci
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Code Quality
|
|
254
|
+
|
|
255
|
+
```bash
|
|
256
|
+
# Lint code
|
|
257
|
+
npm run lint
|
|
258
|
+
|
|
259
|
+
# Fix linting issues
|
|
260
|
+
npm run lint:fix
|
|
261
|
+
|
|
262
|
+
# Verify everything (lint + test + build)
|
|
263
|
+
npm run verify
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Packaging
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
# Create npm package
|
|
270
|
+
npm run package
|
|
271
|
+
|
|
272
|
+
# Prepare for publishing
|
|
273
|
+
npm run prepublishOnly
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Project Structure
|
|
277
|
+
|
|
278
|
+
```
|
|
279
|
+
chrome-devtools-cli/
|
|
280
|
+
├── src/
|
|
281
|
+
│ ├── cli/ # CLI interface and command routing
|
|
282
|
+
│ ├── client/ # CDP client implementation
|
|
283
|
+
│ ├── connection/ # Connection management
|
|
284
|
+
│ ├── handlers/ # Command handlers
|
|
285
|
+
│ ├── interfaces/ # TypeScript interfaces
|
|
286
|
+
│ ├── types/ # Type definitions
|
|
287
|
+
│ ├── utils/ # Utility functions
|
|
288
|
+
│ ├── test/ # Test setup and utilities
|
|
289
|
+
│ └── index.ts # Main entry point
|
|
290
|
+
├── scripts/ # Build and utility scripts
|
|
291
|
+
├── dist/ # Compiled JavaScript output
|
|
292
|
+
├── coverage/ # Test coverage reports
|
|
293
|
+
├── tsconfig.json # TypeScript configuration
|
|
294
|
+
├── tsconfig.prod.json # Production TypeScript config
|
|
295
|
+
├── jest.config.js # Jest test configuration
|
|
296
|
+
├── package.json # Package configuration
|
|
297
|
+
└── README.md # This file
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## API Documentation
|
|
301
|
+
|
|
302
|
+
### TypeScript Support
|
|
303
|
+
|
|
304
|
+
The package includes full TypeScript definitions. Import types for programmatic usage:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
import {
|
|
308
|
+
CDPClient,
|
|
309
|
+
CommandResult,
|
|
310
|
+
CLIConfig,
|
|
311
|
+
BrowserTarget
|
|
312
|
+
} from 'chrome-cdp-cli';
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### Programmatic Usage
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
import { CLIApplication } from 'chrome-cdp-cli';
|
|
319
|
+
|
|
320
|
+
const app = new CLIApplication();
|
|
321
|
+
const result = await app.run(['eval', 'document.title']);
|
|
322
|
+
console.log(result);
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Troubleshooting
|
|
326
|
+
|
|
327
|
+
### Common Issues
|
|
328
|
+
|
|
329
|
+
1. **Connection Refused**
|
|
330
|
+
- Ensure Chrome is running with `--remote-debugging-port=9222`
|
|
331
|
+
- Check if the port is correct and not blocked by firewall
|
|
332
|
+
|
|
333
|
+
2. **Command Timeout**
|
|
334
|
+
- Increase timeout with `--timeout` option
|
|
335
|
+
- Check if the page is responsive
|
|
336
|
+
|
|
337
|
+
3. **Element Not Found**
|
|
338
|
+
- Verify CSS selectors are correct
|
|
339
|
+
- Use `wait_for` command to wait for dynamic elements
|
|
340
|
+
|
|
341
|
+
4. **Permission Denied**
|
|
342
|
+
- Ensure Chrome has necessary permissions
|
|
343
|
+
- Check file system permissions for screenshot output
|
|
344
|
+
|
|
345
|
+
### Debug Mode
|
|
346
|
+
|
|
347
|
+
Enable verbose logging for troubleshooting:
|
|
348
|
+
|
|
349
|
+
```bash
|
|
350
|
+
chrome-cli --verbose eval "console.log('debug')"
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Packaging
|
|
354
|
+
|
|
355
|
+
```bash
|
|
356
|
+
# Create npm package
|
|
357
|
+
npm run package
|
|
358
|
+
|
|
359
|
+
# Prepare for publishing
|
|
360
|
+
npm run prepublishOnly
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
## Publishing to npm
|
|
364
|
+
|
|
365
|
+
To make this tool available via `npx`, you need to publish it to npm:
|
|
366
|
+
|
|
367
|
+
```bash
|
|
368
|
+
# 1. Login to npm (one time setup)
|
|
369
|
+
npm login
|
|
370
|
+
|
|
371
|
+
# 2. Publish the package
|
|
372
|
+
npm publish
|
|
373
|
+
|
|
374
|
+
# 3. Users can then use it with npx
|
|
375
|
+
npx chrome-cdp-cli eval "document.title"
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
**Note**: The package name `chrome-cdp-cli` uses a clean, descriptive approach. This approach:
|
|
379
|
+
|
|
380
|
+
- ✅ **Professional naming** that clearly indicates Chrome DevTools Protocol CLI
|
|
381
|
+
- ✅ **Works with npx**: `npx chrome-cdp-cli eval "document.title"`
|
|
382
|
+
- ✅ **Simple installation**: `npm install -g chrome-cdp-cli`
|
|
383
|
+
- ✅ **Short and memorable** compared to longer alternatives
|
|
384
|
+
|
|
385
|
+
Alternative naming examples:
|
|
386
|
+
1. **Scoped name**: `@nickxiao42/chrome-devtools-cli`
|
|
387
|
+
2. **Longer descriptive**: `chrome-automation-cli`
|
|
388
|
+
|
|
389
|
+
## Contributing
|
|
390
|
+
|
|
391
|
+
1. Fork the repository
|
|
392
|
+
2. Create a feature branch: `git checkout -b feature-name`
|
|
393
|
+
3. Make your changes and add tests
|
|
394
|
+
4. Run the verification suite: `npm run verify`
|
|
395
|
+
5. Commit your changes: `git commit -am 'Add feature'`
|
|
396
|
+
6. Push to the branch: `git push origin feature-name`
|
|
397
|
+
7. Submit a pull request
|
|
398
|
+
|
|
399
|
+
## License
|
|
400
|
+
|
|
401
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
402
|
+
|
|
403
|
+
## Support
|
|
404
|
+
|
|
405
|
+
- 📖 [Documentation](https://github.com/nickxiao42/chrome-devtools-cli/wiki)
|
|
406
|
+
- 🐛 [Issue Tracker](https://github.com/nickxiao42/chrome-devtools-cli/issues)
|
|
407
|
+
- 💬 [Discussions](https://github.com/nickxiao42/chrome-devtools-cli/discussions)
|
|
408
|
+
|
|
409
|
+
## Changelog
|
|
410
|
+
|
|
411
|
+
See [CHANGELOG.md](CHANGELOG.md) for version history and updates.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CLIApplication = void 0;
|
|
4
|
+
const CLIInterface_1 = require("./CLIInterface");
|
|
5
|
+
const ConnectionManager_1 = require("../connection/ConnectionManager");
|
|
6
|
+
const EvaluateScriptHandler_1 = require("../handlers/EvaluateScriptHandler");
|
|
7
|
+
const logger_1 = require("../utils/logger");
|
|
8
|
+
const CommandRouter_1 = require("./CommandRouter");
|
|
9
|
+
class CLIApplication {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.cli = new CLIInterface_1.CLIInterface();
|
|
12
|
+
this.connectionManager = new ConnectionManager_1.ConnectionManager();
|
|
13
|
+
this.logger = new logger_1.Logger();
|
|
14
|
+
this.setupHandlers();
|
|
15
|
+
}
|
|
16
|
+
setupHandlers() {
|
|
17
|
+
this.cli.registerHandler(new EvaluateScriptHandler_1.EvaluateScriptHandler());
|
|
18
|
+
}
|
|
19
|
+
async run(argv) {
|
|
20
|
+
try {
|
|
21
|
+
const command = this.cli.parseArgs(argv);
|
|
22
|
+
if (this.needsConnection(command.name)) {
|
|
23
|
+
await this.ensureConnection(command);
|
|
24
|
+
}
|
|
25
|
+
const result = await this.cli.execute(command);
|
|
26
|
+
this.outputResult(result, command);
|
|
27
|
+
return result.exitCode || (result.success ? CommandRouter_1.ExitCode.SUCCESS : CommandRouter_1.ExitCode.GENERAL_ERROR);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
31
|
+
console.error(`Error: ${errorMessage}`);
|
|
32
|
+
return CommandRouter_1.ExitCode.GENERAL_ERROR;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
needsConnection(commandName) {
|
|
36
|
+
const noConnectionCommands = ['help', 'connect', 'disconnect'];
|
|
37
|
+
return !noConnectionCommands.includes(commandName);
|
|
38
|
+
}
|
|
39
|
+
async ensureConnection(command) {
|
|
40
|
+
if (this.client) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
const targets = await this.connectionManager.discoverTargets(command.config.host, command.config.port);
|
|
45
|
+
if (targets.length === 0) {
|
|
46
|
+
throw new Error(`No Chrome targets found at ${command.config.host}:${command.config.port}. ` +
|
|
47
|
+
'Make sure Chrome is running with --remote-debugging-port=9222');
|
|
48
|
+
}
|
|
49
|
+
const pageTarget = targets.find(target => target.type === 'page');
|
|
50
|
+
if (!pageTarget) {
|
|
51
|
+
throw new Error('No page targets available. Open a tab in Chrome.');
|
|
52
|
+
}
|
|
53
|
+
this.client = await this.connectionManager.connectToTarget(pageTarget);
|
|
54
|
+
this.cli.setClient(this.client);
|
|
55
|
+
if (command.config.verbose) {
|
|
56
|
+
this.logger.info(`Connected to Chrome target: ${pageTarget.title} (${pageTarget.url})`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
throw new Error(`Failed to connect to Chrome: ${error instanceof Error ? error.message : String(error)}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
outputResult(result, command) {
|
|
64
|
+
if (command.config.quiet && result.success) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const output = this.cli.formatOutput(result, command.config.outputFormat);
|
|
68
|
+
if (result.success) {
|
|
69
|
+
console.log(output);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
console.error(output);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async shutdown() {
|
|
76
|
+
if (this.client) {
|
|
77
|
+
try {
|
|
78
|
+
await this.client.disconnect();
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
this.logger.error('Error during shutdown:', error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
getCLI() {
|
|
86
|
+
return this.cli;
|
|
87
|
+
}
|
|
88
|
+
getConnectionManager() {
|
|
89
|
+
return this.connectionManager;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.CLIApplication = CLIApplication;
|