簡體   English   中英

在應用程序中收聽chrome擴展程序安裝

[英]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事件。
  • 查詢與您要通知的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_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/*"
],

網頁

只需在某處查看擴展在成功安裝時觸發的postMessage消息。

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

積分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM