From a4ee0b856cce5bb24ec7f9031db34e4924e49da0 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Fri, 13 Oct 2023 17:04:14 +0700 Subject: [PATCH] fix: prevent premature port disconnection --- static/background.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/static/background.js b/static/background.js index a5d6ba1..624bf7a 100644 --- a/static/background.js +++ b/static/background.js @@ -12,14 +12,6 @@ chrome.runtime.onConnect.addListener((port) => { if (message.type === 'ext/init') { ports.set(message.tabId, sender); - port.onDisconnect.addListener(() => { - ports.delete(message.tabId); - - if (ports.size === 0) { - chrome.tabs.onUpdated.removeListener(courier); - } - }); - return chrome.tabs.onUpdated.addListener(courier); } else if (message.type === 'ext/reload') { return chrome.runtime.reload(); @@ -30,6 +22,14 @@ chrome.runtime.onConnect.addListener((port) => { // relay messages from devtools page to `chrome.scripting` return chrome.tabs.sendMessage(message.tabId, message); }); + + port.onDisconnect.addListener((disconnected) => { + ports.delete(+disconnected.name); + + if (ports.size === 0) { + chrome.tabs.onUpdated.removeListener(courier); + } + }); }); // relay messages from `chrome.scripting` to devtools page