Wow!! Thank you so much, this solved the
disconnected port issue.
But now I am getting port closed when I am trying to receive message from contentScript to devtools-script. For first 5min, devtools-script able to get the response but after 5min, not able to receive the response. I have tried to apply same wait logic in background.js but no luck.
I tried hard but not able to fix this issue, could please guide me here. Appreciate your help. Thank you.
"background.js:1 Uncaught (in promise) Error: The message port closed before a response was received."
devtools-script.js
let connectBackgroundPage;
connect();
setInterval(connect, 250e3);
function connect() {
connectBackgroundPage?.disconnect();
connectBackgroundPage = browserType.runtime.connect({
name: "devtools-page"
});
connectBackgroundPage.postMessage({
name: "init",
tabId: browserType.devtools.inspectedWindow.tabId,
contentScript: "../content-script/contentScript.js",
contentCSS: "../content-script/contentScript.css"
});
}
background.js
browserType.runtime.onConnect.addListener(function(port) {
var extensionListener = function(message, sender, sendResponse) {
if (message.name == "init") {
connections[message.tabId] = port;
return;
}
}
port.onMessage.addListener(extensionListener);
port.onDisconnect.addListener(function(port) {
port.onMessage.removeListener(extensionListener);
var tabs = Object.keys(connections);
for (var i = 0, len = tabs.length; i < len; i++) {
if (connections[tabs[i]] == port) {
delete connections[tabs[i]]
break;
}
}
});
});