Skip to main content

browser_info/
url_extraction.rs

1use crate::{BrowserInfoError, BrowserType};
2use active_win_pos_rs::ActiveWindow;
3
4/// URL抽出と同じ経路で取れたページ情報。
5///
6/// `title` はプラットフォーム側が**信頼できる出所**から取れたときだけ `Some`。
7/// `None` は「取得手段が無かった」であって「タイトルが空」ではない
8/// (空文字のタイトルも `None` に正規化する)。呼び出し側はこのとき
9/// ウィンドウ名など別の出所に当たる。
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct ExtractedPage {
12    pub url: String,
13    pub title: Option<String>,
14}
15
16/// Extract URL from the active browser window
17pub fn extract_url(
18    window: &ActiveWindow,
19    browser_type: &BrowserType,
20) -> Result<String, BrowserInfoError> {
21    extract_page(window, browser_type).map(|page| page.url)
22}
23
24/// Extract URL and (where the platform can provide it) the page title
25/// from the active browser window
26pub fn extract_page(
27    window: &ActiveWindow,
28    browser_type: &BrowserType,
29) -> Result<ExtractedPage, BrowserInfoError> {
30    #[cfg(target_os = "windows")]
31    {
32        crate::platform::windows::extract_page(window, browser_type)
33    }
34
35    #[cfg(target_os = "macos")]
36    {
37        crate::platform::macos::extract_page(window, browser_type)
38    }
39
40    #[cfg(target_os = "linux")]
41    {
42        let _ = (window, browser_type); // Suppress unused variable warnings
43        // TODO: Implement Linux URL extraction
44        Err(BrowserInfoError::PlatformError(
45            "Linux not yet implemented".to_string(),
46        ))
47    }
48
49    #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
50    {
51        let _ = (window, browser_type); // Suppress unused variable warnings
52        Err(BrowserInfoError::PlatformError(
53            "Unsupported platform".to_string(),
54        ))
55    }
56}