Javascript 在应用程序中收听chrome扩展安装
从网络商店安装chrome扩展时,我如何收听/跟踪? 我以前进行过扩展的内联安装,但由于内联安装即将结束,我希望用户操作打开web应用商店来安装扩展,并侦听他们何时添加扩展以进行UI更改,并基于此进行操作 我尝试了中的消息传递方法,但似乎不起作用 manifest.json看起来像: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
"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()
}
}
信用
"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()
}
}