Javascript backgroundjs和contentScriptjs之间的通信错误

Javascript backgroundjs和contentScriptjs之间的通信错误,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我用下面的代码得到了这个错误: Could not establish connection. Receiving end does not exist. my background.js chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {

我用下面的代码得到了这个错误:

Could not establish connection. Receiving end does not exist. 
my background.js

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
    if(chrome.runtime.lastError) {
      console.error(chrome.runtime.lastError.message);
      return;
    }
    console.log(response.farewell);
    console.log('ytr');
  });
});
还有我的contentScript.js

chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
  alert(request.greeting);
});
我的manifest.json的一部分

  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "https://*/*",
    "http://*/*",
    "activeTab",
    "storage",
    "tabs"
  ],
  "content_scripts": [
    {
      "matches": ["https://*/*", "http://*/*"],
      "js": ["jquery.js","contentScript.js"],
      "css": ["style.css"]
    }
  ]

我想知道我的情况有什么问题。

在devtools窗口处于活动状态时,您可能正在运行此代码。我可以通过从后台页面控制台运行该代码来重现问题,但在常规选项卡处于活动状态时则不行。尝试运行console.logURL=+tabs[0]。url以查看活动选项卡是否是您真正期望的选项卡。

我不明白您的意思,在何处运行?我的意思是,如果开发人员控制台打开,问题将发生。例如,如果您转到Chrome设置,选择“更多工具”,然后选择“开发人员控制台”,则会打开网页的开发人员控制台。或者,您可以通过转到查看扩展的开发人员控制台chrome://extensions,选中开发者模式框,然后单击background.js链接打开扩展的开发者控制台。如果这些是活动窗口,sendMessage将显示您看到的错误。尝试使用我建议的console.log命令查看消息发送到的URL