代码之家  ›  专栏  ›  技术社区  ›  nicholaswmin

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

  •  0
  • nicholaswmin  · 技术社区  · 6 年前

    Chrome Web Store 而不是内联安装?

    上下文

    Chrome has deprecated inline installation 行不通 从现在开始:

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

    从现在开始 只能通过网络商店安装。

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Community CDub    5 年前

    background script content script ):

    background.js

    • 倾听 onInstalled 事件。
    • 查询与要通知的URL匹配的所有打开的选项卡。
    • 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/*"
    ],
    

    网页

    就在某个地方听音乐 消息激发者

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

    信用