【问题标题】:Get Firefox URL?获取火狐网址?
【发布时间】:2010-09-30 15:28:59
【问题描述】:

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

谢谢!

【问题讨论】:

  • 你需要清楚一点。您的意思是如何从 windows/console 应用程序中运行的 firefox 实例获取 url?
  • 是的...我已经编辑了这个问题。谢谢!
  • 如果可以做到这一点,您就会意识到可能有多个实例,每个实例都有多个选项卡。你想达到什么目的?
  • 是的,我确实意识到了这一点。我有这个与 IE 一起使用,事实上,我有这个与 firefox 一起使用,但它使用的是 .NET 3.5 类,但客户希望在 .NET 2.0 中完成它

标签: c# .net firefox


【解决方案1】:

以 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 的更多信息。

【讨论】:

  • 我从几年前就开始使用这段代码了。然而,随着新版本 FF 的发布,它不再工作了。不知道有没有其他方法可以达到同样的效果?
【解决方案2】:

对于大多数浏览器,包括 Internet Explorer、Navigator、Firefox 和 Opera,支持和认可的方式是 use DDE。它们中的主题名称都是WWW_GetWindowInfo;只有目标窗口的名称不同。但是,这种技术对您来说会很困难,因为 .Net 不支持 DDE。如果您能找到解决该限制的方法,那么您将万事俱备。

【讨论】:

    【解决方案3】:
    【解决方案4】:

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

    【讨论】:

    • 我已编辑问题以添加更多详细信息。我正在做一个 winforms 应用程序,我需要获取浏览器 URL。我已经有了 IE Url 的代码。谢谢!
    【解决方案5】:

    使用 MozRepl:https://github.com/bard/mozrepl/wiki/ + mozRepl .NET 连接器:http://mozreplconnector.codeplex.com/releases/view/17398

      var connect = new MozReplConnectDotNet.MozReplConnect(4242);
      connect.Connect();
      Console.WriteLine(connect.SendRecieve("gBrowser.currentURI.spec"));
    

    【讨论】:

      【解决方案6】:
          [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;
          }
      

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

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

      如果有任何问题请告诉我

      【讨论】:

        【解决方案7】:

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

        这种方法有很多问题(其中如果用户在电脑前,它会对用户产生奇怪的影响),所以它只是一种备份解决方案......

        【讨论】:

        • 没有。因为我不懂C#。我会用 AutoHotkey 做到这一点... :-P
        【解决方案8】:

        试试这个:

                //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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-04
          • 2011-06-04
          • 2018-04-17
          • 1970-01-01
          • 2011-10-10
          • 1970-01-01
          • 1970-01-01
          • 2020-03-29
          相关资源
          最近更新 更多