Chrome消息传递:“接收端不存在”和“在收到响应之前端口已关闭”

时间:2019-05-29 18:00:55

标签: javascript google-chrome-extension

一切似乎都正常,但是这些错误不断抛出:

1)“未经检查的runtime.lastError:无法建立连接。接收端不存在。”

2)“未经检查的runtime.lastError:消息端口在收到响应之前已关闭。”

当我开始从后台脚本传递内容脚本时,它开始发生。这是我添加到background.js的内容:

  chrome.windows.getAll({populate:true},
    function(windows)
    {
      windows.forEach(function(window)
      {
        window.tabs.forEach(function(tab)
        {
          chrome.tabs.sendMessage(tab.id, {msg: "FINITO!"}, function(){
            console.log("?");
          });
        });
      });
    });

contentscript.js:

chrome.runtime.onMessage.addListener(
  function(response, sender, sendResponse)
  {
    alert("i gotchu");

    //copypasted this from stackoverflow as a fix, I don't really know what this is for and it doesn't seem to work loll
    return Promise.resolve("Dummy response to keep the console quiet");
  });

manifest.json:

{
  "manifest_version": 2,
  "name": "Drill Sergeant",
  "description": "Tracks time spent on 'watchlist' websites",
  "version": "1.0.0",
  "browser_action": {"default_popup": "popup.html"},
  "background": {"scripts": ["background.js"]},
  "content_scripts": [{"matches": ["<all_urls>"], "js": ["contentscript.js"]}],
  "permissions": ["activeTab", "webNavigation", "notifications", "tabs", "background"]
}

错误1)和2)是什么意思?您将如何解决它们?每当代码执行时,我都会收到“ i gotchu”警报,因此似乎消息传递正在工作,但是我不知道为什么这些错误总是随警报一起出现...

编辑:我做了更多的测试,并且警报仅显示在活动选项卡上,所以我认为消息传递实际上无效。

0 个答案:

没有答案