I'm trying to retrieve data from the localStorage inside background.js and pass it to popup.js, but receiving the below error,
Unchecked runtime.lastError: The message port closed before a response was received.
Note: the below snippet prints the localStorage to the console of background.js
chrome.storage.local.get(["dsr_db"], function (result) { console.log(result); });
popup.html
<button type="button" class="btn btn-primary" id="display-db">
Displaye DB
</button>
popup.js
let display_db = document.getElementById("display-db");
display_db.addEventListener("click", function() {
chrome.runtime.sendMessage({greeting: "display-db"}, function(response) {
alert(response.message);
});
});
backgroun.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.greeting == "display-db") {
chrome.storage.local.get(["dsr_db"], function (result) {
sendResponse({ message: result });
});
}
});
Any workaround which could be made inside background.js to make the connection long enough and pass the localStorage object to popup.js??
Thanks in advance