From 20e1144c3b9e828bed3bb1d54ce178b5f043ae82 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 5 Aug 2025 14:18:04 -0700 Subject: [PATCH] chore(extension): proper watchdog for inactive page selector (#835) --- extension/src/background.ts | 44 +++++++++++++++++++------------------ extension/tsconfig.json | 1 + 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/extension/src/background.ts b/extension/src/background.ts index 41b4399..e7b2efe 100644 --- a/extension/src/background.ts +++ b/extension/src/background.ts @@ -36,6 +36,7 @@ class TabShareExtension { constructor() { chrome.tabs.onRemoved.addListener(this._onTabRemoved.bind(this)); chrome.tabs.onUpdated.addListener(this._onTabUpdated.bind(this)); + chrome.tabs.onActivated.addListener(this._onTabActivated.bind(this)); chrome.runtime.onMessage.addListener(this._onMessage.bind(this)); } @@ -153,30 +154,31 @@ class TabShareExtension { this._connectedTabId = null; } - private async _onTabUpdated(tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab): Promise { - if (changeInfo.status === 'complete' && this._connectedTabId === tabId) { - await this._setConnectedTabId(tabId); - return; - } - const pending = this._pendingTabSelection.get(tabId); - if (!pending) - return; - if (tab.active && pending.timerId) { - clearTimeout(pending.timerId); - pending.timerId = undefined; - return; - } - if (!tab.active && !pending.timerId) { - debugLog('Starting inactivity timer', tabId); - pending.timerId = window.setTimeout(() => { - const existed = this._pendingTabSelection.delete(tabId); - if (existed) - pending.connection.close('Tab is not active'); - }, 5000); - return; + private _onTabActivated(activeInfo: chrome.tabs.TabActiveInfo) { + for (const [tabId, pending] of this._pendingTabSelection) { + if (tabId === activeInfo.tabId) { + if (pending.timerId) { + clearTimeout(pending.timerId); + pending.timerId = undefined; + } + continue; + } + if (!pending.timerId) { + pending.timerId = setTimeout(() => { + const existed = this._pendingTabSelection.delete(tabId); + if (existed) + pending.connection.close('Tab has been inactive for 5 seconds'); + }, 5000); + return; + } } } + private _onTabUpdated(tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) { + if (changeInfo.status === 'complete' && this._connectedTabId === tabId) + void this._setConnectedTabId(tabId); + } + private async _getTabs(): Promise { const tabs = await chrome.tabs.query({}); return tabs.filter(tab => tab.url && !['chrome:', 'edge:', 'devtools:'].some(scheme => tab.url!.startsWith(scheme))); diff --git a/extension/tsconfig.json b/extension/tsconfig.json index 6fa7759..70fb340 100644 --- a/extension/tsconfig.json +++ b/extension/tsconfig.json @@ -8,6 +8,7 @@ "rootDir": "src", "outDir": "./dist/lib", "resolveJsonModule": true, + "types": ["chrome"], "jsx": "react-jsx", "jsxImportSource": "react" },