最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to implement "return true;"? Error: "The message port closed before a response was r

programmeradmin3浏览0评论

How can I implement that my event-handler returns true? (Tried everything but the error returns)

I'm getting following error:

"Unchecked runtime.lastError: The message port closed before a response was received."

Solution is:

"Note: The sendResponse callback is only valid if used synchronously, or if the event handler returns true to indicate that it will respond asynchronously. The sendMessage function's callback will be invoked automatically if no handlers return true or if the sendResponse callback is garbage-collected."

edit: See also (chrome):

Here's my code, I would be very thankful:

// receive message from pop-up or options
chrome.extension.onMessage.addListener(function (aRequest, aSender, 
aSendResponse) {
    if (!aSender) {
        return;
    }
    switch (aRequest.cmd) {
        // reload lists
    case 'reload':
        XX.blockedDomains = {};
        XX.load();
        break;
        // send list of recently blocked
    case 'blocked':
        aSendResponse(Object.keys(XX.blockedDomains));
        break;
        // deny domain
    case 'deny':
        XX.blocklist[aRequest.domain] = 1;
        delete XX.blockedDomains[aRequest.domain];
        XX.save();
        break;
    }
});

How can I implement that my event-handler returns true? (Tried everything but the error returns)

I'm getting following error:

"Unchecked runtime.lastError: The message port closed before a response was received."

Solution is:

"Note: The sendResponse callback is only valid if used synchronously, or if the event handler returns true to indicate that it will respond asynchronously. The sendMessage function's callback will be invoked automatically if no handlers return true or if the sendResponse callback is garbage-collected."

https://developer.chrome.com/extensions/messaging#simple

edit: See also (chrome): https://github.com/mozilla/webextension-polyfill/issues/130

Here's my code, I would be very thankful:

// receive message from pop-up or options
chrome.extension.onMessage.addListener(function (aRequest, aSender, 
aSendResponse) {
    if (!aSender) {
        return;
    }
    switch (aRequest.cmd) {
        // reload lists
    case 'reload':
        XX.blockedDomains = {};
        XX.load();
        break;
        // send list of recently blocked
    case 'blocked':
        aSendResponse(Object.keys(XX.blockedDomains));
        break;
        // deny domain
    case 'deny':
        XX.blocklist[aRequest.domain] = 1;
        delete XX.blockedDomains[aRequest.domain];
        XX.save();
        break;
    }
});
Share Improve this question edited Feb 21, 2019 at 20:02 AEOS7 asked Feb 17, 2019 at 18:56 AEOS7AEOS7 3511 gold badge2 silver badges8 bronze badges 3
  • 1 The posted code doesn't invoke asynchronous functions so there's no need to add return true here. The problem must be something else, not in the posted code, but rather in the code that sends the message. It's also not clear why you've linked the issue in WebExtension polyfill, which you aren't using here. – woxxom Commented Feb 17, 2019 at 19:34
  • Thanks, I'm checking all other code. Thought the one posted above is the problem and so (logically) nothing helped. – AEOS7 Commented Feb 17, 2019 at 20:40
  • Please see support.google.com/chrome/thread/2047906?msgid=2556826 if this error occurring in Chrome 73 – Jonathan Lin Commented Mar 17, 2019 at 13:45
Add a comment  | 

2 Answers 2

Reset to default 14

Seems fixed now. I added return true; on the second last line and now there are no more error entries in the log.

Is this solution ok? Would be glad for feedback if something is wrong with it. Otherwise, I will mark this thread in a few days as solved.

// receive message from pop-up or options
chrome.extension.onMessage.addListener(function (aRequest, aSender, 
aSendResponse) {
    if (!aSender) {
        return;
    }
    switch (aRequest.cmd) {
        // reload lists
    case 'reload':
        XX.blockedDomains = {};
        XX.load();
        break;
        // send list of recently blocked
    case 'blocked':
        aSendResponse(Object.keys(XX.blockedDomains));
        break;
        // deny domain
    case 'deny':
        XX.blocklist[aRequest.domain] = 1;
        delete XX.blockedDomains[aRequest.domain];
        XX.save();
        break;
    }
    return true;
});

return true can resolve your problem just because:

you always need to sendresponse, you can sendresponse to any value, but you must sendresponse.

In your code, there are many situations, than no sendresponse.

This sendresponse function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).

ref: https://developer.chrome.com/extensions/runtime#event-onMessage

发布评论

评论列表(0)

  1. 暂无评论