【发布时间】:2018-10-25 10:53:10
【问题描述】:
我正在编写一个应用程序以在 Chrome 浏览器中捕获活动标签的 URL。我浏览了一些代码,所有这些代码都会导致 GUI 冻结并且需要很长时间才能获取 URL。我真正想要的是获取活动标签的 URL。就是这样。这是我用过的。非常感谢您的帮助。
Imports System.Windows.Automation
Public Class Form1
Private Const ChromeProcess As [String] = "chrome"
Private Const AddressCtl As [String] = "Address and search bar"
Public Function GetChromeActiveWindowUrl() As [String]
Dim procs = Process.GetProcessesByName(ChromeProcess)
If (procs.Length = 0) Then
Return [String].Empty
End If
Return procs _
.Where(Function(p) p.MainWindowHandle <> IntPtr.Zero) _
.Select(Function(s) GetUrlControl(s)) _
.Where(Function(p) p IsNot Nothing) _
.Select(Function(s) GetValuePattern(s)) _
.Where(Function(p) p.Item2.Length > 0) _
.Select(Function(s) GetValuePatternUrl(s)) _
.FirstOrDefault
End Function
Private Function GetUrlControl(
proses As Process) _
As AutomationElement
Dim propCondition =
New PropertyCondition(
AutomationElement.NameProperty,
AddressCtl)
Return AutomationElement _
.FromHandle(proses.MainWindowHandle) _
.FindFirst(
TreeScope.Descendants,
propCondition)
End Function
Private Function GetValuePatternUrl(
element As Tuple(Of
AutomationElement, AutomationPattern())) As [String]
Dim ap = element.Item2(0)
Dim ovp = element.Item1.GetCurrentPattern(ap)
Dim vp = CType(ovp, ValuePattern)
Return vp.Current.Value
End Function
Private Function GetValuePattern(
element As AutomationElement) _
作为元组(的 自动化元素, 自动化模式())
Return New Tuple(Of
AutomationElement,
AutomationPattern())(
element,
element.GetSupportedPatterns())
End Function
【问题讨论】:
-
这是浏览器插件吗?
-
不适用于 VB.Net 应用程序
-
如果它是一个 Web 应用程序,而不是内置在浏览器中。或者浏览器扩展,那么出于安全原因,您将无法访问用户的浏览会话。因此,获取有关用户打开了哪些选项卡或哪些选项卡处于活动状态的信息将是禁止的
标签: vb.net google-chrome url ui-automation freeze