Google chrome extension background.js未接收chrome.runtime消息

Google chrome extension background.js未接收chrome.runtime消息,google-chrome-extension,Google Chrome Extension,我的Google Chrome扩展中有以下流程: html运行popup.js(工作正常): basicData.js执行一些计算等,然后向后台脚本发送一条消息: ... console.log("Hello world"); //prints fine chrome.runtime.sendMessage({...data}); background.js未收到任何消息: chrome.runtime.onMessage.addListener(function(response,send

我的Google Chrome扩展中有以下流程:

html运行popup.js(工作正常):

basicData.js执行一些计算等,然后向后台脚本发送一条消息:

...
console.log("Hello world"); //prints fine
chrome.runtime.sendMessage({...data}); 
background.js未收到任何消息:

chrome.runtime.onMessage.addListener(function(response,sender,sendResponse) {

  console.log("hiya")

  if (response.type==="Data complete"){
    console.log("Hello2");
    chrome.tabs.sendMessage({...response});
  }

})
以下是我的manifest.json供参考:

{
    "name": "Project X",
    "version": "1.0",
    "description": "Build an Extension!",
    "background": {
        "scripts": ["background.js"],
        "persistent": false
      },
      "content_scripts": [
        {
          "matches": ["<all_urls>","http://*/*", "https://*/*"],
          "js": ["testScript.js"]
        }
      ],
      "browser_action": { 
        "default_popup": "popup.html"
      },
      "content_security_policy": "script-src 'self' https://www.gstatic.com/ https://*.firebaseio.com https://www.googleapis.com; object-src 'self'; connect-src 'self' wss://*.firebaseio.com;",
      "permissions": ["tabs","storage","declarativeContent", "activeTab","http://*/","https://*/","webNavigation"],
    "manifest_version": 2
  }
{
“名称”:“项目X”,
“版本”:“1.0”,
“说明”:“生成扩展!”,
“背景”:{
“脚本”:[“background.js”],
“持续”:假
},
“内容脚本”:[
{
“匹配项”:[“”、“http://*/*”、“https://*/*”],
“js”:[“testScript.js”]
}
],
“浏览器操作”:{
“默认弹出窗口”:“popup.html”
},
“内容安全策略”:“脚本src'self”https://www.gstatic.com/ https://*.firebaseio.comhttps://www.googleapis.com;对象src'self';连接src'self'wss://*.firebaseio.com;“,
“权限”:[“选项卡”、“存储”、“声明内容”、“活动选项卡”、“http://*/”、“https://*/”、“网络导航”],
“清单版本”:2
}
我复制了这段代码和流程的大部分,它来自于我在过去制作的一个Google Chrome扩展,它工作得非常好,所以我不知道为什么basicata.js没有发送消息,或者background.js没有接收消息。欢迎所有建议


感谢阅读。

1),2)chrome.tabs.sendMessage需要一个tab id作为第一个参数,例如
sender.tab.id
,但是通常最好使用
sendResponse
和basicData.js中的回调,请参见。我正在查看popup.js日志!
chrome.runtime.onMessage.addListener(function(response,sender,sendResponse) {

  console.log("hiya")

  if (response.type==="Data complete"){
    console.log("Hello2");
    chrome.tabs.sendMessage({...response});
  }

})
{
    "name": "Project X",
    "version": "1.0",
    "description": "Build an Extension!",
    "background": {
        "scripts": ["background.js"],
        "persistent": false
      },
      "content_scripts": [
        {
          "matches": ["<all_urls>","http://*/*", "https://*/*"],
          "js": ["testScript.js"]
        }
      ],
      "browser_action": { 
        "default_popup": "popup.html"
      },
      "content_security_policy": "script-src 'self' https://www.gstatic.com/ https://*.firebaseio.com https://www.googleapis.com; object-src 'self'; connect-src 'self' wss://*.firebaseio.com;",
      "permissions": ["tabs","storage","declarativeContent", "activeTab","http://*/","https://*/","webNavigation"],
    "manifest_version": 2
  }