I know that this question has already been answered, but none of the previous answers helped me. I am recently in JS and still do not understand the intricacies of all things.
When I try to send a message from the pop-up to the script content tab and get a response to the message, I get an error (below). Without responding to the message, everything works fine.
Spent a lot of time reading the chrome extensions docs and digging through the forums, but everything I don't try returns me to the error:
Unchecked runtime.lastError: The message port closed before a response was received.
I tried to return true and promises, but still marking time. Popup code and content script are below.
// popup side
let params = {
active: true,
// lastFocusedWindow: true
};
chrome.tabs.query(params, init);
function init(tabs) {
htmlStates.url = tabs[0].url;
chrome.tabs.sendMessage(tabs[0].id, htmlStates, (response) => {
console.log(response);
})
}
// content script side
function messageCheck(message, sender, sendResponse) {
// return true;
// return Promise.resolve({response: 'received'});
sendResponse({response: 'some response'})
}