Skip to content Skip to sidebar Skip to footer

How To Implement "return True;"? Error: "the Message Port Closed Before A Response Was Received."

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

Solution 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 listscase'reload':
        XX.blockedDomains = {};
        XX.load();
        break;
        // send list of recently blockedcase'blocked':
        aSendResponse(Object.keys(XX.blockedDomains));
        break;
        // deny domaincase'deny':
        XX.blocklist[aRequest.domain] = 1;
        deleteXX.blockedDomains[aRequest.domain];
        XX.save();
        break;
    }
    returntrue;
});

Solution 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

Post a Comment for "How To Implement "return True;"? Error: "the Message Port Closed Before A Response Was Received.""