use browser_info::{
ExtractionMethod, get_browser_info, get_browser_info_safe, get_browser_info_with_method,
};
use browser_info::{get_active_browser_info, get_active_browser_url, is_browser_active};
#[cfg(all(feature = "devtools", target_os = "windows"))]
use browser_info::get_browser_info_fast;
use std::thread;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("๐ Browser Info Library - Basic Usage Demo");
println!("==========================================");
println!("\n๐ Instructions:");
println!("1. Open a browser (Chrome, Firefox, Edge, etc.)");
println!("2. Navigate to any website");
println!("3. Wait for the countdown to finish");
println!("4. When it says 'NOW!', quickly click on the browser window");
println!("\nโฐ Starting in 5 seconds...");
for i in (1..=5).rev() {
println!(" {i} seconds...");
thread::sleep(Duration::from_secs(1));
}
println!("\n๐ NOW! Quickly click on your browser window!");
thread::sleep(Duration::from_millis(2000));
println!("\n๐ Checking for active browser...");
if !is_browser_active() {
println!("โ No browser window detected as active");
println!("\n๐ Let's try a different approach...");
println!("๐ Please click on your browser window, then press Enter here...");
println!(" (You have 10 seconds to switch)");
for i in (1..=10).rev() {
thread::sleep(Duration::from_secs(1));
if is_browser_active() {
println!("โ
Browser detected!");
break;
}
if i > 1 {
println!(" Checking... {} seconds left", i - 1);
}
}
if !is_browser_active() {
println!("โ Still no browser detected");
println!("๐ก Try running this command while keeping a browser open:");
println!(" cargo run --example basic_usage");
return Ok(());
}
} else {
println!("โ
Browser detected immediately!");
}
println!("\n๐ Testing URL extraction...");
match get_active_browser_url() {
Ok(url) => {
println!("โ
URL extracted: {url}");
}
Err(e) => {
println!("โ ๏ธ URL extraction failed: {e}");
println!(" (This is expected with current dummy implementation)");
}
}
println!("\n๐ Testing full browser info extraction...");
match get_active_browser_info() {
Ok(info) => {
println!("โ
Full information extracted:");
println!(" ๐ URL: {url}", url = info.url);
println!(" ๐ Title: {title}", title = info.title);
println!(
" ๐ Browser: {} ({:?})",
info.browser_name, info.browser_type
);
println!(
" ๐ Process ID: {process_id}",
process_id = info.process_id
);
println!(
" ๐ Position: ({:.0}, {:.0})",
info.window_position.x, info.window_position.y
);
println!(
" ๐ Size: {:.0}x{:.0}",
info.window_position.width, info.window_position.height
);
if info.is_incognito {
println!(" ๐ Private browsing: Yes");
}
}
Err(e) => {
println!("โ Full info extraction failed: {e}");
}
}
println!("\n๐๏ธ Testing different extraction methods...");
println!("\n1๏ธโฃ Auto method:");
match get_browser_info().await {
Ok(info) => println!(
" โ
Auto: {browser_name} - {title}",
browser_name = info.browser_name,
title = info.title
),
Err(e) => println!(" โ Auto failed: {e}"),
}
#[cfg(all(feature = "devtools", target_os = "windows"))]
{
println!("\n2๏ธโฃ Fast method (DevTools - Windows only):");
match get_browser_info_fast().await {
Ok(info) => println!(
" โ
Fast: {browser_name} - {title}",
browser_name = info.browser_name,
title = info.title
),
Err(e) => println!(" โ Fast failed: {e}"),
}
}
#[cfg(not(all(feature = "devtools", target_os = "windows")))]
{
println!("\n2๏ธโฃ Fast method: Not available on this platform (Windows only)");
}
println!("\n3๏ธโฃ Safe method (Cross-platform):");
match get_browser_info_safe() {
Ok(info) => println!(
" โ
Safe: {browser_name} - {title}",
browser_name = info.browser_name,
title = info.title
),
Err(e) => println!(" โ Safe failed: {e}"),
}
println!("\n4๏ธโฃ Explicit method specification:");
for method in [
ExtractionMethod::Auto,
ExtractionMethod::DevTools,
ExtractionMethod::PowerShell,
] {
match get_browser_info_with_method(method).await {
Ok(info) => println!(
" โ
{method:?}: {browser_name} - {title}",
browser_name = info.browser_name,
title = info.title
),
Err(e) => println!(" โ {method:?} failed: {e}"),
}
}
println!("\n๐ฏ Test completed!");
println!("๐ก Notes:");
println!(" โข DevTools methods require Chrome with --remote-debugging-port=9222");
println!(" โข DevTools and some methods are Windows-only");
println!(" โข macOS uses AppleScript, Linux support is planned");
Ok(())
}