I am finding the 'Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received' error in the background.js. My chrome extension works but the dev tools displays a lot of these errors on background.js
I recently migrated my chrome extension from Manifest V2 to Manifest V3 and since then I am finding the error.
This is the place from where the error seems to be arising :
const browser = chrome || browser;
// Listen for messages from content script
browser.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
var frameId = sender.frameId || 0;
if (request.type == "connect") {
connect(sender.tab, frameId);
sendResponse({ success: true });
} else if (request.type == "disconnect") {
disconnect(sender.tab, frameId);
sendResponse({ success: true });
} else if (request.type == "send") {
sendNativeMessage(request.data, sender.tab);
document.addEventListener('nativeMessage',
function handleNativeMessage(e) {
sendResponse(e.detail);
document.removeEventListener(e.type, handleNativeMessage);
}, false);
sendResponse({});
return true;
}
});
The sendresponse is processed in ContentScript :
const browser = chrome || browser;
self.addEventListener("message", function(event){
var request = event.data;
if(request != null && request.type == "SendMessage")
{
ProcessNativeMessage(request.data);
}
});
function ProcessNativeMessage(nativeMessageData) {
var request = new Object();
request.data = nativeMessageData;
browser.runtime.sendMessage(request,handleExtensionResponse);
}
function handleExtensionResponse(value)
{
//alert(value);
};
I played around by returning true, returning false, returning undefined, sending empty response and even disabling the other extensions but none of these changes worked.
I even tried changing the code with async/await as suggested here but it makes no difference.