当通过Chrome Web Store而不是内联安装完成安装时,如何通知打开的选项卡是否已安装Chrome扩展程序?
自2018年6月以及之后的Chrome has deprecated inline installation因此,如果安装了扩展程序,通知以下机制将从现在起不起作用:
chrome.webstore.install(url, successCallback, failureCallback)
从现在开始,必须仅通过Web Store安装扩展。
我们已经构建了一个屏幕共享扩展,允许用户分享他的屏幕。
当我们的用户点击“共享屏幕”时,我们打算将它们重定向到网上应用店内的Chrome扩展程序,并在安装扩展程序后立即重新触发共享屏幕功能。
这是我如何从background script解决它(没有使用content script):
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
只需听听扩展成功安装时触发的"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()
}
}