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}