我正在尝试将消息从内容发送到我的 Chrome 应用程序中的后台脚本。
// background.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
console.log(message)
sendResponse("hey man!");
});
// content.js
chrome.runtime.sendMessage("hello world!", function(response) {
console.log(response);
});
当我调用sendMessage
指定回调的内容时(如上面的代码中所述),最终出现此错误:
Port: Could not establish connection. Receiving end does not exist.
侦听器未启动,但是,回调是(具有未定义的响应)。当我从 sendMessage 参数中省略回调时(因此只传递一条消息),侦听器按预期启动。
我只是不知道如何设置回调函数来工作。任何人?