检查用户是否安装了 Chrome 扩展

问题描述:

我正在构建 Chrome 扩展程序,为了让整个过程按我希望的方式工作,我需要一个外部 JavaScript 脚本来检测用户是否安装了我的扩展程序.

I am in the process of building a Chrome extension, and for the whole thing to work the way I would like it to, I need an external JavaScript script to be able to detect if a user has my extension installed.

例如:用户安装了我的插件,然后访问带有我的脚本的网站.该网站检测到我的扩展程序已安装并相应地更新页面.

For example: A user installs my plugin, then goes to a website with my script on it. The website detects that my extension is installed and updates the page accordingly.

这可能吗?

我确信有一种直接的方法(直接调用扩展上的函数,或者使用扩展的 JS 类),但有一种间接的方法(直到某些更好的出现):

I am sure there is a direct way (calling functions on your extension directly, or by using the JS classes for extensions), but an indirect method (until something better comes along):

让您的 Chrome 扩展程序在您的页面上查找具有非常具体 ID 的特定 DIV 或其他元素.

Have your Chrome extension look for a specific DIV or other element on your page, with a very specific ID.

例如:

<div id="ExtensionCheck_JamesEggersAwesomeExtension"></div>

执行 getElementById 并将 innerHTML 设置为您的扩展程序的版本号或其他内容.然后您可以读取该客户端的内容.

Do a getElementById and set the innerHTML to the version number of your extension or something. You can then read the contents of that client-side.

同样,如果有可用的方法,您应该使用直接方法.

Again though, you should use a direct method if there is one available.

找到直接方法!!

使用此处找到的连接方法:https://developer.chrome.com/extensions/extension#global-events

Use the connection methods found here: https://developer.chrome.com/extensions/extension#global-events

未经测试,但您应该能够做到...

Untested, but you should be able to do...

var myPort=chrome.extension.connect('yourextensionid_qwerqweroijwefoijwef', some_object_to_send_on_connect);