(MVC) MVC (2018 год)

Small .Net Wrapper Around Firefox

If you want deep parse HTML inside Firefox windows this class is not enough, please look at page CefSharp.Winforms.ChromiumWebBrowser minimal example on VB.NET (with cookies collector and script executor), but if you goal is only manipulate URL in various Firefox instance, this class is best choice.

This class use ProcessExtensions extension function, descripted in page How to Read URLs from Firefox tabs by UI Automation. and also I have separated this class for two level - simple wrapper and UIautomation level.


   1:  Imports Microsoft.Win32
   2:   
   3:  Class Firefox
   4:      Inherits UiAutomation
   5:   
   6:      Private FirefoxPath As String
   7:   
   8:      Public Sub New()
   9:          Dim programFilesX64 As String = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion")?.GetValue("ProgramFilesDir")
  10:          Dim FirefoxPath64 As String = String.Format("{0}\Mozilla Firefox\firefox.exe", programFilesX64)
  11:          Dim FirefoxPath32 As String = String.Format("{0}\Mozilla Firefox\firefox.exe", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))
  12:          Dim Firefox32 As IO.FileInfo = New IO.FileInfo(FirefoxPath32)
  13:          Dim Firefox64 As IO.FileInfo = New IO.FileInfo(FirefoxPath64)
  14:          If Firefox32.Exists Then
  15:              FirefoxPath = FirefoxPath32
  16:          ElseIf Firefox64.Exists Then
  17:              FirefoxPath = FirefoxPath64
  18:          End If
  19:      End Sub
  20:   
  21:      Public Function IsFirefoxAvailable() As Boolean
  22:          If FirefoxPath = "" Then Return False Else Return True
  23:      End Function
  24:   
  25:      Public Sub OpenFirefox()
  26:          Open(String.Empty)
  27:      End Sub
  28:   
  29:      Public Sub OpenFirefox(ByVal url As String)
  30:          Open(url)
  31:      End Sub
  32:   
  33:      Public Sub OpenFirefoxInSaveMode()
  34:          Open("-safe-mode")
  35:      End Sub
  36:   
  37:      Public Sub OpenFirefoxInNewWindow(ByVal url As String)
  38:          Open(String.Format("-new-window {0}", url))
  39:      End Sub
  40:   
  41:      Public Sub OpenFirefoxInNewTab(ByVal url As String)
  42:          Open(String.Format("-new-tab {0}", url))
  43:      End Sub
  44:   
  45:      Private Sub Open(ByVal arguments As String)
  46:          If Not IsFirefoxAvailable() Then
  47:              Throw New Exception("Firefox is not installed.")
  48:          Else
  49:              Process.Start(FirefoxPath, arguments)
  50:          End If
  51:      End Sub
  52:   
  53:  End Class

   1:  Imports System.Windows.Automation
   2:  Public Class UiAutomation
   3:   
   4:      Dim Arr1 As New List(Of String)
   5:      Dim Firefox As Process() = Process.GetProcessesByName("firefox")
   6:   
   7:      Public Function IsFirefoxOpen() As Boolean
   8:          If Firefox.Length > 0 Then
   9:              Return True
  10:          Else
  11:              Return False
  12:          End If
  13:      End Function
  14:   
  15:      Public Function GetOpenedUrls() As String()
  16:          If Firefox.Length > 0 Then
  17:              Dim Parent = Firefox(0).Parent
  18:              Dim RootElement As AutomationElement = AutomationElement.FromHandle(Parent.MainWindowHandle)
  19:              Dim Controls As Condition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
  20:              Dim ControlTypeCondition As Condition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom)
  21:              Dim CustomControl_Level_1 As AutomationElement = GetAllNextLevelCustomControl(RootElement, ControlTypeCondition)
  22:              For Each CustomControl_Level_2 As AutomationElement In CustomControl_Level_1.FindAll(TreeScope.Children, ControlTypeCondition)
  23:                  For Each CustomControl_Level_3 As AutomationElement In CustomControl_Level_2.FindAll(TreeScope.Children, ControlTypeCondition)
  24:                      GetDocumentFromControl(CustomControl_Level_3)
  25:                  Next
  26:              Next
  27:              Return Arr1.ToArray
  28:          End If
  29:      End Function
  30:   
  31:      Private Function GetDocumentFromControl(CustomControl As AutomationElement)
  32:          Dim condDocument As Condition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)
  33:          Dim Document1 As AutomationElement = CustomControl.FindFirst(TreeScope.Children, condDocument)
  34:          If Document1 IsNot Nothing Then
  35:              For Each pattern As AutomationPattern In Document1.GetSupportedPatterns()
  36:                  If TypeOf Document1.GetCurrentPattern(pattern) Is ValuePattern Then
  37:                      Dim Url1 As String = (TryCast(Document1.GetCurrentPattern(pattern), ValuePattern)).Current.Value.ToString()
  38:                      Arr1.Add(Url1)
  39:                  End If
  40:              Next
  41:          End If
  42:      End Function
  43:   
  44:      Private Function GetAllNextLevelCustomControl(ByVal rootElement As AutomationElement, ByVal condCustomControl As Condition) As AutomationElement
  45:          Return rootElement.FindAll(TreeScope.Children, condCustomControl).Cast(Of AutomationElement)().ToList().Where(Function(x) x.Current.BoundingRectangle <> System.Windows.Rect.Empty).FirstOrDefault()
  46:      End Function
  47:   
  48:  End Class


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/NetWrapperAroundFirefox/index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>