How to implement “return true;”? Error: “The message port closed before a response was received.”

倾然丶 夕夏残阳落幕 提交于 2021-01-28 04:50:52

问题


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;
    }
});

回答1:


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;
});



回答2:


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



来源:https://stackoverflow.com/questions/54736574/how-to-implement-return-true-error-the-message-port-closed-before-a-respon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!