You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET WinForms应用:Chrome打开新标签时触发应用内函数方案咨询

实现VB.NET WinForms监听Chrome新标签页并调用内部函数的可行思路

以下是几种实用的实现方案,你可以根据需求选择:

1. Chrome扩展 + Native Messaging(推荐)

这是最可靠且官方支持的方式,精准监听新标签页事件,无系统级钩子的性能开销:

  • 步骤1:开发Chrome扩展
    在扩展的background.js中监听标签创建事件,判断是否为新标签页并发送消息给WinForms应用:

    chrome.tabs.onCreated.addListener((tab) => {
      if (tab.url === 'chrome://newtab/') {
        // 发送消息给Native应用
        chrome.runtime.sendNativeMessage('com.yourcompany.yourapp', {action: 'newTabOpened'}, (response) => {
          if (chrome.runtime.lastError) {
            console.log(chrome.runtime.lastError.message);
          }
        });
      }
    });
    

    扩展的manifest.json需要声明tabs权限和Native Messaging配置:

    {
      "manifest_version": 3,
      "name": "New Tab Listener",
      "version": "1.0",
      "permissions": ["tabs"],
      "background": {
        "service_worker": "background.js"
      },
      "nativeMessaging": {
        "hosts": ["com.yourcompany.yourapp"]
      }
    }
    
  • 步骤2:配置Native Messaging主机
    在Windows系统中创建注册表项(路径:HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.yourcompany.yourapp),默认值指向你的主机配置文件路径。主机配置文件(com.yourcompany.yourapp.json)内容示例:

    {
      "name": "com.yourcompany.yourapp",
      "description": "New Tab Listener Host",
      "path": "C:\\Path\\To\\YourWinFormsApp.exe",
      "type": "stdio",
      "allowed_origins": ["chrome-extension://你的扩展ID/"]
    }
    
  • 步骤3:WinForms应用接收消息
    在VB.NET中通过标准输入输出接收扩展发来的JSON消息,触发内部函数:

    Imports System.IO
    Imports System.Text.Json
    
    Public Class MainForm
        Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' 在后台线程监听标准输入
            Task.Run(AddressOf ListenForNativeMessages)
        End Sub
    
        Private Sub ListenForNativeMessages()
            Dim reader As New StreamReader(Console.OpenStandardInput())
            While True
                Dim lengthBytes(3) As Byte
                reader.BaseStream.Read(lengthBytes, 0, 4)
                Dim length As Integer = BitConverter.ToInt32(lengthBytes, 0)
                Dim buffer(length - 1) As Char
                reader.Read(buffer, 0, length)
                Dim message As String = New String(buffer)
                
                ' 解析消息
                Dim msg = JsonSerializer.Deserialize(Of Dictionary(Of String, String))(message)
                If msg.ContainsKey("action") AndAlso msg("action") = "newTabOpened" Then
                    ' 调用你的内部函数
                    Me.Invoke(Sub() OnChromeNewTabOpened())
                End If
            End While
        End Sub
    
        Private Sub OnChromeNewTabOpened()
            ' 这里写你的业务逻辑
            MessageBox.Show("Chrome新标签页已打开!")
        End Sub
    End Class
    

2. Windows API全局钩子监听窗口创建

通过CBT钩子监听系统中所有窗口的创建事件,筛选Chrome的新标签页窗口:

  • VB.NET钩子实现示例:
    Imports System.Runtime.InteropServices
    
    Public Class ChromeTabHook
        Private Delegate Function HookProc(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function SetWindowsHookEx(idHook As Integer, lpfn As HookProc, hMod As IntPtr, dwThreadId As UInteger) As IntPtr
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function CallNextHookEx(hhk As IntPtr, nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function UnhookWindowsHookEx(hhk As IntPtr) As Boolean
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function GetClassName(hWnd As IntPtr, lpClassName As StringBuilder, nMaxCount As Integer) As Integer
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function GetWindowThreadProcessId(hWnd As IntPtr, ByRef lpdwProcessId As Integer) As UInteger
        End Function
    
        Private Const WH_CBT As Integer = 5
        Private Const HCBT_CREATEWND As Integer = 3
        Private hookHandle As IntPtr = IntPtr.Zero
        Private hookProc As HookProc
    
        Public Sub StartHook()
            hookProc = New HookProc(AddressOf CbtHookProc)
            hookHandle = SetWindowsHookEx(WH_CBT, hookProc, IntPtr.Zero, 0)
        End Sub
    
        Public Sub StopHook()
            If hookHandle <> IntPtr.Zero Then
                UnhookWindowsHookEx(hookHandle)
                hookHandle = IntPtr.Zero
            End If
        End Sub
    
        Private Function CbtHookProc(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
            If nCode = HCBT_CREATEWND Then
                Dim processId As Integer = 0
                GetWindowThreadProcessId(wParam, processId)
                Dim process = System.Diagnostics.Process.GetProcessById(processId)
                If process.ProcessName.Equals("chrome", StringComparison.OrdinalIgnoreCase) Then
                    Dim className As New StringBuilder(256)
                    GetClassName(wParam, className, className.Capacity)
                    If className.ToString().Equals("Chrome_WidgetWin_1", StringComparison.OrdinalIgnoreCase) Then
                        Dim windowText As New StringBuilder(256)
                        GetWindowText(wParam, windowText, windowText.Capacity)
                        ' 判断是否为新标签页(根据标题或后续获取URL)
                        If windowText.ToString().Contains("新标签页") Then
                            ' 调用你的内部函数
                            Form1.Invoke(Sub() Form1.OnChromeNewTabOpened())
                        End If
                    End If
                End If
            End If
            Return CallNextHookEx(hookHandle, nCode, wParam, lParam)
        End Function
    End Class
    
  • 注意:需要以管理员权限运行应用,钩子会影响系统性能,退出时必须释放钩子。

3. 轮询Chrome历史记录数据库

Chrome的历史记录存储在SQLite数据库中,可定时轮询检测新的chrome://newtab/记录:

  • VB.NET代码示例:
    Imports System.Data.SQLite
    Imports System.IO
    
    Public Class HistoryMonitor
        Private historyPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Google\Chrome\User Data\Default\History")
        Private lastCheckedTime As DateTime = DateTime.Now
    
        Public Sub StartMonitoring()
            Timer1.Interval = 5000 ' 每5秒轮询一次
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim tempPath As String = Path.Combine(Path.GetTempPath(), "ChromeHistoryTemp.db")
            Try
                ' 复制数据库(Chrome运行时会锁定原文件)
                File.Copy(historyPath, tempPath, True)
                Using conn As New SQLiteConnection($"Data Source={tempPath};Version=3;")
                    conn.Open()
                    Dim cmd As New SQLiteCommand("SELECT last_visit_time FROM urls WHERE url = 'chrome://newtab/' ORDER BY last_visit_time DESC LIMIT 1", conn)
                    Dim result = cmd.ExecuteScalar()
                    If result IsNot Nothing Then
                        ' Chrome的时间是微秒级时间戳,转换为DateTime
                        Dim visitTime As DateTime = New DateTime(1601, 1, 1).AddTicks(CType(result, Long) \ 10)
                        If visitTime > lastCheckedTime Then
                            lastCheckedTime = visitTime
                            ' 调用内部函数
                            Form1.Invoke(Sub() Form1.OnChromeNewTabOpened())
                        End If
                    End If
                End Using
            Catch ex As Exception
                ' 处理异常(比如Chrome未运行时数据库不存在)
            Finally
                If File.Exists(tempPath) Then
                    File.Delete(tempPath)
                End If
            End Try
        End Sub
    End Class
    
  • 注意:Chrome的历史记录格式可能随版本变化,且复制数据库时可能遇到权限问题。

内容的提问来源于stack exchange,提问作者user2777546

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 03:50:23