Google chrome extension Can';t从后台页面向Chrome扩展的内容脚本发送消息

Google chrome extension Can';t从后台页面向Chrome扩展的内容脚本发送消息,google-chrome-extension,message,content-script,Google Chrome Extension,Message,Content Script,我的背景页上有以下内容: function go(){ chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) { console.log(response.farewell); }); }); } go(); 在我的内容脚本中: chr

我的背景页上有以下内容:

function go(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});

}

go();
在我的内容脚本中:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      alert(request.greeting);
  });
我希望在启用应用程序时启动警报,但什么也没有发生。我错过什么了吗

编辑:manifest.json:

{
  "manifest_version": 2,

  "name": "Naming",
  "description": "any",
  "version": "1.0",

  "permissions": [
    "tabs",
    "activeTab",
    "https://google.com.br/"
  ],

 "background": {"scripts":["background.js"]},

  "content_scripts": [
    {
      "js": ["CLAWS_Content_Script.js", "jquery.js"],
      "css": ["CLAWS_Content_Script_CSS.css"],
      "matches": ["<all_urls>"]

    }
  ],

  "web_accessible_resources": [
        "CLAWS_Sem_Imagens.html",
        "icone_XVermelho.png"
  ]


}
{
“清单版本”:2,
“名称”:“命名”,
“说明”:“任何”,
“版本”:“1.0”,
“权限”:[
“标签”,
“活动标签”,
"https://google.com.br/"
],
“背景”:{“脚本”:[“background.js”]},
“内容脚本”:[
{
“js”:[“CLAWS\u Content\u Script.js”,“jquery.js”],
“css”:[“CLAWS\u Content\u Script\u css.css”],
“匹配项”:[“”]
}
],
“网络可访问资源”:[
“CLAWS\u Sem\u Imagens.html”,
“icone_XVermelho.png”
]
}

内容脚本被加载,因为它提供的其他功能可以完美地工作。控制台上没有错误。

当浏览器启动或启用扩展时,会发送消息,但此时没有活动内容脚本接收消息。类似地,当您重新加载网页时,您的背景页不会重新加载,因此不会发送任何消息,因此不会收到任何消息


如果您真的希望在我启用扩展时启动警报,请通过手动注入内容脚本,在这种情况下,您不需要manifest.json中的
“content\u scripts”
部分。

@wOxxOm抱歉,我不知道如何调试它…但是其他信息。添加:)哦,您知道当您重新启用扩展时,manifest.json中声明的内容脚本不会神奇地重新插入吗?你也应该重新加载网页。@wOxxOm这是一个很好的猜测,但不幸的是没有起作用:)而且,根据我在过去五分钟里对chrome extensions中调试的了解,我可以说,也许内容脚本中的侦听器从未被调用过……而且,哦,我真的能够将一些消息从内容脚本传递到后台页面……试试这个:setInterval(function(){go();},1000);在背景页谢谢!这很有帮助!