I need to send data from popup.js to background.js and after that to send from background.js to popup.js. But if popup.html closed it can't recive data from background.js because port doesn't exist. How to resolve it?
My code:
popup.js
var port = chrome.runtime.connect({
name: 'Communication with background.js',
});
port.postMessage("Hi BackGround");
port.onMessage.addListener(function(msg) {
console.log("message recieved" + msg);
});
background.js
chrome.runtime.onConnect.addListener(function(port) {
console.log("Connected .....");
port.onMessage.addListener(function(msg) {
console.log("message recieved " + msg);
port.postMessage("Hi Popup.js");
});
});