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

Javascript 如何将消息从后台发送到内容脚本?,javascript,jquery,google-chrome-extension,Javascript,Jquery,Google Chrome Extension,我正在将消息从后台发送到内容脚本。 My background.js chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { switch(request.type){ case "login-check": checkLogin(); break; } }); function checkLogin() { // var test = localSto

我正在将消息从后台发送到内容脚本。 My background.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);
  }); 
 });
}
My content-script.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.type == "login")
      sendResponse({farewell: "goodbye"});
  });
它向我显示了(未知)的事件处理程序中的一个错误“error”:TypeError:无法读取未定义的属性'Affore' ". 我几乎试过所有的方法,但都不管用,请帮忙。
提前感谢

我猜您调试时的活动窗口是后台页面的开发工具。无论如何,请尝试在弹出窗口中使用chrome.tabs.query,并将选项卡id与登录检查一起传递。登录检查从何而来?相同的内容脚本?这可能会回答您的问题: