[英]Listen to chrome extension installation in app
如何从网上商店安装Chrome扩展程序时收听/跟踪? 我以前有一个内联安装的扩展,但内联安装很快就会结束,我希望用户操作打开网上商店来安装扩展,并在他们为UI更改添加扩展时监听,并根据它进行操作。
我尝试了在这里找到的消息传递方法,但它似乎无法正常工作。
manifest.json看起来像:
"background": {
"scripts":["index.js"],
"persistent": false
},
"permissions": ["desktopCapture"],
"externally_connectable": {
"matches": [
"*://localhost:*/*",
"https://*.stageten.tv/*"
]
}
和index.js :
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (request === screenShareExtensionId) {
if (request.message) {
if (request.message == "version") {
sendResponse({version: 1.0})
alert('Hiiii')
}
}
}
return true;
})
在我的应用内:
chrome.runtime.sendMessage(screenShareExtensionId, { message: "version" },
function (reply) {
if (reply) {
if (reply.version) {
return true;
}
}
else {
return false;
}
})
并且基于我的redux逻辑中的值,UI是否更改/等待扩展安装。
您可以在后台页面的开头执行此操作。
您需要将标志(例如,它可以是扩展的版本)保存到localStorage 。
之后,在后台页面的每次启动时,您需要检查此标志是否在您的存储中。 如果没有标志 - 那么你需要跟踪安装,否则,它只是通常重新加载后台页面。
同样的方法可以用来跟踪商店扩展的更新,只需要比较版本。
在这个自我回答的问题中解决了这个问题 ,由于没有接受/投票,我无法将其标记为副本。
background.js
onInstalled
事件。 postMessage
将通知安装成功。 chrome.runtime.onInstalled.addListener(function listener(details) {
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
chrome.tabs.query({
url: [
'https://localhost:3000/*',
'https://staging.foo.com/*',
'https://production.foo.com/*'
]
}, tabs => {
Array.from(tabs).forEach(tab => {
chrome.tabs.executeScript(tab.id, {
code: `window.postMessage('screenshare-ext-installed', window.origin);`
});
});
});
chrome.runtime.onInstalled.removeListener(listener);
}
});
manifest.json
只需确保externally_connectable
和permissions
声明要通知的站点的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()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.