How can I get the url from a running instance of firefox using .NET 2.0 windows/console app? C# or VB codes will do.
Thanks!
How can I get the url from a running instance of firefox using .NET 2.0 windows/console app? C# or VB codes will do.
Thanks!
Building on Rob Kennedy's answer and using NDde
using NDde.Client;
class Test
{
public static string GetFirefoxURL()
{
DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
dde.Disconnect();
return url;
}
}
NB: This is very slow. It takes a few seconds on my computer. The result will look something like this :
"http://stackoverflow.com/questions/430614/get-firefox-url","Get Firefox URL? - Stack Overflow",""
More info on browser DDE here.
For most browsers, including Internet Explorer, Navigator, Firefox, and Opera, the supported and sanctioned way of doing this is to use DDE. The topic name in all of them is WWW_GetWindowInfo
; only the name of the target window varies. That technique will be difficult for you, though, because .Net doesn't support DDE. If you can find a way to get around that limitation, you'll be all set.
it seems that this might be difficult, here's some discussion on it: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/c60b1699-9fd7-408d-a395-110c1cd4f297/
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr parentHandle,
IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd,
int msg, int wParam, StringBuilder ClassName);
private static string GetURL(IntPtr intPtr, string programName, out string url)
{
string temp=null;
if (programName.Equals("chrome"))
{
var hAddressBox = FindWindowEx(intPtr, IntPtr.Zero, "Chrome_OmniboxView", IntPtr.Zero);
var sb = new StringBuilder(256);
SendMessage(hAddressBox, 0x000D, (IntPtr)256, sb);
temp = sb.ToString();
}
if (programName.Equals("iexplore"))
{
foreach (InternetExplorer ie in new ShellWindows())
{
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(ie.FullName);
if (fileNameWithoutExtension != null)
{
var filename = fileNameWithoutExtension.ToLower();
if (filename.Equals("iexplore"))
{
temp+=ie.LocationURL + " ";
}
}
}
}
if (programName.Equals("firefox"))
{
DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
dde.Connect();
string url1 = dde.Request("URL", int.MaxValue);
dde.Disconnect();
temp = url1.Replace("\"","").Replace("\0","");
}
url = temp;
return temp;
}
Please do following to run this code Add Reference > Com > Microsoft.Internet.Controls from VS.NET in your project
Download the bin from http://ndde.codeplex.com/ for DdeClient class and add it to your project
Please Let me know if any issue
You may want to check into the source code of WatiN. Their next version is open source and supports firefox, so I would imagine the functionality for doing this is in it.
Use MozRepl: https://github.com/bard/mozrepl/wiki/ + mozRepl .NET Connector: http://mozreplconnector.codeplex.com/releases/view/17398
var connect = new MozReplConnectDotNet.MozReplConnect(4242);
connect.Connect();
Console.WriteLine(connect.SendRecieve("gBrowser.currentURI.spec"));
Poor man's solution, if anything else fails: activate the Firefox window, send Ctrl+L (activates address bar), send Ctrl+C (copy selection, ie. URL, to clipboard) and read the clipboard.
Lot of issues with this method (among them it does strange stuff for the user if they are in front of the computer) so it is only a backup solution...
try this one:
//get all running process of firefox
Process[] procsfirefox = Process.GetProcessesByName("firefox");
foreach (Process firefox in procsfirefox)
{
//the firefox process must have a window
if (firefox.MainWindowHandle == IntPtr.Zero)
{
continue;
}
AutomationElement sourceElement = AutomationElement.FromHandle(firefox.MainWindowHandle);
AutomationElement editBox = sourceElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Search with Google or enter address"));
// if it can be found, get the value from the editbox
if (editBox != null)
{
ValuePattern val = ((ValuePattern)editBox.GetCurrentPattern(ValuePattern.Pattern));
Console.WriteLine("\n Firefox URL found: " + val.Current.Value);
}
//-----------------------------find titlebar element for site title---------------------------------//
AutomationElement elmTitleBar = sourceElement.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar));
if (elmTitleBar != null)
Console.WriteLine("\n Firefox TitleBar found: " + elmTitleBar.Current.Name);
}
full source code:https://github.com/Moeedahmad899/GetFirefoxURL