C#从firefox获取URL但不使用DDE

问题描述 投票:0回答: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 吗? (例如使用“SendMessage”等 API 窗口)

c# api url firefox dde
2个回答
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;

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;
   }
© www.soinside.com 2019 - 2024. All rights reserved.