1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use clap::{Parser, Subcommand};
const CLI_LONG_ABOUT: &str = "\
chrome-agent — browser automation for AI agents. Controls Chrome via CDP.\n\
Single binary, zero runtime dependencies. Named pages persist between invocations.\n\
Use --stealth to bypass bot detection (Cloudflare, Turnstile).\n\
Use --copy-cookies to access sites where you're already logged in (X.com, Gmail).\n\
\n\
Workflow: inspect → read uids → act (click/fill) → inspect again.\n\
Use --inspect on action commands to combine action + observation in one call.";
const CLI_AFTER_LONG_HELP: &str = include_str!("../llm-guide.txt");
#[derive(Parser)]
#[command(
name = "chrome-agent",
version,
about = "chrome-agent — browser automation for AI agents",
long_about = CLI_LONG_ABOUT,
after_long_help = CLI_AFTER_LONG_HELP,
)]
#[allow(clippy::struct_excessive_bools)]
pub struct Cli {
/// Named browser profile (default: "default")
#[arg(long, default_value = "default")]
pub browser: String,
/// Connect to existing browser: ws:// URL, http:// URL, or "auto"
#[arg(long)]
pub connect: Option<String>,
/// Launch browser with a visible window (default is headless)
#[arg(long)]
pub headed: bool,
/// Global timeout in seconds for page loads
#[arg(long, default_value = "30")]
pub timeout: u64,
/// Ignore HTTPS certificate errors
#[arg(long)]
pub ignore_https_errors: bool,
/// Output structured JSON instead of text
#[arg(long)]
pub json: bool,
/// Stealth mode: 7 anti-detection patches (webdriver, UA, WebGL, input leak, Runtime.enable skipped)
#[arg(long)]
pub stealth: bool,
/// Max depth for --inspect output (used by goto, click, fill, etc.)
#[arg(long)]
pub max_depth: Option<usize>,
/// Copy cookies from your real Chrome profile (uses your logged-in sessions)
#[arg(long)]
pub copy_cookies: bool,
/// Named page/tab within the browser (default: "default")
#[arg(long, default_value = "default")]
pub page: String,
/// How to answer JS dialogs (alert/confirm/prompt/beforeunload): accept, dismiss, or manual
#[arg(long, default_value = "accept")]
pub dialog: String,
/// Text to submit for `prompt()` dialogs when --dialog accept (default: empty)
#[arg(long)]
pub dialog_text: Option<String>,
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand)]
pub enum Command {
/// Navigate to a URL
#[command(alias = "navigate", alias = "open", alias = "go")]
Goto {
/// Target URL
url: String,
/// Inspect page after navigation
#[arg(long)]
inspect: bool,
/// Max depth for inspect output (also accepted as global flag)
#[arg(long)]
max_depth: Option<usize>,
/// Wait for a CSS selector to appear after navigation
#[arg(long)]
wait_for: Option<String>,
/// Extra HTTP header "Name: Value" (repeatable) sent with the navigation
#[arg(long = "header")]
headers: Vec<String>,
},
/// Click an element by uid, CSS selector, or coordinates
#[command(alias = "tap")]
Click {
/// Element uid (e.g. "n47") — omit if using --selector or --xy
uid: Option<String>,
/// CSS selector to click
#[arg(long)]
selector: Option<String>,
/// Click at x,y coordinates (e.g. --xy 100,200)
#[arg(long, value_delimiter = ',')]
xy: Option<Vec<f64>>,
/// Inspect page after clicking
#[arg(long)]
inspect: bool,
/// Max depth for inspect output (also accepted as global flag)
#[arg(long)]
max_depth: Option<usize>,
},
/// Fill an input element by uid or CSS selector
Fill {
/// Value to fill
value: String,
/// Element uid (e.g. "n47") — omit if using --selector
#[arg(long)]
uid: Option<String>,
/// CSS selector to fill
#[arg(long)]
selector: Option<String>,
/// Inspect page after filling
#[arg(long)]
inspect: bool,
/// Max depth for inspect output (also accepted as global flag)
#[arg(long)]
max_depth: Option<usize>,
},
/// Fill multiple form fields at once
#[command(name = "fill-form")]
FillForm {
/// uid=value pairs (e.g. "e5=hello" "e7=world")
pairs: Vec<String>,
/// Inspect page after filling
#[arg(long)]
inspect: bool,
/// Max depth for inspect output (also accepted as global flag)
#[arg(long)]
max_depth: Option<usize>,
},
/// Extract visible text from the page or an element
Text {
/// Element uid to extract text from (default: entire page)
uid: Option<String>,
/// CSS selector to extract text from (e.g. "article", ".content")
#[arg(long)]
selector: Option<String>,
/// Truncate output to N characters (appends "..." if truncated)
#[arg(long)]
truncate: Option<usize>,
},
/// Extract main content using Readability (Mozilla's reader mode)
Read {
/// Return cleaned HTML instead of plain text
#[arg(long)]
html: bool,
/// Truncate output to N characters
#[arg(long)]
truncate: Option<usize>,
},
/// Navigate back in browser history
Back,
/// Navigate forward in browser history
Forward {
/// Inspect page after navigation
#[arg(long)]
inspect: bool,
/// Max depth for inspect output
#[arg(long)]
max_depth: Option<usize>,
},
/// Double-click an element by uid, CSS selector, or coordinates
Dblclick {
/// Element uid
uid: Option<String>,
/// CSS selector
#[arg(long)]
selector: Option<String>,
/// Click at x,y coordinates
#[arg(long, value_delimiter = ',')]
xy: Option<Vec<f64>>,
/// Inspect page after double-clicking
#[arg(long)]
inspect: bool,
/// Max depth for inspect output
#[arg(long)]
max_depth: Option<usize>,
},
/// Select a dropdown option by value or visible text
Select {
/// Value or visible text to select
value: String,
/// Element uid of the <select>
#[arg(long)]
uid: Option<String>,
/// CSS selector of the <select>
#[arg(long)]
selector: Option<String>,
/// Inspect page after selecting
#[arg(long)]
inspect: bool,
/// Max depth for inspect output
#[arg(long)]
max_depth: Option<usize>,
},
/// Ensure a checkbox/radio is checked (idempotent)
Check {
/// Element uid
uid: Option<String>,
/// CSS selector
#[arg(long)]
selector: Option<String>,
/// Inspect page after checking
#[arg(long)]
inspect: bool,
/// Max depth for inspect output
#[arg(long)]
max_depth: Option<usize>,
},
/// Ensure a checkbox/radio is unchecked (idempotent)
Uncheck {
/// Element uid
uid: Option<String>,
/// CSS selector
#[arg(long)]
selector: Option<String>,
/// Inspect page after unchecking
#[arg(long)]
inspect: bool,
/// Max depth for inspect output
#[arg(long)]
max_depth: Option<usize>,
},
/// Upload file(s) to a file input element
Upload {
/// File path(s) to upload
files: Vec<String>,
/// Element uid of the file input
#[arg(long)]
uid: Option<String>,
/// CSS selector of the file input
#[arg(long)]
selector: Option<String>,
/// Inspect page after uploading
#[arg(long)]
inspect: bool,
/// Max depth for inspect output
#[arg(long)]
max_depth: Option<usize>,
},
/// Drag an element to another element
Drag {
/// Source element uid
from: String,
/// Destination element uid
to: String,
/// Inspect page after dragging
#[arg(long)]
inspect: bool,
/// Max depth for inspect output
#[arg(long)]
max_depth: Option<usize>,
},
/// Take an accessibility tree inspection
#[command(alias = "snap", alias = "snapshot", alias = "tree")]
Inspect {
/// Include ignored/generic nodes
#[arg(long)]
verbose: bool,
/// Maximum tree depth (0 = root only)
#[arg(long)]
max_depth: Option<usize>,
/// Only inspect children of this uid
#[arg(long)]
uid: Option<String>,
/// Only show nodes matching these roles (comma-separated, e.g. "button,link,textbox")
#[arg(long)]
filter: Option<String>,
/// Scroll to load lazy content before inspecting
#[arg(long)]
scroll: bool,
/// Collect N items by scrolling (for virtualized lists like X.com)
#[arg(long)]
limit: Option<usize>,
/// Include href URLs on link nodes
#[arg(long)]
urls: bool,
/// Cap output to N characters (appends a truncation note; keeps context small)
#[arg(long)]
max_chars: Option<usize>,
/// Skip the first K characters of output (paging; use with --max-chars)
#[arg(long, default_value = "0")]
offset: usize,
},
/// Show what changed since the last inspect
Diff,
/// Capture a screenshot
#[command(alias = "capture")]
Screenshot {
/// Output filename (default: timestamped)
#[arg(long)]
filename: Option<String>,
/// Image format: png (default) or jpeg (smaller, use with --quality)
#[arg(long, default_value = "png")]
format: String,
/// JPEG quality 0-100 (ignored for png)
#[arg(long)]
quality: Option<u32>,
/// Downscale so the captured width fits within N CSS pixels (keeps files/tokens small)
#[arg(long)]
max_width: Option<u32>,
/// Capture only the element with this uid
#[arg(long)]
uid: Option<String>,
/// Capture only the element matching this CSS selector
#[arg(long)]
selector: Option<String>,
},
/// Download a URL to disk, fetched in-page so cookies/auth are preserved
///
/// Click-triggered browser-native downloads are not supported; resolve the
/// target href (e.g. `inspect --urls`) and pass it here.
Download {
/// URL to download (fetched with the page's session)
url: String,
/// Output path or filename (default: derived from Content-Disposition/URL into ~/.chrome-agent/tmp)
#[arg(long)]
out: Option<String>,
/// Timeout in seconds
#[arg(long, default_value = "30")]
timeout: u64,
},
/// Print the current page to a PDF file
Pdf {
/// Output filename (default: timestamped)
#[arg(long)]
filename: Option<String>,
/// Landscape orientation
#[arg(long)]
landscape: bool,
/// Include background graphics/colors
#[arg(long)]
background: bool,
},
/// Auto-extract structured data from repeating page elements (lists, tables, cards)
Extract {
/// CSS selector to scope extraction (e.g. "main", ".results")
#[arg(long)]
selector: Option<String>,
/// Max items to extract
#[arg(long, default_value = "10")]
limit: usize,
/// Scroll to load lazy content before extracting (useful for infinite-scroll pages)
#[arg(long)]
scroll: bool,
/// Use accessibility tree instead of DOM (works on React SPAs like X.com)
#[arg(long)]
a11y: bool,
},
/// Evaluate JavaScript in the page
#[command(alias = "js", alias = "execute")]
Eval {
/// JS expression to evaluate (if --selector, `el` is the matched element)
expression: String,
/// CSS selector — the matched element is available as `el` in the expression
#[arg(long)]
selector: Option<String>,
},
/// Wait for a condition (text, url, selector, or network-idle)
Wait {
/// What to wait for: "text", "url", "selector", or "network-idle"
what: String,
/// Pattern to match (ignored for network-idle)
#[arg(default_value = "")]
pattern: String,
/// Timeout in seconds
#[arg(long, default_value = "10")]
timeout: u64,
/// For network-idle: required quiet window in milliseconds
#[arg(long, default_value = "500")]
idle_ms: u64,
},
/// Type text into the focused element (or focus a selector first)
Type {
/// Text to type
text: String,
/// CSS selector to focus before typing
#[arg(long)]
selector: Option<String>,
},
/// Press a key (Enter, Tab, Escape, etc.)
Press {
/// Key name
key: String,
},
/// Scroll the page or an element into view
Scroll {
/// "up", "down", or a uid to scroll into view
target: String,
/// Pixels to scroll when using "up" or "down" (default: 500)
#[arg(long, default_value = "500")]
px: u64,
},
/// Hover over an element by uid
Hover {
/// Element uid (e.g. "n47")
uid: String,
},
/// Capture network requests (API responses, XHR, fetch)
Network {
/// URL pattern to filter (case-insensitive contains match)
#[arg(long)]
filter: Option<String>,
/// Include response bodies (JSON/text only, truncated to 2000 chars)
#[arg(long)]
body: bool,
/// Capture live traffic for N seconds (default: show already-loaded resources via Performance API)
#[arg(long)]
live: Option<u64>,
/// Max entries to show
#[arg(long, default_value = "50")]
limit: usize,
/// Block requests matching this URL pattern
#[arg(long)]
abort: Option<String>,
},
/// Show captured console messages and JS errors
Console {
/// Filter by level: log, warn, error, info, exception
#[arg(long)]
level: Option<String>,
/// Clear captured messages after reading
#[arg(long)]
clear: bool,
/// Max entries to show
#[arg(long, default_value = "50")]
limit: usize,
},
/// Replay a recorded session file
Replay {
/// Path to the recording file
file: String,
/// Variable substitutions (key=value, comma-separated)
#[arg(long, value_delimiter = ',')]
vars: Option<Vec<String>>,
},
/// Show browsing history
History {
/// Filter entries by URL pattern (case-insensitive)
#[arg(long)]
filter: Option<String>,
/// Max entries to show
#[arg(long, default_value = "20")]
limit: usize,
},
/// Switch execution context to an iframe (or back to main)
Frame {
/// CSS selector of the iframe, or "main" to return to top-level
target: String,
},
/// Execute multiple commands from a JSON array on stdin
Batch,
/// Persistent connection mode — read JSON commands from stdin (one per line)
Pipe,
/// List open browser tabs
Tabs,
/// Close the managed browser
Close {
/// Also delete the browser profile (cookies, cache, data)
#[arg(long)]
purge: bool,
},
/// Show session status
Status,
/// Stop the background daemon
Stop,
/// Daemon management
Daemon {
#[command(subcommand)]
action: DaemonAction,
},
}
#[derive(Subcommand)]
pub enum DaemonAction {
/// Start the daemon (foreground, used internally)
Start,
}