2

为了在 Firefox 中检测 URL,我使用 DDE

 DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
 dde.Connect();
 string url1 = dde.Request("URL", int.MaxValue);
 dde.Disconnect();
 temp = url1.Replace("\"", "").Replace("\0", "");
 dde = null;

此代码完美运行!但连接(dde.Connect();)太慢了7/8秒!还有其他方法可以从 Firefox 获取 url 吗?(例如使用 API 窗口,如“SendMessage”)

4

2 回答 2

2
            Process[] process = Process.GetProcessesByName("firefox");

            foreach (Process firefox in process)
            {
                // the chrome process must have a window
                if (firefox.MainWindowHandle == IntPtr.Zero)
                {
                    return null;
                }

                AutomationElement element = AutomationElement.FromHandle(firefox.MainWindowHandle);
                if (element == null)
                    return null;

                //search for first custom element
                AutomationElement custom1 = element.FindFirst(TreeScope.Descendants,
                 new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

                //search all custom element children
                AutomationElementCollection custom2 = custom1.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

                //for each custom child
                foreach (AutomationElement item in custom2)
                {
                    //search for first custom element
                    AutomationElement custom3 = item.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));
                    //search for first document element
                    AutomationElement doc3 = custom3.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));



                    if (!doc3.Current.IsOffscreen)
                    {
                        url = ((ValuePattern)doc3.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
                        return url;
                    }                      
                }
            }             
            return url;
   }
于 2014-10-05T10:39:10.640 回答
2

这对我很有效:

                AutomationElement element = AutomationElement.FromHandle(intPtr);  // intPtr is the MainWindowHandle for FireFox browser
                element = element.FindFirst(TreeScope.Subtree,
                      new AndCondition(
                          new PropertyCondition(AutomationElement.NameProperty, "Search or enter address"),
                          new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
                string url = ((ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
于 2017-06-27T23:54:44.870 回答