Skip to main content

get_active_browser_url

Function get_active_browser_url 

Source
pub fn get_active_browser_url() -> Result<BrowserInfo, BrowserInfoError>
Expand description

Retrieve information about the currently active browser

This function combines window detection (via active-win-pos-rs) with specialized browser information extraction.

ยงExamples

use browser_url::get_active_browser_url;

match get_active_browser_url() {
    Ok(info) => {
        println!("URL: {}", info.url);
        println!("Title: {}", info.title);
        println!("Browser: {}", info.browser_name);
        println!("Process ID: {}", info.process_id);
        println!("Position: ({}, {})", info.window_position.x, info.window_position.y);
        println!("Size: {}x{}", info.window_position.width, info.window_position.height);
    }
    Err(e) => eprintln!("Failed to get browser info: {}", e),
}
Examples found in repository?
examples/basic_usage.rs (line 55)
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}