Javascript 在应用程序中收听chrome扩展安装

Javascript 在应用程序中收听chrome扩展安装,javascript,reactjs,google-chrome-extension,react-redux,Javascript,Reactjs,Google Chrome Extension,React Redux,从网络商店安装chrome扩展时,我如何收听/跟踪? 我以前进行过扩展的内联安装,但由于内联安装即将结束,我希望用户操作打开web应用商店来安装扩展,并侦听他们何时添加扩展以进行UI更改,并基于此进行操作 我尝试了中的消息传递方法,但似乎不起作用 manifest.json看起来像: "background": { "scripts":["index.js"], "persistent": false }, "permissions": ["desktopC

从网络商店安装chrome扩展时,我如何收听/跟踪? 我以前进行过扩展的内联安装,但由于内联安装即将结束,我希望用户操作打开web应用商店来安装扩展,并侦听他们何时添加扩展以进行UI更改,并基于此进行操作

我尝试了中的消息传递方法,但似乎不起作用

manifest.json看起来像:

   "background": {
     "scripts":["index.js"],
     "persistent": false
   },
   "permissions": ["desktopCapture"],
   "externally_connectable": {
        "matches": [
            "*://localhost:*/*",
         "https://*.stageten.tv/*"
        ]
    }
和index.js:

在我的应用程序中:

chrome.runtime.sendMessage(screenShareExtensionId, { message: "version" },
    function (reply) {
        if (reply) {
            if (reply.version) {
              return true;
            }
        }
        else {
          return false;
        }
    })

根据我的redux逻辑中的值,UI要么更改,要么不更改/等待安装扩展。

您可以在开始安装时执行此操作

您需要将标志(例如,它可以是扩展的版本)保存到

之后,在后台页面的每个开头,您都需要检查此标志是否在您的存储中。如果没有标志,则需要跟踪安装,否则,只需重新加载背景页面

同样的方法也可以用于从应用商店跟踪扩展的更新,只需要比较版本

在中解决了这个问题,我不能将其标记为该问题的副本,因为没有接受/支持投票


下面是我如何从(不使用a)解决它的:

background.js
  • 侦听已安装的
    事件
  • 查询与要通知的URL匹配的所有打开的选项卡
  • 在每个选项卡中执行一个小脚本,以通知 那次安装是成功的
manifest.json
只需确保
外部可连接
权限
声明 要通知的站点的URL模式

"externally_connectable": {
    "matches": [
    "https://localhost:3000/*",
    "https://staging.foo.com/*",
    "https://production.foo.com/*"
  ]
},
"permissions": [
  "desktopCapture",
  "https://localhost:3000/*",
  "https://staging.foo.com/*",
  "https://production.foo.com/*"
],
网页 只需在某处收听由发出的
postMessage
消息 成功安装时的扩展

window.onmessage = e => {
  if (e.data === 'screenshare-ext-installed') {
    // extension successfully installed
    startScreenShare()
  }
}
信用

只需通过扩展中的XMLHttpRequest向服务器发送http请求即可?或者枚举打开的选项卡,找到站点中的选项卡,并在其中运行内容脚本,该脚本通过DOM CustomEvent或window.postMessage向站点发布消息。因此,在安装扩展之前,后台页面不会运行,而且我的应用程序中似乎根本没有运行该扩展,因为我在脚本文件中没有看到警报或接收到任何更改。
"externally_connectable": {
    "matches": [
    "https://localhost:3000/*",
    "https://staging.foo.com/*",
    "https://production.foo.com/*"
  ]
},
"permissions": [
  "desktopCapture",
  "https://localhost:3000/*",
  "https://staging.foo.com/*",
  "https://production.foo.com/*"
],
window.onmessage = e => {
  if (e.data === 'screenshare-ext-installed') {
    // extension successfully installed
    startScreenShare()
  }
}