Chrome Extension development: keep getting Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist

Chrome Extension development: keep getting Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist

我正在开发一个 chrome 扩展,它需要将消息从扩展后台页面发送到内容脚本,但无论我做什么,我都会收到以下错误: "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist."

后台脚本发送的消息正在被一个 API 获取,该 API 在每次 url 更改时运行。

这是 manifest.json 文件:

{
  "manifest_version": 2,
  "name": "tool",
  "description": "help tool",
  "version": "1.0",
  "icons": {
    "16": "icons/icon16.png",
    "32": "icons/icon32.png",
    "48": "icons/icon48.png",
    "128": "icons/icon128.png"
  },

  "page_action": {
    "default_icon": "icons/icon128.png",
    "default_popup": "src/popup.html",
    "default_title": "tool"
  },

  "background": {
    "scripts": [
                "src/background.js"
                ],
    "persistent": true
  },

  "web_accessible_resources": [
    "src/content.css"
  ],

  "content_scripts": [
    {
    "matches": [
      "https://*"
    ],
    "js": [
      "src/jquery-3.4.1.min.js",
      "src/content.js"
      ],
      "css": [
        "src/content.css"
      ]
    }
  ],

  "permissions": [
    "tabs",
    "Api URL"
  ]
}

这是我在后台和内容脚本中使用的代码:

background.js- 通过 chrome.tabs.onUpdated.addListener 函数在每次 url 更改时调用 callApi:

   chrome.tabs.onUpdated.addListener(() => {
     chrome.tabs.query({active: true, currentWindow: true},function(tabs) {
       var tab = tabs[0];
       var url = tab.url;
       var para = "url=" + url
       console.log(para)
       callApi(para)
     })
  })

    const callApi = (para) =>   {
    var url = "API URL" + para
    fetch(url)
        .then((response) => {
            return response.text();
     })
     .then((data) => {
        console.log(data);
        connectToCurrentTab(data);
     })
   }

    function getCurrentTabId(cb) {
        var query = {active: true, currentWindow: true};
        chrome.tabs.query(query, function(tabArray) {
            cb(tabArray[0].id)
        });
    }

    function connectToCurrentTab (data) {
        getCurrentTabId(function(currentTabId) {
           var port = chrome.tabs.connect(currentTabId, {name: "knock"})

           port.postMessage(data);
           port.onMessage.addListener((msg) => {
               return true;
           });
        });
    }

content.js:

chrome.runtime.onConnect.addListener(port => {
    console.log('connected ', port);
    if (port.name === 'knock') {
       port.onMessage.addListener(function(msg) {
           var data = JSON.parse(msg) 
             port.postMessage({data: "received"});
         });
    }
});

我能够获取内容脚本中的数据,但它也显示错误。 谁能帮我解决这个问题? 非常感谢任何帮助。

问题已解决。我正在使用一个复杂的过程将消息发送到内容脚本。 onUpdate 函数也有错误。 在 url 更改时调用 api 的正确方法是:

chrome.tabs.onUpdated.addListener(
  function(tabId, changeInfo, tab) {
    if (changeInfo.url) {
      para = "url="+changeInfo.url
      callApi(para)
    }
  }
);

谢谢你帮助我。