我正在尝试从我的 C# 应用程序控制 chrome。
我希望在我的 C# 应用程序和 Chrome 之间建立一个相当简单的 API。
- Navigate to url
- Find HTML element based on some criteria (typically to be done via jQuery)
- Click on the selected element
- Repeat as needed
我的 C# 程序将管理多个执行此类工作的 Chrome 实例。
我尝试实施的解决方案是使用 Chrome 扩展“内容”脚本
这是我当前的 Manifest.json:
{
"name": "ScraperAPI",
"manifest_version": 2,
"version": "0.0.1",
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"js": [ "AutomationApi.js"],
"run_at" : "document_start",
"all_frames" : false
}
],
"permissions": [ "tabs", "http://*/*", "storage" ]
"web_accessible_resources": [ "jQuery.min.v.2.0.3.map" ]
}
内容脚本使用 WebSocket 与我的 C# 应用程序进行通信
到目前为止,WebSocket 非常适用于通信 API 请求(例如“导航”)和响应(例如“文档就绪”)。
我的扩展程序监听准备好的文档并向我的 C# 程序打开一个 WebSocket。- 这行得通
我的问题是:
[1] 如何让内容脚本在 Chrome 启动时自动启动? 看来,直到我在导航栏中手动输入 URL,我的扩展程序才加载
内容脚本如何识别它是第一次在 chrome 实例中运行?
[2] 如何在页面加载时维护内容脚本的一般状态。
特别是,C# 程序使用内容脚本导航到 URL,找到特定的 HTML 元素,单击它,在“文档准备好”之后,它想要继续浏览并进一步单击页面。
不幸的是(对我来说),每次加载页面时,Chrome 都会加载内容脚本的一个新实例。
==> I don't know how to have the script determine whether it is running for the first time or not.
On the first time through it has to open a WebSocket to the C#. On subsequent loads in the same tab
I want it to recognize that the WebSocket connection exists and continue using it.
I tried to create a 'window.myApi' object to save data, but each page seems to get a new window object.
I was going to try to use 'local storage' but that is shared between all local instances of all the scripts
and a fresh instance of the content script does not know whether it is running for first time or not.
By the way, when my content script opens a WebSocket to the C#, the C# responds with a unique id (GUID) so
all further communications use this unique id in the messages.
It would be great if a content script can tell if it has been assigned a unique id by the C# program.
Is there an 'uber' window for the entire Chrome instance that I can latch onto?
我应该使用不同的方法(不使用内容脚本)来自动化 Chrome 会话吗?
有没有一种方法可以保存我错过的状态?
任何帮助将不胜感激。
- 提前非常感谢 [大卫]