Javascript 在';background.js';至';contentScript.js';

Javascript 在';background.js';至';contentScript.js';,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,我尝试应用以下流程: 为background.js中的键绑定,按下时: 从background.js->contentScript.js 从contentScript.js->background.js发送响应 以下是menifit.json定义: "background" : { "scripts" : ["background.js"], "persistent" : true }, "content_scripts": [ { "mat

我尝试应用以下流程:

background.js
中的键绑定,按下时: 从
background.js->contentScript.js
contentScript.js->background.js发送响应

以下是
menifit.json
定义:

  "background" : {

    "scripts" : ["background.js"],
    "persistent" : true
    },
  "content_scripts": [
    {
      "matches": ["*://*/*"],      
      "js": ["contentScript.js"]
    }
    ],
装订部分工作正常

下面是
background.js
中的代码:

    chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
        console.log(response.farewell);
        });
    });
chrome.runtime.onMessage.addListener(HandleMessage);

function HandleMessage(request, sender, sendResponse)
{
    if (request.greeting == "hello")
    {
        sendResponse({farewell: "goodbye"});        
    }
};
以下是
contentScript.js
中的代码:

    chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
        console.log(response.farewell);
        });
    });
chrome.runtime.onMessage.addListener(HandleMessage);

function HandleMessage(request, sender, sendResponse)
{
    if (request.greeting == "hello")
    {
        sendResponse({farewell: "goodbye"});        
    }
};
这是我得到的错误:

Error in event handler for (unknown): Cannot read property 'farewell' of undefined
Stack trace: TypeError: Cannot read property 'farewell' of undefined
    at chrome-extension://fejkdlpdejnjkmaeadiclinbijnjoeei/background.js:64:26
    at disconnectListener (extensions::messaging:338:9)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at EventImpl.dispatchToListener (extensions::event_bindings:397:22)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:93:26)
    at EventImpl.dispatch_ (extensions::event_bindings:379:35)
    at EventImpl.dispatch (extensions::event_bindings:403:17)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.publicClass.(anonymous function) [as dispatch] (extensions::utils:93:26) 
如有变动

console.log(response.farewell);

它工作得很好,但这样我就无法从
contentScript.js


我应该更改什么?

在Chrome Extension中有3种发送消息的方式,您可以参考

通常使用一次性请求,例如:

在background.js中

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {  
  chrome.tabs.sendMessage(tabs[0].id, {message: "hello"}, function(response) {
    console.log(response);
  }); 
});
在contentscripts.js中

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  console.log(request.message);
  if (request.message == "hello") {
    sendResponse({farewell: "goodbye"});
  }
});

您正在尝试将消息从后台页面发送到内容脚本。从中,您将使用以下代码段从后台页面发送消息:

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
 console.log(response.farewell);
});
但是,文档清楚地提到,上面提到的代码是从内容脚本发送消息。这就是您可能会出现错误的原因:

Error in event handler for (unknown): Cannot read property 'farewell' of undefined
Stack trace: TypeError: Cannot read property 'farewell' of undefined
您想要实现的是从扩展(后台页面)向内容脚本发送请求,该内容脚本要求您指定内容脚本所在的选项卡:

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

我希望这能澄清问题,让您从正确的方向开始。

在这样做的同时,我在响应部分对控制台日志进行了“未定义”。@USer22999299您必须在contentscript中发送响应使用侦听器函数的第三个参数,您可以尝试使用上面的演示,尽管正确,这个答案不能解释什么是错的。因此,这并不是真正有用的。与接受的答案进行比较。相同的事情,得到相同的错误。您是否可以指定“这要求您指定内容脚本所在的选项卡”是什么意思?contentScript.js仅在我的电脑上,不是任何网页的一部分。内容脚本在打开选项卡的网页上运行。这只是指定您要将消息发送到哪个选项卡。好,现在可以了。问题是网页已经打开,必须先关闭,然后再重新打开..问题应该编辑为声明它是关于Chrome扩展的,而不是Chrome应用程序。我已经删除了google chrome应用程序标签。