简体   繁体   English

检测何时安装Chrome扩展程序,而不进行内联安装

[英]Detect when Chrome extension was installed, without inline installation

How can an open tab get notified that a Chrome extension has just been installed, when the installation was done through the Chrome Web Store instead of inline-installation? 当通过Chrome网上应用店而不是嵌入式安装完成安装时,如何打开标签会通知您已经安装了Chrome扩展程序?

Context 上下文

Since June 2018 and onwards Chrome has deprecated inline installation hence the following mechanism to get notified if extension was installed won't work from now on: 自2018年6月起, Chrome已弃用内联安装,因此以下通知安装扩展功能的机制从现在开始将不起作用

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

From now on extensions must be installed only via the Web Store. 从现在开始, 必须仅通过Web Store安装扩展。

We've built a screen share extension that allows prompting the user to share his screen. 我们构建了一个屏幕共享扩展程序,该扩展程序可以提示用户共享其屏幕。

When our users hit "Share Screen", we intend to redirect them to the Chrome extension within the Web Store and as soon as they install the extension to re-trigger the Share Screen functionality. 当我们的用户点击“共享屏幕”时,我们打算将他们重定向到网上商店中的Chrome扩展程序,并在他们安装扩展程序后重新触发共享屏幕功能。

Here's how I solved it from the background script (w/o using a content script ): 这是我从后台脚本 (不使用内容脚本 )解决的方法:

background.js

  • Listen for onInstalled event. 监听onInstalled事件。
  • Query all opened tabs that match the URL's you want to notify. 查询与您要通知的URL匹配的所有打开的选项卡。
  • Execute a small script in each tab that will postMessage notifying that installation was succesful. 在每个选项卡中执行一个小的脚本,该脚本将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

Just make sure both externally_connectable and permissions declare the URL patterns of the sites you want to notify. 只要确保externally_connectablepermissions声明了您要通知的站点的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/*"
],

Web page 网页

Just listen somewhere for the postMessage message fired by the extension on succesful installation. 只需在某处侦听成功安装扩展名触发的postMessage消息。

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

Credits 积分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM