chore(extension): proper watchdog for inactive page selector (#835)

This commit is contained in:
Yury Semikhatsky
2025-08-05 14:18:04 -07:00
committed by GitHub
parent eab20aa69e
commit 20e1144c3b
2 changed files with 24 additions and 21 deletions

View File

@@ -36,6 +36,7 @@ class TabShareExtension {
constructor() { constructor() {
chrome.tabs.onRemoved.addListener(this._onTabRemoved.bind(this)); chrome.tabs.onRemoved.addListener(this._onTabRemoved.bind(this));
chrome.tabs.onUpdated.addListener(this._onTabUpdated.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)); chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
} }
@@ -153,30 +154,31 @@ class TabShareExtension {
this._connectedTabId = null; this._connectedTabId = null;
} }
private async _onTabUpdated(tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab): Promise<void> { private _onTabActivated(activeInfo: chrome.tabs.TabActiveInfo) {
if (changeInfo.status === 'complete' && this._connectedTabId === tabId) { for (const [tabId, pending] of this._pendingTabSelection) {
await this._setConnectedTabId(tabId); if (tabId === activeInfo.tabId) {
return; if (pending.timerId) {
} clearTimeout(pending.timerId);
const pending = this._pendingTabSelection.get(tabId); pending.timerId = undefined;
if (!pending) }
return; continue;
if (tab.active && pending.timerId) { }
clearTimeout(pending.timerId); if (!pending.timerId) {
pending.timerId = undefined; pending.timerId = setTimeout(() => {
return; const existed = this._pendingTabSelection.delete(tabId);
} if (existed)
if (!tab.active && !pending.timerId) { pending.connection.close('Tab has been inactive for 5 seconds');
debugLog('Starting inactivity timer', tabId); }, 5000);
pending.timerId = window.setTimeout(() => { return;
const existed = this._pendingTabSelection.delete(tabId); }
if (existed)
pending.connection.close('Tab is not active');
}, 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<chrome.tabs.Tab[]> { private async _getTabs(): Promise<chrome.tabs.Tab[]> {
const tabs = await chrome.tabs.query({}); const tabs = await chrome.tabs.query({});
return tabs.filter(tab => tab.url && !['chrome:', 'edge:', 'devtools:'].some(scheme => tab.url!.startsWith(scheme))); return tabs.filter(tab => tab.url && !['chrome:', 'edge:', 'devtools:'].some(scheme => tab.url!.startsWith(scheme)));

View File

@@ -8,6 +8,7 @@
"rootDir": "src", "rootDir": "src",
"outDir": "./dist/lib", "outDir": "./dist/lib",
"resolveJsonModule": true, "resolveJsonModule": true,
"types": ["chrome"],
"jsx": "react-jsx", "jsx": "react-jsx",
"jsxImportSource": "react" "jsxImportSource": "react"
}, },