检测Chrome扩展程序何时安装,无需内联安装

问题描述 投票:0回答:1

当通过Chrome Web Store而不是内联安装完成安装时,如何通知打开的选项卡是否已安装Chrome扩展程序?

上下文

自2018年6月以及之后的Chrome has deprecated inline installation因此,如果安装了扩展程序,通知以下机制将从现在起不起作用:

chrome.webstore.install(url, successCallback, failureCallback)

从现在开始,必须仅通过Web Store安装扩展。

我们已经构建了一个屏幕共享扩展,允许用户分享他的屏幕。

当我们的用户点击“共享屏幕”时,我们打算将它们重定向到网上应用店内的Chrome扩展程序,并在安装扩展程序后立即重新触发共享屏幕功能。

javascript google-chrome-extension chrome-web-store
1个回答
0
投票

这是我如何从background script解决它(没有使用content script):

background.js

  • 听听onInstalled事件。
  • 查询与您要通知的URL匹配的所有已打开选项卡。
  • 在每个选项卡中执行一个小脚本,以便qazxsw poi通知安装成功。
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.jsonexternally_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() } }
© www.soinside.com 2019 - 2024. All rights reserved.