获取firefox网址

时间:2010-01-07 14:35:19

标签: c# firefox

我需要知道用户当前的网址。(使用firefox)
我想到了一个用于跟踪网址的键盘记录器,
但是当用户点击链接时会发生什么? 标题还不够,我需要完整的网址 使用IE浏览器这很容易,但是使用firefox它不是 对于IE我正在使用:

private string GetUrlFromIE()
{
IntPtr windowHandle = GetForegroundWindow();
IntPtr childHandle;
String strUrlToReturn = "";

//IE's toolbar container
childHandle = FindWindowEx(windowHandle,IntPtr.Zero,"WorkerW",IntPtr.Zero);
if(childHandle != IntPtr.Zero)
{
    //get a handle to address bar
    childHandle = FindWindowEx(childHandle,IntPtr.Zero,"ReBarWindow32",IntPtr.Zero);
    if(childHandle != IntPtr.Zero)
    {
        // get a handle to combo boxes
        childHandle = FindWindowEx(childHandle, IntPtr.Zero, "ComboBoxEx32", IntPtr.Zero);
        if(childHandle != IntPtr.Zero)
        {
            // get a handle to combo box
            childHandle = FindWindowEx(childHandle, IntPtr.Zero, "ComboBox", IntPtr.Zero);
            if(childHandle != IntPtr.Zero)
            {
                //get handle to edit
                childHandle = FindWindowEx(childHandle, IntPtr.Zero, "Edit", IntPtr.Zero);
                if (childHandle != IntPtr.Zero)
                {
                    strUrlToReturn = GetText(childHandle);
                }
            }
        }
    }
}
return strUrlToReturn;
}

任何想法?

2 个答案:

答案 0 :(得分:3)

您可以使用Windows IAccessible 界面获取网址。

为了便于IAccessible操作,我建议使用 Managed Windows API 库。 你应该提前使用FireFox窗口处理。

以下是从FireFox获取网址的C#代码:

    private static string GetUrlFromFirefox(IntPtr windowHandle)
    {
        SystemAccessibleObject sao = SystemAccessibleObject.FromWindow(new SystemWindow(windowHandle), AccessibleObjectID.OBJID_WINDOW);
        var preds = new Predicate<SystemAccessibleObject>[] { 
            s => s.RoleString == "application",
            s => s.RoleString == "property page",
            s => s.RoleString == "grouping" && s.StateString == "None",
            s => s.RoleString == "property page" && s.StateString == "None",
            s => s.RoleString == "browser",
            s => s.RoleString == "document" && s.Visible
        };

        var current = sao.Children;
        SystemAccessibleObject child = null;
        foreach (var pred in preds)
        {
            child = Array.Find(current, pred);
            if (child != null)
            {
                current = child.Children;
            }
        }

        if (child != null)
        {
            return child.Value;
        }

        return string.Empty;
    }

适用于FireFox 14。

答案 1 :(得分:0)

在javascript中,您可以通过

访问该网址
window.location.href