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

如何获取Google Chrome中的用户操作?已尝试C#/.Net未成功

捕获Google Chrome用户操作并响应的可行方案

下面是几种经过验证的实现方案,覆盖不同场景需求:

1. Chrome扩展(官方推荐,稳定性最高)

Chrome本身提供了完善的扩展API,可以直接监听浏览器级别的操作,这是最可靠的方案。

核心API示例:

  • 监听新建标签页:使用tabs API监听标签创建事件

    // background.js
    chrome.tabs.onCreated.addListener((tab) => {
      // 捕获到新建标签页操作,执行响应逻辑
      console.log('新建标签页:', tab.url);
      // 示例:发送桌面通知
      chrome.notifications.create({
        type: 'basic',
        iconUrl: 'icon.png',
        title: '操作捕获',
        message: '用户新建了标签页'
      });
    });
    
  • 监听保存操作:通过commands API绑定浏览器保存快捷键,或监听下载事件(保存页面/文件会触发下载)

    // manifest.json 中声明快捷键配置
    {
      "name": "操作捕获扩展",
      "version": "1.0",
      "manifest_version": 3,
      "background": { "service_worker": "background.js" },
      "commands": {
        "save-page": {
          "suggested_key": { "default": "Ctrl+S" },
          "description": "监听保存页面操作"
        }
      }
    }
    
    // background.js 中监听快捷键触发事件
    chrome.commands.onCommand.addListener((command) => {
      if (command === 'save-page') {
        console.log('用户执行了保存操作');
        // 执行自定义响应逻辑
      }
    });
    

如果需要和外部C#程序交互,可使用Native Messaging机制:扩展捕获操作后,通过chrome.runtime.sendNativeMessage将消息发送给本地C#程序,实现跨进程通信。

2. Windows平台:UI自动化(UIAutomation)+ C#

针对你之前用C#/.Net的尝试,可以用Windows UI自动化框架识别Chrome的UI元素,监听控件事件。

C#代码示例:

using System;
using System.Windows.Automation;

class ChromeOperationMonitor
{
    static void Main()
    {
        // 查找Chrome主窗口
        var chromeCondition = new PropertyCondition(AutomationElement.NameProperty, "Google Chrome", PropertyConditionFlags.IgnoreCase);
        var chromeWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, chromeCondition);

        if (chromeWindow != null)
        {
            // 监听控件点击事件(比如新建标签页按钮)
            Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent,
                chromeWindow, TreeScope.Descendants,
                (sender, e) =>
                {
                    var element = sender as AutomationElement;
                    if (element?.Current.Name == "新建标签页")
                    {
                        Console.WriteLine("用户点击了新建标签页按钮");
                        // 执行自定义响应逻辑
                    }
                });
        }

        Console.ReadLine();
    }
}

注意:这种方法依赖Chrome的UI结构,版本更新可能导致失效,且需要程序以足够权限运行,可能被安全软件拦截。

3. Chrome DevTools协议(CDP)+ C#

通过Chrome的远程调试协议,连接并监听浏览器的底层操作,适合自动化场景。

步骤与代码示例:

  1. 启动Chrome时添加参数:chrome.exe --remote-debugging-port=9222
  2. 使用C#的CDP库(如ChromeDevTools NuGet包)连接调试端口并监听事件:
using ChromeDevTools;
using ChromeDevTools.Target;
using ChromeDevTools.Page;
using System.Threading.Tasks;

class CDPMonitor
{
    static async Task Main()
    {
        var client = await ChromeClient.CreateAsync("http://localhost:9222");
        
        // 监听新标签创建事件
        client.Target.TargetCreated += (sender, e) =>
        {
            Console.WriteLine($"新建标签页:{e.TargetInfo.Url}");
        };

        // 监听页面保存操作(通过下载事件触发)
        client.Page.DownloadWillBegin += (sender, e) =>
        {
            Console.WriteLine($"用户触发保存操作:{e.Guid}");
        };

        await Task.Delay(-1);
    }
}

优缺点:功能强大,能获取几乎所有浏览器操作,但需要用户开启远程调试,不适合普通用户场景。


内容的提问来源于stack exchange,提问作者Yam Ben Zvi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 13:05:30