获取Firefox URL?

时间:2009-01-10 05:40:04

标签: c# .net firefox

如何使用.NET 2.0 Windows / console应用程序从正在运行的firefox实例获取url? C#或VB代码都可以。

谢谢!

8 个答案:

答案 0 :(得分:6)

以Rob Kennedy的答案为基础并使用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;
        }
}

注意:这很慢。我的电脑需要几秒钟。结果将如下所示:

"http://stackoverflow.com/questions/430614/get-firefox-url","Get Firefox URL? - Stack Overflow",""

有关浏览器DDE here的更多信息。

答案 1 :(得分:4)

对于大多数浏览器,包括Internet Explorer,Navigator,Firefox和Opera,支持和批准的方法是use DDE。所有主题名称都是WWW_GetWindowInfo;只有目标窗口的名称不同。但是,这种技术对你来说很难,因为.Net不支持DDE。如果你能找到解决这个限制的方法,那么你将会全力以赴。

答案 2 :(得分:2)

答案 3 :(得分:1)

您可能需要查看WatiN的源代码。他们的下一个版本是开源的,并且支持firefox,所以我认为这样做的功能就在其中。

答案 4 :(得分:1)

穷人的解决方案,如果有任何其他方法失败:激活Firefox窗口,发送Ctrl + L(激活地址栏),发送Ctrl + C(复制选择,即URL,到剪贴板)并读取剪贴板。

这种方法存在很多问题(其中如果用户在计算机前面,它会为用户做些奇怪的事情),所以它只是一个备份解决方案......

答案 5 :(得分:1)

使用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"));

答案 6 :(得分:1)

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

请执行以下操作以运行此代码 添加参考> Com>来自项目中VS.NET的Microsoft.Internet.Controls

http://ndde.codeplex.com/下载适用于DdeClient类的bin并将其添加到项目中

如果有任何问题,请告诉我

答案 7 :(得分:0)

尝试这个:

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

完整的源代码:https://github.com/Moeedahmad899/GetFirefoxURL