You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF桌面程序获取多浏览器当前页面URL的问题求助

问题:跨浏览器活动标签页URL获取方案优化

我开发了一款WPF桌面应用,可检测活动窗口,当活动窗口为浏览器时获取当前进程的URL,但现有方法仅50%有效——部分网站无法追踪,部分浏览器无法精准获取,还存在耗时久、CPU占用高的问题。

已尝试的方案及问题

1. DdeClient 实现(针对Firefox)

public static string GetFirefoxURL()
{
    try
    {
        DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
        dde.Connect();
        string url = dde.Request("URL", int.MaxValue);
        string[] text = url.Split(new string[] {"",""}, StringSplitOptions.RemoveEmptyEntries);
        dde.Disconnect();
        return text[0].Substring(1);
    }
    catch
    {
        return null;
    }
}

该方法稳定性差,频繁出现连接失败或返回空值的情况。

2. UI Automation 实现

通用UIA实现

// 前置代码
var hWnd = NativeMethods.GetForegroundWindow();
var process = NativeMethods.GetWindowThreadProcessId(hWnd, out processID);

// URL追踪函数
public static string GetUrlFromProcess(Process process)
{
    using (process)
    {
        try
        {
                 CUIAutomation _automation = new CUIAutomation();
                 IUIAutomationElement elm = _automation.ElementFromHandle(process.MainWindowHandle);
                 IUIAutomationCondition Cond = _automation.CreatePropertyCondition(30003, 50004);
                 IUIAutomationElementArray elm2 = elm.FindAll(TreeScope.TreeScope_Descendants, Cond);

                 if (elm2 != null)
                 {
                     for (int i = 0; i < elm2.Length; i++)
                     {
                         IUIAutomationElement elm3 = elm2.GetElement(i);
                         IUIAutomationValuePattern val = (IUIAutomationValuePattern)elm3.GetCurrentPattern(10002);
                         if (val != null)
                         {
                             if (val.CurrentValue != "")
                             {
                                 return val.CurrentValue;
                             }
                         }
                         return "";
                     }
                 }
                 return "";
        }

        catch
        { return "something went wrong"; }
    }
}

此方法遍历控件层级时效率极低,CPU占用高,且无法适配部分浏览器和网站的控件结构,导致URL获取失败。

Firefox专用UIA实现

public static string GetFirefoxUrl(Process process)
{
    if (process == null)
        throw new ArgumentNullException("process");

    if (process.MainWindowHandle == IntPtr.Zero)
        return null;

    AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
    if (element == null)
        return null;

    AutomationElement doc = element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
    if (doc == null)
        return null;
    var d = ((ValuePattern)doc.GetCurrentPattern(ValuePattern.Pattern));
    var c = d.Current;
    var b = c.Value;
    Debug.Write("url ff: " + b);
    return b;
// 此方法仅返回Firefox第一个标签页的URL,无法识别当前活动标签页
}

仅能获取Firefox的第一个标签页URL,无法定位到当前活动的标签页。

需求

需要实现**任意网站(包括全屏播放的YouTube)**在Chrome、Opera、Firefox、Edge、Brave等主流浏览器中的当前活动标签页URL获取,同时要求低CPU占用、快速响应。

恳请提供可行解决方案,万分感谢!

内容的提问来源于stack exchange,提问作者MJR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 02:03:36