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?
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.
In javascript, you can access the URL by way of
window.location.href
精彩评论