0

我从弹出窗口向后台进程发送消息。反馈信号对我来说不是必需的。我可以不打电话给sendResponse吗?

popup.js

window.onload = function(){
    chrome.extension.sendMessage('Hi, i opened');
}

背景.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    console.log(request);
    console.log(sendResponse);
    // sendResponse not called
})

在 sendmessage responseCallback 可选函数developer.chrome.com/extensions/runtime.html#method-sendMessage的文档中

如果我不传输responseCallback,就在那里sendResponse。上面的代码打印出来。

function (response) {
    if (port) {
        port.postMessage(response);
        port.destroy_();
        port = null;
      } else {
        // We nulled out port when sending the response, and now the page
        // is trying to send another response for the same request.
        handleSendRequestError(isSendMessage, responseCallbackPreserved,
                               sourceExtensionId, targetExtensionId);
      }
    }

在端口的功能被删除。不调用 sendResponse 肯定是内存泄漏

4

1 回答 1

0

据我了解,您想向 background.js 发送消息,并且不想收到回复。如果我是正确的,那么您只需要发送一条消息并忘记"sendResponse"。我来给你展示。

内容,js

window.onload = function(){
   chrome.extension.sendMessage('Hi, i opened');
}

background.js中

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
  console.log(request);
  //console.log(sendResponse); // **this line is not needed**
  // just do what ever you want to do with the "request", it has the message
  // content.js sent.
});
于 2013-05-26T08:22:49.123 回答