browser-info 1.0.0

Cross-platform(planned) library retrieving active browser URL and detailed information
Documentation
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!("==========================================");

    // Step 1: ๆ“ไฝœ็”จใฎๆ™‚้–“
    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)); // 2็ง’ใฎ็Œถไบˆ

    // Step 2: ใƒ–ใƒฉใ‚ฆใ‚ถใƒใ‚งใƒƒใ‚ฏ
    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)");

        // 10็ง’้–“ใ€1็ง’ใŠใใซใƒใ‚งใƒƒใ‚ฏ
        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!");
    }

    // Step 3: URLๅ–ๅพ—ใƒ†ใ‚นใƒˆ
    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)");
        }
    }

    // Step 4: ๅฎŒๅ…จใชๆƒ…ๅ ฑๅ–ๅพ—
    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...");

    // 1. ่‡ชๅ‹•้ธๆŠž
    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}"),
    }

    // 2. ้ซ˜้€Ÿใƒขใƒผใƒ‰ (Windows only)
    #[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)");
    }

    // 3. ๅฎ‰ๅ…จใƒขใƒผใƒ‰ (Cross-platform)
    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}"),
    }

    // 4. ๆ˜Ž็คบ็š„ๆŒ‡ๅฎš
    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(())
}