alex
alex

Reputation: 1278

Getting Firefox URL

I need to know the URL on which the user currently is.(with Firefox)
I thought of a keylogger to keep track of the URL, but what when the user clicks a link?

The title is not enough, I need the complete URL.
With IE this is easy, but with Firefox it isn't.
for IE I'm using:

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;
}

any ideas?

Upvotes: 4

Views: 1983

Answers (2)

Hovhannes Hakobyan
Hovhannes Hakobyan

Reputation: 1326

You can get the URL using Windows IAccessible interface.

For easy IAccessible manipulation I'll suggest to use Managed Windows API library. You should have FireFox window handle in advance.

Here is C# code to grab the URL from FireFox:

    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;
    }

This works for FireFox 14.

Upvotes: 3

James Maroney
James Maroney

Reputation: 3256

In javascript, you can access the URL by way of

window.location.href

Upvotes: 0

Related Questions