oxibrowser 0.9.1

Headless browser engine with CDP support
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! OxiBrowser CLI — headless browser with CDP support.
//!
//! Subcommands:
//! - `oxibrowser fetch <url>` — fetch a URL and dump HTML/markdown (enhanced)
//! - `oxibrowser eval <url> <expr>` — evaluate JS on a page
//! - `oxibrowser extract <url>` — extract structured data from a page
//! - `oxibrowser browse <url>` — interactive Tab API browsing (CDP-free)
//! - `oxibrowser serve` — start the CDP server
//! - `oxibrowser version` — print version

use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tracing::info;

/// OxiBrowser — a headless browser engine with CDP support.
#[derive(Parser)]
#[command(name = "oxibrowser")]
#[command(version, about = "Headless browser engine with CDP support")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Fetch a URL and dump its content.
    Fetch {
        /// URL to fetch.
        url: String,
        /// Output format: html, markdown, or text.
        #[arg(long, default_value = "html")]
        format: String,
        /// Print response headers to stderr.
        #[arg(long)]
        headers: bool,
        /// Print only the HTTP status code.
        #[arg(long)]
        status: bool,
        /// HTTP method (GET or POST).
        #[arg(long, default_value = "GET")]
        method: String,
        /// Output as JSON.
        #[arg(long)]
        json: bool,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 30)]
        timeout: u64,
    },

    /// Evaluate JavaScript on a page.
    Eval {
        /// URL to navigate to.
        url: String,
        /// JavaScript expression to evaluate.
        expression: String,
        /// Output as JSON.
        #[arg(long)]
        json: bool,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 30)]
        timeout: u64,
    },

    /// Extract structured data from a page.
    Extract {
        /// URL to extract from.
        url: String,
        /// Extract all <a href> links (one per line).
        #[arg(long)]
        links: bool,
        /// Extract the <title> text.
        #[arg(long)]
        title: bool,
        /// Extract full text content.
        #[arg(long)]
        text: bool,
        /// Convert page to Markdown.
        #[arg(long)]
        markdown: bool,
        /// CSS selector to match elements.
        #[arg(long)]
        selector: Option<String>,
        /// With --selector, print all matching elements (not just the first).
        #[arg(long)]
        all: bool,
        /// Output as JSON.
        #[arg(long)]
        json: bool,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 30)]
        timeout: u64,
    },

    /// Browse a page using the Tab API (CDP-free).
    Browse {
        /// URL to navigate to.
        url: String,
        /// Output format: markdown, html, text, json, links.
        #[arg(long, default_value = "markdown")]
        format: String,
        /// Click an element matching a CSS selector.
        #[arg(long)]
        click: Option<String>,
        /// Fill an element (format: "selector:text").
        #[arg(long)]
        input: Option<String>,
        /// Press a key combo (e.g., Enter, Ctrl+C, Shift+Tab).
        #[arg(long)]
        press: Option<String>,
        /// Wait for a CSS selector before continuing.
        #[arg(long)]
        wait: Option<String>,
        /// Wait timeout in ms.
        #[arg(long, default_value_t = 5000)]
        wait_timeout: u64,
        /// Extract text from elements matching a selector.
        #[arg(long)]
        extract: Option<String>,
        /// With --extract, print all matches.
        #[arg(long)]
        all: bool,
        /// Save PNG screenshot to a file.
        #[arg(long)]
        screenshot: Option<String>,
        /// Screenshot width in pixels.
        #[arg(long, default_value_t = 800)]
        width: u32,
        /// Evaluate JS and print result.
        #[arg(long)]
        eval: Option<String>,
        /// Print response metadata to stderr.
        #[arg(long)]
        headers: bool,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 30)]
        timeout: u64,
    },

    /// Start the CDP server.
    Serve {
        /// Host to bind to.
        #[arg(long, default_value = "127.0.0.1")]
        host: String,
        /// Port to listen on.
        #[arg(long, default_value_t = 9222)]
        port: u16,
        /// Cookie persistence file.
        #[arg(long)]
        cookie_file: Option<String>,
        /// Request timeout in seconds.
        #[arg(long, default_value_t = 30)]
        timeout: u64,
    },

    /// Run a YAML script on a Tab.
    Run {
        /// Path to the YAML script file or inline YAML.
        script: String,
        /// Timeout in seconds.
        #[arg(long, default_value_t = 60)]
        timeout: u64,
    },

    /// Print version information.
    Version,
}

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
        )
        .init();

    let cli = Cli::parse();

    // Set up Ctrl+C handler for graceful shutdown.
    let ctrlc = tokio::signal::ctrl_c();
    tokio::pin!(ctrlc);

    match cli.command {
        Commands::Fetch {
            url,
            format,
            headers,
            status,
            method,
            json,
            timeout,
        } => {
            let op = run_fetch(&url, &format, headers, status, &method, json, timeout);
            tokio::select! {
                result = op => result?,
                _ = &mut ctrlc => {
                    eprintln!("\nInterrupted.");
                    std::process::exit(130);
                }
            }
        }
        Commands::Eval {
            url,
            expression,
            json,
            timeout,
        } => {
            let op = run_eval(&url, &expression, json, timeout);
            tokio::select! {
                result = op => result?,
                _ = &mut ctrlc => {
                    eprintln!("\nInterrupted.");
                    std::process::exit(130);
                }
            }
        }
        Commands::Extract {
            url,
            links,
            title,
            text,
            markdown,
            selector,
            all,
            json,
            timeout,
        } => {
            let op = run_extract(
                &url,
                links,
                title,
                text,
                markdown,
                selector.as_deref(),
                all,
                json,
                timeout,
            );
            tokio::select! {
                result = op => result?,
                _ = &mut ctrlc => {
                    eprintln!("\nInterrupted.");
                    std::process::exit(130);
                }
            }
        }
        Commands::Browse {
            url,
            format,
            click,
            input,
            press,
            wait,
            wait_timeout,
            extract,
            all,
            screenshot,
            width,
            eval,
            headers,
            timeout,
        } => {
            let op = run_browse(
                &url,
                &format,
                click.as_deref(),
                input.as_deref(),
                press.as_deref(),
                wait.as_deref(),
                wait_timeout,
                extract.as_deref(),
                all,
                screenshot.as_deref(),
                width,
                eval.as_deref(),
                headers,
                timeout,
            );
            tokio::select! {
                result = op => result?,
                _ = &mut ctrlc => {
                    eprintln!("\nInterrupted.");
                    std::process::exit(130);
                }
            }
        }
        Commands::Serve {
            host,
            port,
            cookie_file,
            timeout: _timeout,
        } => {
            let op = run_serve(&host, port, cookie_file.as_deref());
            tokio::select! {
                result = op => result?,
                _ = &mut ctrlc => {
                    // Ctrl+C is handled inside run_serve via its own ctrl_c listener.
                    // This branch is a fallback.
                }
            }
        }
        Commands::Run { script, timeout } => {
            let op = run_script(&script, timeout);
            tokio::select! {
                result = op => result?,
                _ = &mut ctrlc => {
                    eprintln!("\nInterrupted.");
                    std::process::exit(130);
                }
            }
        }
        Commands::Version => {
            println!("oxibrowser {}", env!("CARGO_PKG_VERSION"));
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// fetch
// ---------------------------------------------------------------------------

/// Fetch a URL and print the content.
async fn run_fetch(
    url: &str,
    format: &str,
    show_headers: bool,
    status_only: bool,
    method: &str,
    json_output: bool,
    timeout: u64,
) -> Result<()> {
    let _ = method; // Method selection is a future enhancement (HTTP client currently GETs only).

    info!(url = %url, format = %format, timeout, "fetching URL");

    let config = oxibrowser_core::BrowserConfig::headless();
    let browser = oxibrowser_core::Browser::new(config).await?;

    let session_result =
        tokio::time::timeout(Duration::from_secs(timeout), browser.new_page(url)).await;

    let session = match session_result {
        Ok(Ok(s)) => s,
        Ok(Err(e)) => {
            eprintln!("error: {e}");
            std::process::exit(1);
        }
        Err(_) => {
            eprintln!("error: timed out after {timeout}s");
            std::process::exit(1);
        }
    };

    let session_guard = session.read().await;

    match session_guard.page() {
        Some(page) => {
            if status_only {
                if json_output {
                    println!("{}", serde_json::json!({"status": page.status()}));
                } else {
                    println!("{}", page.status());
                }
            } else {
                if show_headers {
                    if json_output {
                        eprintln!(
                            "{}",
                            serde_json::json!({
                                "status": page.status(),
                                "content_type": page.content_type(),
                            })
                        );
                    } else {
                        eprintln!("HTTP {}", page.status());
                        eprintln!("Content-Type: {}", page.content_type());
                    }
                }

                let body = match format {
                    "markdown" | "md" => page.to_markdown(),
                    "text" => page
                        .root_frame()
                        .document()
                        .query_text("body")
                        .unwrap_or_default(),
                    _ => page.content().to_string(),
                };

                if json_output {
                    println!(
                        "{}",
                        serde_json::json!({
                            "status": page.status(),
                            "content_type": page.content_type(),
                            "format": format,
                            "body": body,
                        })
                    );
                } else {
                    print!("{body}");
                }
            }
        }
        None => {
            eprintln!("error: no page loaded");
            std::process::exit(1);
        }
    }

    drop(session_guard);
    browser.close().await?;
    Ok(())
}

// ---------------------------------------------------------------------------
// eval
// ---------------------------------------------------------------------------

/// Evaluate JavaScript on a page and print the result.
async fn run_eval(url: &str, expression: &str, json_output: bool, timeout: u64) -> Result<()> {
    info!(url = %url, expr = %expression, timeout, "evaluating JS");

    let config = oxibrowser_core::BrowserConfig::headless();
    let browser = oxibrowser_core::Browser::new(config).await?;

    let session_result =
        tokio::time::timeout(Duration::from_secs(timeout), browser.new_page(url)).await;

    let session = match session_result {
        Ok(Ok(s)) => s,
        Ok(Err(e)) => {
            eprintln!("error: {e}");
            std::process::exit(1);
        }
        Err(_) => {
            eprintln!("error: timed out after {timeout}s");
            std::process::exit(1);
        }
    };

    // Need write access for evaluate_js (it takes &mut Session).
    let result = {
        let mut guard = session.write().await;
        guard.evaluate_js(expression).await?
    };

    if json_output {
        // JSON output: always produce valid JSON.
        if result.is_ok() {
            let value = result.value.unwrap_or(serde_json::Value::Null);
            println!("{value}");
        } else {
            let err = result.exception.as_deref().unwrap_or("unknown error");
            eprintln!("error: {err}");
            std::process::exit(1);
        }
    } else if result.is_ok() {
        if let Some(v) = &result.value {
            match v {
                serde_json::Value::String(s) => println!("{s}"),
                serde_json::Value::Null => {}
                other => println!("{other}"),
            }
        }
        // void/undefined: nothing to print
        // Also print any captured console output.
        for line in &result.console_output {
            eprintln!("[console] {line}");
        }
    } else {
        let err = result.exception.as_deref().unwrap_or("unknown error");
        eprintln!("error: {err}");
        std::process::exit(1);
    }

    browser.close().await?;
    Ok(())
}

// ---------------------------------------------------------------------------
// extract
// ---------------------------------------------------------------------------

/// Extract structured data from a page.
#[allow(clippy::too_many_arguments)]
async fn run_extract(
    url: &str,
    links: bool,
    title: bool,
    text: bool,
    markdown: bool,
    selector: Option<&str>,
    all: bool,
    json_output: bool,
    timeout: u64,
) -> Result<()> {
    info!(url = %url, timeout, "extracting data");

    let config = oxibrowser_core::BrowserConfig::headless();
    let browser = oxibrowser_core::Browser::new(config).await?;

    let session_result =
        tokio::time::timeout(Duration::from_secs(timeout), browser.new_page(url)).await;

    let session = match session_result {
        Ok(Ok(s)) => s,
        Ok(Err(e)) => {
            eprintln!("error: {e}");
            std::process::exit(1);
        }
        Err(_) => {
            eprintln!("error: timed out after {timeout}s");
            std::process::exit(1);
        }
    };

    let session_guard = session.read().await;

    let page = match session_guard.page() {
        Some(p) => p,
        None => {
            eprintln!("error: no page loaded");
            std::process::exit(1);
        }
    };

    let frame = page.root_frame();
    let doc = frame.document();

    // Collect results into a JSON object for --json mode, otherwise print directly.
    let mut json_map = serde_json::Map::new();

    if title {
        let title_text = page.title().unwrap_or("").to_string();
        if json_output {
            json_map.insert("title".into(), serde_json::Value::String(title_text));
        } else {
            println!("{title_text}");
        }
    }

    if links {
        let link_nodes = doc.query_selector_all("a");
        let hrefs: Vec<String> = link_nodes
            .iter()
            .filter_map(|id| {
                doc.get_node(*id)
                    .and_then(|n| n.href().map(|h| h.to_string()))
            })
            .collect();
        if json_output {
            json_map.insert(
                "links".into(),
                serde_json::Value::Array(
                    hrefs.into_iter().map(serde_json::Value::String).collect(),
                ),
            );
        } else {
            for href in &hrefs {
                println!("{href}");
            }
        }
    }

    if text {
        let body_text = doc.query_text("body").unwrap_or_default();
        if json_output {
            json_map.insert("text".into(), serde_json::Value::String(body_text));
        } else {
            print!("{body_text}");
        }
    }

    if markdown {
        let md = page.to_markdown();
        if json_output {
            json_map.insert("markdown".into(), serde_json::Value::String(md));
        } else {
            print!("{md}");
        }
    }

    if let Some(sel) = selector {
        if all {
            let node_ids = doc.query_selector_all(sel);
            let texts: Vec<String> = node_ids
                .iter()
                .filter_map(|id| doc.text_content(*id))
                .map(|t| t.trim().to_string())
                .filter(|t| !t.is_empty())
                .collect();
            if json_output {
                json_map.insert(
                    "selector".into(),
                    serde_json::Value::String(sel.to_string()),
                );
                json_map.insert(
                    "matches".into(),
                    serde_json::Value::Array(
                        texts.into_iter().map(serde_json::Value::String).collect(),
                    ),
                );
            } else {
                for t in &texts {
                    println!("{t}");
                }
            }
        } else {
            let text_val = doc
                .query_text(sel)
                .map(|t| t.trim().to_string())
                .unwrap_or_default();
            if json_output {
                json_map.insert(
                    "selector".into(),
                    serde_json::Value::String(sel.to_string()),
                );
                json_map.insert("match".into(), serde_json::Value::String(text_val));
            } else {
                println!("{text_val}");
            }
        }
    }

    // If no extract flags were given, default to dumping the page title + text.
    if !title && !links && !text && !markdown && selector.is_none() {
        let title_text = page.title().unwrap_or("").to_string();
        let body_text = doc.query_text("body").unwrap_or_default();
        if json_output {
            json_map.insert("title".into(), serde_json::Value::String(title_text));
            json_map.insert("text".into(), serde_json::Value::String(body_text));
        } else {
            if !title_text.is_empty() {
                println!("Title: {title_text}");
            }
            println!("{body_text}");
        }
    }

    if json_output {
        println!("{}", serde_json::Value::Object(json_map));
    }

    drop(session_guard);
    browser.close().await?;
    Ok(())
}

// ---------------------------------------------------------------------------
// browse
// ---------------------------------------------------------------------------

/// Browse a page using the Tab API (CDP-free).
#[allow(clippy::too_many_arguments)]
async fn run_browse(
    url: &str,
    format: &str,
    click: Option<&str>,
    input: Option<&str>,
    press: Option<&str>,
    wait: Option<&str>,
    wait_timeout: u64,
    extract: Option<&str>,
    all: bool,
    screenshot: Option<&str>,
    width: u32,
    eval: Option<&str>,
    headers: bool,
    timeout: u64,
) -> Result<()> {
    info!(url = %url, timeout, "browsing URL");

    let config = oxibrowser_core::BrowserConfig::headless();
    let browser = oxibrowser_core::Browser::new(config).await?;
    let tab = browser.new_tab().await?;

    let nav_result = tokio::time::timeout(Duration::from_secs(timeout), tab.goto(url)).await;
    let nav = match nav_result {
        Ok(Ok(result)) => result,
        Ok(Err(e)) => {
            eprintln!("error: {e}");
            std::process::exit(1);
        }
        Err(_) => {
            eprintln!("error: timed out after {timeout}s");
            std::process::exit(1);
        }
    };

    if headers {
        eprintln!("HTTP {}", nav.status);
        eprintln!("URL: {}", nav.url);
        eprintln!("Title: {}", nav.title);
    }

    if let Some(selector) = wait {
        tab.wait_for(selector, wait_timeout).await?;
    }
    if let Some(selector) = click {
        tab.click(selector).await?;
    }
    if let Some(spec) = input {
        let (selector, value) = spec
            .split_once(':')
            .ok_or_else(|| anyhow!("--input must be in the form selector:text"))?;
        tab.fill(selector, value).await?;
    }
    if let Some(keys) = press {
        tab.press(keys).await?;
    }

    if let Some(path) = screenshot {
        let png = tab.screenshot(width).await?;
        std::fs::write(path, &png)?;
        eprintln!("Screenshot: {path} ({} bytes)", png.len());
    }

    if let Some(js) = eval {
        let value = tab.evaluate(js).await?;
        match value {
            serde_json::Value::String(s) => print!("{s}"),
            serde_json::Value::Null => {}
            other => print!("{other}"),
        }
    } else if let Some(selector) = extract {
        let matches = tab.query_all(selector).await?;
        if all {
            for text in matches {
                let trimmed = text.trim();
                if !trimmed.is_empty() {
                    println!("{trimmed}");
                }
            }
        } else if let Some(first) = matches.iter().find(|t| !t.trim().is_empty()) {
            println!("{}", first.trim());
        }
    } else {
        let content = tab.content().await?;
        match format {
            "markdown" | "md" => print!("{}", content.markdown),
            "html" => print!("{}", content.html),
            "text" => {
                if let serde_json::Value::String(body) = tab
                    .evaluate("document.body ? document.body.textContent : ''")
                    .await?
                {
                    print!("{body}");
                }
            }
            "json" => println!(
                "{}",
                serde_json::to_string_pretty(&content).unwrap_or_default()
            ),
            "links" => {
                let value = tab
                    .evaluate("Array.from(document.querySelectorAll('a[href]')).map(a => a.href)")
                    .await?;
                if let serde_json::Value::Array(items) = value {
                    for item in items {
                        if let Some(link) = item.as_str() {
                            println!("{link}");
                        }
                    }
                }
            }
            _ => print!("{}", content.markdown),
        }
    }

    tab.close().await?;
    browser.close().await?;
    Ok(())
}

// ---------------------------------------------------------------------------
// serve
// ---------------------------------------------------------------------------

/// Start the CDP server with a real Browser instance.
async fn run_serve(host: &str, port: u16, cookie_file: Option<&str>) -> Result<()> {
    let addr: SocketAddr = format!("{host}:{port}")
        .parse()
        .map_err(|e: std::net::AddrParseError| anyhow::anyhow!("invalid address: {e}"))?;

    info!(addr = %addr, "starting CDP server");

    let mut config = oxibrowser_core::BrowserConfig::headless();
    if let Some(path) = cookie_file {
        config.cookie_file = Some(std::path::PathBuf::from(path));
    }
    // Disable SSRF filter for CDP server mode — clients navigate to arbitrary URLs
    config.enable_ssrf_filter = false;
    let browser = Arc::new(oxibrowser_core::Browser::new(config).await?);

    let server = Arc::new(oxibrowser_cdp::CdpServer::new(addr, browser.clone()));
    let bound_addr = server.start().await?;

    info!(addr = %bound_addr, "CDP server ready");
    println!("OxiBrowser CDP server listening on {bound_addr}");
    println!("  DevTools: http://{bound_addr}/json/version");
    println!("  WebSocket: ws://{bound_addr}/ws");

    tokio::signal::ctrl_c().await?;
    info!("shutting down");

    server.shutdown();
    browser.close().await?;

    Ok(())
}

// ---------------------------------------------------------------------------
// run script
// ---------------------------------------------------------------------------

use oxibrowser_core::script::{parse_script, ScriptRunner};

/// Run a YAML script on a new Tab.
async fn run_script(script_path_or_yaml: &str, timeout: u64) -> Result<()> {
    info!(script = %script_path_or_yaml, timeout, "running script");

    let script_config = if std::path::Path::new(script_path_or_yaml).exists() {
        parse_script(&std::fs::read_to_string(script_path_or_yaml)?)
            .map_err(|e| anyhow::anyhow!("failed to parse script: {e}"))?
    } else {
        parse_script(script_path_or_yaml)
            .map_err(|e| anyhow::anyhow!("failed to parse script: {e}"))?
    };

    // Create a browser and a tab for the script
    let mut browser_config = oxibrowser_core::BrowserConfig::headless();
    browser_config.enable_ssrf_filter = false; // Allow script to navigate anywhere
    let browser = oxibrowser_core::Browser::new(browser_config).await?;

    // Create a tab for the script
    let tab = browser
        .new_tab()
        .await
        .map_err(|e| anyhow::anyhow!("failed to create tab: {e}"))?;

    // Run the script
    let mut runner = ScriptRunner::new(&tab);
    let result = runner
        .run_config(&script_config)
        .await
        .map_err(|e| anyhow::anyhow!("{e}"))?;

    // Print the result
    println!("{}", serde_json::to_string_pretty(&result).unwrap());

    if result.success {
        info!("script completed successfully");
    } else {
        eprintln!("script completed with errors");
    }

    browser.close().await?;
    Ok(())
}