chrome-cli 1.2.0

A CLI tool for browser automation via the Chrome DevTools Protocol
Documentation
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use std::fmt::Write;
use std::time::Duration;

use serde::Serialize;

use chrome_cli::cdp::{CdpClient, CdpConfig};
use chrome_cli::chrome::{TargetInfo, activate_target, query_targets};
use chrome_cli::connection::{resolve_connection, select_target};
use chrome_cli::error::{AppError, ExitCode};
use chrome_cli::session;

use crate::cli::{GlobalOpts, TabsArgs, TabsCommand};

/// Execute the `tabs` subcommand group.
///
/// # Errors
///
/// Returns `AppError` if the subcommand fails.
pub async fn execute_tabs(global: &GlobalOpts, args: &TabsArgs) -> Result<(), AppError> {
    match &args.command {
        TabsCommand::List(list_args) => execute_list(global, list_args.all).await,
        TabsCommand::Create(create_args) => {
            execute_create(global, create_args.url.as_deref(), create_args.background).await
        }
        TabsCommand::Close(close_args) => execute_close(global, &close_args.targets).await,
        TabsCommand::Activate(act_args) => {
            execute_activate(global, &act_args.target, act_args.quiet).await
        }
    }
}

// =============================================================================
// Output types
// =============================================================================

#[derive(Serialize)]
struct TabInfo {
    id: String,
    url: String,
    title: String,
    active: bool,
}

#[derive(Serialize)]
struct CreateResult {
    id: String,
    url: String,
    title: String,
}

#[derive(Serialize)]
struct CloseResult {
    closed: Vec<String>,
    remaining: usize,
}

#[derive(Serialize)]
struct ActivateResult {
    activated: String,
    url: String,
    title: String,
}

// =============================================================================
// Output formatting
// =============================================================================

fn print_output(value: &impl Serialize, output: &crate::cli::OutputFormat) -> Result<(), AppError> {
    let json = if output.pretty {
        serde_json::to_string_pretty(value)
    } else {
        serde_json::to_string(value)
    };
    let json = json.map_err(|e| AppError {
        message: format!("serialization error: {e}"),
        code: ExitCode::GeneralError,
        custom_json: None,
    })?;
    println!("{json}");
    Ok(())
}

fn format_plain_table(tabs: &[TabInfo]) -> String {
    let mut out = String::new();
    let _ = writeln!(
        out,
        "  {:<3} {:<14} {:<20} {:<26} ACTIVE",
        "#", "ID", "TITLE", "URL"
    );
    for (i, tab) in tabs.iter().enumerate() {
        let active_marker = if tab.active { "*" } else { "" };
        let title: String = tab.title.chars().take(20).collect();
        let url: String = tab.url.chars().take(26).collect();
        let _ = writeln!(
            out,
            "  {i:<3} {:<14} {:<20} {:<26} {}",
            tab.id, title, url, active_marker
        );
    }
    out
}

// =============================================================================
// Filtering
// =============================================================================

fn filter_page_targets(targets: &[TargetInfo], include_all: bool) -> Vec<&TargetInfo> {
    targets
        .iter()
        .filter(|t| t.target_type == "page")
        .filter(|t| {
            if include_all {
                return true;
            }
            let url = &t.url;
            // Always include chrome://newtab/
            if url.starts_with("chrome://newtab") {
                return true;
            }
            // Exclude other chrome:// and chrome-extension:// URLs
            if url.starts_with("chrome://") || url.starts_with("chrome-extension://") {
                return false;
            }
            true
        })
        .collect()
}

// =============================================================================
// Subcommand handlers
// =============================================================================

async fn execute_list(global: &GlobalOpts, include_all: bool) -> Result<(), AppError> {
    let conn = resolve_connection(&global.host, global.port, global.ws_url.as_deref()).await?;

    let targets = query_targets(&conn.host, conn.port).await?;
    let filtered = filter_page_targets(&targets, include_all);

    // Determine the active tab by querying Chrome's actual visibility state
    // via CDP sessions, rather than relying on /json/list positional ordering
    // (which does not reliably reflect activation state in headless mode).
    let config = cdp_config(global);
    let visible_id = query_visible_target_id(&conn.ws_url, &filtered, config).await;

    let tabs: Vec<TabInfo> = filtered
        .iter()
        .enumerate()
        .map(|(i, t)| TabInfo {
            id: t.id.clone(),
            url: t.url.clone(),
            title: t.title.clone(),
            active: match &visible_id {
                Some(vid) => t.id == *vid,
                None => i == 0, // fallback if CDP query fails
            },
        })
        .collect();

    if global.output.plain {
        print!("{}", format_plain_table(&tabs));
        return Ok(());
    }

    print_output(&tabs, &global.output)
}

async fn execute_create(
    global: &GlobalOpts,
    url: Option<&str>,
    background: bool,
) -> Result<(), AppError> {
    let conn = resolve_connection(&global.host, global.port, global.ws_url.as_deref()).await?;

    let config = cdp_config(global);
    let client = CdpClient::connect(&conn.ws_url, config.clone()).await?;

    // When --background is used, record the currently active (visible) tab
    // so we can re-activate it after creation. Uses CDP visibilityState
    // rather than /json/list ordering (which is unreliable in headless mode).
    let original_active_id = if background {
        let targets = query_targets(&conn.host, conn.port).await?;
        let pages: Vec<&TargetInfo> = targets.iter().filter(|t| t.target_type == "page").collect();
        let mut visible_id = None;
        for page in &pages {
            if check_target_visible(&client, &page.id).await {
                visible_id = Some(page.id.clone());
                break;
            }
        }
        // Fall back to first page target if visibility check fails
        visible_id.or_else(|| pages.first().map(|t| t.id.clone()))
    } else {
        None
    };

    let target_url = url.unwrap_or("about:blank");
    let mut params = serde_json::json!({ "url": target_url });
    if background {
        params["background"] = serde_json::json!(true);
    }

    let result = client
        .send_command("Target.createTarget", Some(params))
        .await?;

    let target_id = result["targetId"].as_str().unwrap_or_default().to_string();

    // Re-activate the original tab if --background was requested.
    // Uses HTTP /json/activate to tell Chrome which tab should be visible,
    // then verifies via CDP document.visibilityState (which is authoritative,
    // unlike /json/list ordering which is unreliable in headless mode).
    if let Some(ref active_id) = original_active_id {
        activate_target(&conn.host, conn.port, active_id).await?;

        // Stability verification: page-load events can re-activate the new
        // tab after our initial activation succeeds. Wait briefly, then
        // verify via CDP that the original tab is actually visible.
        tokio::time::sleep(Duration::from_millis(100)).await;
        if !check_target_visible(&client, active_id).await {
            // Page-load re-activated the new tab. Re-activate and re-verify.
            activate_target(&conn.host, conn.port, active_id).await?;
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }

    // Re-query to get the new tab's resolved URL and title
    let targets = query_targets(&conn.host, conn.port).await?;
    let new_tab = targets.iter().find(|t| t.id == target_id);

    let output = CreateResult {
        id: target_id,
        url: new_tab.map_or(target_url.to_string(), |t| t.url.clone()),
        title: new_tab.map_or(String::new(), |t| t.title.clone()),
    };

    print_output(&output, &global.output)
}

async fn execute_close(global: &GlobalOpts, target_args: &[String]) -> Result<(), AppError> {
    let conn = resolve_connection(&global.host, global.port, global.ws_url.as_deref()).await?;

    let targets = query_targets(&conn.host, conn.port).await?;

    // Resolve all target arguments BEFORE closing any (avoids index shift)
    let mut to_close: Vec<&TargetInfo> = Vec::new();
    for arg in target_args {
        let target = select_target(&targets, Some(arg))?;
        to_close.push(target);
    }

    // Last-tab protection: count page targets and how many we're closing
    let page_count = targets.iter().filter(|t| t.target_type == "page").count();
    let closing_page_count = to_close.iter().filter(|t| t.target_type == "page").count();
    if closing_page_count >= page_count {
        return Err(AppError::last_tab());
    }

    let config = cdp_config(global);
    let client = CdpClient::connect(&conn.ws_url, config).await?;

    let mut closed_ids = Vec::new();
    for target in &to_close {
        let params = serde_json::json!({ "targetId": target.id });
        client
            .send_command("Target.closeTarget", Some(params))
            .await?;
        closed_ids.push(target.id.clone());
    }

    // Poll until Chrome's HTTP endpoint reflects the tab closures.
    // The /json/list endpoint updates asynchronously after CDP commands,
    // so we retry (matching the pattern in execute_create).
    let expected_remaining = page_count - closing_page_count;
    let mut remaining = expected_remaining;
    for _ in 0..10 {
        let remaining_targets = query_targets(&conn.host, conn.port).await?;
        remaining = remaining_targets
            .iter()
            .filter(|t| t.target_type == "page")
            .count();
        if remaining == expected_remaining {
            break;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    let output = CloseResult {
        closed: closed_ids,
        remaining,
    };

    print_output(&output, &global.output)
}

async fn execute_activate(
    global: &GlobalOpts,
    target_arg: &str,
    quiet: bool,
) -> Result<(), AppError> {
    let conn = resolve_connection(&global.host, global.port, global.ws_url.as_deref()).await?;

    let targets = query_targets(&conn.host, conn.port).await?;
    let target = select_target(&targets, Some(target_arg))?;

    let config = cdp_config(global);
    let client = CdpClient::connect(&conn.ws_url, config).await?;

    let params = serde_json::json!({ "targetId": target.id });
    client
        .send_command("Target.activateTarget", Some(params))
        .await?;

    // Poll until Chrome's HTTP endpoint reflects the activation.
    for _ in 0..50 {
        let check = query_targets(&conn.host, conn.port).await?;
        let first_page = check.iter().find(|t| t.target_type == "page");
        if first_page.is_some_and(|t| t.id == target.id) {
            break;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    // Persist active tab ID in session for cross-invocation use
    if let Ok(Some(mut session_data)) = session::read_session() {
        session_data.active_tab_id = Some(target.id.clone());
        session_data.timestamp = session::now_iso8601();
        if let Err(e) = session::write_session(&session_data) {
            eprintln!("warning: could not persist active tab: {e}");
        }
    }

    if quiet {
        return Ok(());
    }

    let output = ActivateResult {
        activated: target.id.clone(),
        url: target.url.clone(),
        title: target.title.clone(),
    };

    print_output(&output, &global.output)
}

// =============================================================================
// Helpers
// =============================================================================

fn cdp_config(global: &GlobalOpts) -> CdpConfig {
    let mut config = CdpConfig::default();
    if let Some(timeout_ms) = global.timeout {
        config.command_timeout = Duration::from_millis(timeout_ms);
    }
    config
}

/// Check whether a target's `document.visibilityState` is `"visible"` via a
/// CDP session on the given client.
async fn check_target_visible(client: &CdpClient, target_id: &str) -> bool {
    let Ok(session) = client.create_session(target_id).await else {
        return false;
    };
    let params = serde_json::json!({
        "expression": "document.visibilityState",
        "returnByValue": true,
    });
    let Ok(result) = session.send_command("Runtime.evaluate", Some(params)).await else {
        return false;
    };
    result["result"]["value"].as_str() == Some("visible")
}

/// Determine the active (visible) target by querying `document.visibilityState`
/// for each page target. Returns the target ID of the first target that reports
/// itself as `"visible"`, or `None` if the query fails for all targets.
///
/// Uses an optimistic strategy: checks the first target first, since it is the
/// most common case. Falls back to scanning the rest if needed.
async fn query_visible_target_id(
    ws_url: &str,
    page_targets: &[&TargetInfo],
    config: CdpConfig,
) -> Option<String> {
    if page_targets.is_empty() {
        return None;
    }

    let client = CdpClient::connect(ws_url, config).await.ok()?;

    for target in page_targets {
        if check_target_visible(&client, &target.id).await {
            return Some(target.id.clone());
        }
    }

    None
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn make_target(id: &str, target_type: &str, url: &str) -> TargetInfo {
        TargetInfo {
            id: id.to_string(),
            target_type: target_type.to_string(),
            title: format!("Title {id}"),
            url: url.to_string(),
            ws_debugger_url: Some(format!("ws://127.0.0.1:9222/devtools/page/{id}")),
        }
    }

    #[test]
    fn filter_excludes_chrome_urls() {
        let targets = vec![
            make_target("a", "page", "https://google.com"),
            make_target("b", "page", "chrome://settings/"),
            make_target("c", "page", "chrome://extensions/"),
        ];
        let filtered = filter_page_targets(&targets, false);
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].id, "a");
    }

    #[test]
    fn filter_keeps_chrome_newtab() {
        let targets = vec![
            make_target("a", "page", "chrome://newtab/"),
            make_target("b", "page", "chrome://settings/"),
        ];
        let filtered = filter_page_targets(&targets, false);
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].id, "a");
    }

    #[test]
    fn filter_excludes_chrome_extension_urls() {
        let targets = vec![
            make_target("a", "page", "https://example.com"),
            make_target("b", "page", "chrome-extension://abc123/popup.html"),
        ];
        let filtered = filter_page_targets(&targets, false);
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].id, "a");
    }

    #[test]
    fn filter_all_returns_all_page_targets() {
        let targets = vec![
            make_target("a", "page", "https://google.com"),
            make_target("b", "page", "chrome://settings/"),
            make_target("c", "page", "chrome-extension://abc/popup.html"),
        ];
        let filtered = filter_page_targets(&targets, true);
        assert_eq!(filtered.len(), 3);
    }

    #[test]
    fn filter_excludes_non_page_types() {
        let targets = vec![
            make_target("a", "page", "https://google.com"),
            make_target("b", "service_worker", "https://sw.example.com"),
            make_target("c", "background_page", "chrome-extension://abc/bg.html"),
        ];
        let filtered = filter_page_targets(&targets, false);
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].id, "a");
    }

    #[test]
    fn visible_target_is_marked_active() {
        let targets = vec![
            make_target("a", "page", "https://google.com"),
            make_target("b", "page", "https://github.com"),
        ];
        let filtered = filter_page_targets(&targets, false);
        // When CDP reports target "b" as visible, it should be active
        let visible_id = Some("b".to_string());
        let tabs: Vec<TabInfo> = filtered
            .iter()
            .enumerate()
            .map(|(i, t)| TabInfo {
                id: t.id.clone(),
                url: t.url.clone(),
                title: t.title.clone(),
                active: match &visible_id {
                    Some(vid) => t.id == *vid,
                    None => i == 0,
                },
            })
            .collect();
        assert!(!tabs[0].active);
        assert!(tabs[1].active);
    }

    #[test]
    fn fallback_to_first_when_no_visible_id() {
        let targets = vec![
            make_target("a", "page", "https://google.com"),
            make_target("b", "page", "https://github.com"),
        ];
        let filtered = filter_page_targets(&targets, false);
        // When CDP visibility query fails (None), fall back to first target
        let visible_id: Option<String> = None;
        let tabs: Vec<TabInfo> = filtered
            .iter()
            .enumerate()
            .map(|(i, t)| TabInfo {
                id: t.id.clone(),
                url: t.url.clone(),
                title: t.title.clone(),
                active: match &visible_id {
                    Some(vid) => t.id == *vid,
                    None => i == 0,
                },
            })
            .collect();
        assert!(tabs[0].active);
        assert!(!tabs[1].active);
    }

    #[test]
    fn plain_table_format() {
        let tabs = vec![
            TabInfo {
                id: "ABC123".to_string(),
                url: "https://google.com".to_string(),
                title: "Google".to_string(),
                active: true,
            },
            TabInfo {
                id: "DEF456".to_string(),
                url: "https://github.com".to_string(),
                title: "GitHub".to_string(),
                active: false,
            },
        ];
        let output = format_plain_table(&tabs);
        assert!(output.contains('#'));
        assert!(output.contains("ID"));
        assert!(output.contains("TITLE"));
        assert!(output.contains("URL"));
        assert!(output.contains("ACTIVE"));
        assert!(output.contains("ABC123"));
        assert!(output.contains("Google"));
        assert!(output.contains('*'));
        assert!(output.contains("DEF456"));
        assert!(output.contains("GitHub"));
    }

    #[test]
    fn last_tab_protection_logic() {
        // Simulate: 2 page targets, closing both should fail
        let page_count = 2;
        let closing_page_count = 2;
        assert!(closing_page_count >= page_count);

        // Simulate: 2 page targets, closing 1 should succeed
        let closing_page_count = 1;
        assert!(closing_page_count < page_count);

        // Simulate: 1 page target, closing 1 should fail
        let page_count = 1;
        let closing_page_count = 1;
        assert!(closing_page_count >= page_count);
    }
}