0

我正在尝试将消息从内容发送到我的 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 参数中省略回调时(因此只传递一条消息),侦听器按预期启动。

我只是不知道如何设置回调函数来工作。任何人?

4

1 回答 1

0

当我将这两个函数添加到窗口状态示例 Chrome 30.0.1599.22 时,问题中的代码对我有用。

于 2013-09-09T15:59:46.710 回答