Chrome Extension message passing: Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist

前端 未结 4 951
臣服心动
臣服心动 2020-12-13 00:45

My chrome extension has the following two javascripts:

background.js, running as background script:

chrome.runtime.onMessage.addListene         


        
4条回答
  •  半阙折子戏
    2020-12-13 01:00

    Some security changes in chrome seems like you have to take a slightly different approach when messaging between content scripts and the background script.

    The calls to use are

    1. From background page:

    chrome.runtime.onMessageExternal.addListener Notice the External part.

    1. The manifest.json needs extra permissions
          "externally_connectable": {
            "ids": ["abcdefghijklmnopqrstuvwxyzabcdef"],
            "matches": ["https://example.com/*"],
            "accepts_tls_channel_id": false
          },
    

    "abcdefghijklmnopqrstuvwxyzabcdef" is your extension id.

    "https://example.com/*" is the domain the content script runs on.

    1. From the content script:

      chrome.runtime.sendMessage / chrome.runtime.connect with a extensionId as the first parameter.

    Read more here https://developer.chrome.com/extensions/manifest/externally_connectable

提交回复
热议问题