Skip to main content

is_browser_active

Function is_browser_active 

Source
pub fn is_browser_active() -> bool
Examples found in repository?
examples/basic_usage.rs (line 26)
5fn main() {
6    println!("🌐 Browser URL - Basic Usage Demo");
7    println!("==================================");
8
9    // Give the user time to switch to a browser window
10    println!("\nšŸ“‹ Instructions:");
11    println!("1. Open a browser (Chrome, Firefox, Zen, etc.)");
12    println!("2. Navigate to any website");
13    println!("3. When it says 'NOW!', quickly click on the browser window");
14    println!("\nā° Starting in 5 seconds...");
15
16    for i in (1..=5).rev() {
17        println!("   {i} seconds...");
18        thread::sleep(Duration::from_secs(1));
19    }
20
21    println!("\nšŸš€ NOW! Quickly click on your browser window!");
22    thread::sleep(Duration::from_millis(2000));
23
24    // Check if a browser is the active window
25    println!("\nšŸ” Checking for active browser...");
26    if !is_browser_active() {
27        println!("āŒ No browser window detected as active");
28        println!("\nšŸ”„ Retrying for 10 seconds — switch to your browser!");
29
30        let mut found = false;
31        for i in (1..=10).rev() {
32            thread::sleep(Duration::from_secs(1));
33            if is_browser_active() {
34                println!("āœ… Browser detected!");
35                found = true;
36                break;
37            }
38            if i > 1 {
39                println!("   Checking... {} seconds left", i - 1);
40            }
41        }
42
43        if !found {
44            println!("āŒ Still no browser detected.");
45            println!("šŸ’” Try running again while keeping a browser window focused:");
46            println!("   cargo run --example basic_usage");
47            return;
48        }
49    } else {
50        println!("āœ… Browser detected immediately!");
51    }
52
53    // Extract browser info including URL
54    println!("\nšŸ”— Extracting browser URL...");
55    match get_active_browser_url() {
56        Ok(info) => {
57            println!("āœ… Browser info extracted successfully!\n");
58            println!("   šŸ”— URL:        {}", info.url);
59            println!("   šŸ“ Title:      {}", info.title);
60            println!("   🌐 Browser:    {} ({:?})", info.browser_name, info.browser_type);
61            println!("   šŸ†” Process ID: {}", info.process_id);
62            println!(
63                "   šŸ“ Position:   ({:.0}, {:.0})",
64                info.window_position.x, info.window_position.y
65            );
66            println!(
67                "   šŸ“ Size:       {:.0}x{:.0}",
68                info.window_position.width, info.window_position.height
69            );
70        }
71        Err(e) => {
72            println!("āŒ Failed to extract browser info: {e}");
73        }
74    }
75
76    println!("\nšŸŽÆ Demo completed!");
77}