如何从后台发送消息到内容脚本?

hu7*_*7sy 5 javascript jquery google-chrome-extension

我正在从后台向内容脚本发送消息。我的背景.js

chrome.runtime.onMessage.addListener(
 function(request, sender, sendResponse) {
  switch(request.type){
    case "login-check":
     checkLogin();
    break;
  }
});

function checkLogin() {
 // var test = localStorage.getItem("test");
 // alert(test);
 chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ 
  chrome.tabs.sendMessage(tabs[0].id, {type: "login"}, function(response) {
   console.log(response.farewell);
   //alert(response.farewell);
  }); 
 });
}
Run Code Online (Sandbox Code Playgroud)

我的 content-script.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.type == "login")
      sendResponse({farewell: "goodbye"});
  });
Run Code Online (Sandbox Code Playgroud)

它向我显示了一个错误“(未知)的事件处理程序中的错误:TypeError:无法读取未定义的属性'告别'”。我几乎尝试了所有方法,但没有奏效,请帮忙。提前致谢