Skip to main content

browser_url/
lib.rs

1//! # browser-url
2//!
3//! Cross-platform library for retrieving active browser URL and detailed information.
4//!
5//! Built on top of `active-win-pos-rs` for reliable window detection, with specialized
6//! browser information extraction capabilities.
7//!
8//! ## Quick Start
9//!
10//! ```rust
11//! use browser_url::get_active_browser_url;
12//!
13//! match get_active_browser_url() {
14//!     Ok(info) => {
15//!         println!("URL: {}", info.url);
16//!     }
17//!     Err(e) => eprintln!("Error: {}", e),
18//! }
19//! ```
20
21use active_win_pos_rs::get_active_window;
22use serde::{Deserialize, Serialize};
23
24pub mod browser_detection;
25pub mod error;
26pub mod url_extraction;
27
28pub mod platform;
29
30pub use error::BrowserInfoError;
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct BrowserInfo {
34    pub url: String,
35    pub title: String,
36    pub browser_name: String,
37    pub browser_type: BrowserType,
38    // pub is_incognito: bool,
39    pub process_id: u64,
40    pub window_position: WindowPosition,
41}
42
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44pub enum BrowserType {
45    Chrome,
46    Firefox,
47    Zen,
48    Edge,
49    Safari,
50    Brave,
51    Opera,
52    Vivaldi,
53    Unknown(String),
54}
55
56#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
57pub struct WindowPosition {
58    pub x: f64,
59    pub y: f64,
60    pub width: f64,
61    pub height: f64,
62}
63
64//================================================================================================
65// procedure
66//================================================================================================
67
68/// Retrieve information about the currently active browser
69///
70/// This function combines window detection (via `active-win-pos-rs`) with
71/// specialized browser information extraction.
72///
73/// # Examples
74///
75/// ```rust
76/// use browser_url::get_active_browser_url;
77///
78/// match get_active_browser_url() {
79///     Ok(info) => {
80///         println!("URL: {}", info.url);
81///         println!("Title: {}", info.title);
82///         println!("Browser: {}", info.browser_name);
83///         println!("Process ID: {}", info.process_id);
84///         println!("Position: ({}, {})", info.window_position.x, info.window_position.y);
85///         println!("Size: {}x{}", info.window_position.width, info.window_position.height);
86///     }
87///     Err(e) => eprintln!("Failed to get browser info: {}", e),
88/// }
89/// ```
90
91pub fn get_active_browser_url() -> Result<BrowserInfo, BrowserInfoError> {
92    if !is_browser_active() {
93        return Err(BrowserInfoError::NotABrowser);
94    }
95
96    let window = get_active_window().map_err(|_| BrowserInfoError::WindowNotFound)?;
97
98    let browser_type = browser_detection::classify_browser(&window)?;
99
100    let url = url_extraction::extract_url(&window, &browser_type)?;
101
102    Ok(BrowserInfo {
103        url,
104        title: window.title,
105        browser_name: window.app_name,
106        browser_type,
107        // is_incognito: url.is_incognito,
108        process_id: window.process_id,
109        window_position: WindowPosition {
110            x: window.position.x,
111            y: window.position.y,
112            width: window.position.width,
113            height: window.position.height,
114        },
115    })
116}
117
118
119pub fn is_browser_active() -> bool {
120    if let Ok(window) = get_active_window() {
121        browser_detection::classify_browser(&window).is_ok()
122    } else {
123        false
124    }
125}