// Tab Park - popup. Actions are handed to the background page: switching // tabs closes the popup and would otherwise kill the rest of the handler. const $ = id => document.getElementById(id); function tuSetStatus(text) { $('status').textContent = text; } async function tuRefreshCounts() { const tabs = await browser.tabs.query({ currentWindow: true }); const loaded = tabs.filter(tab => !tab.discarded).length; const pinned = tabs.filter(tab => tab.pinned).length; const parts = [`${loaded} loaded`, `${tabs.length - loaded} unloaded`]; if (pinned) parts.push(`${pinned} pinned`); $('counts').textContent = parts.join(', '); } function tuSyncDisabled() { const on = $('auto').checked; $('idle').disabled = !on; } async function tuLoadForm() { const settings = await tuSettings(); $('auto').checked = settings.autoUnload; $('idle').value = settings.idleSeconds; $('skip-audible').checked = settings.skipAudible; tuSyncDisabled(); } async function tuLoadProtect() { const [tab] = await browser.tabs.query({ active: true, currentWindow: true }); if (!tab) return; $('protect').checked = await tuIsProtected(tab.id); } $('protect').addEventListener('change', async () => { const [tab] = await browser.tabs.query({ active: true, currentWindow: true }); if (tab) await tuSetProtected(tab.id, $('protect').checked); }); $('auto').addEventListener('change', async () => { tuSyncDisabled(); await tuSaveSettings({ autoUnload: $('auto').checked }); }); $('idle').addEventListener('change', async () => { const value = Math.max(TU_MIN_IDLE_SECONDS, Math.round(Number($('idle').value) || TU_DEFAULTS.idleSeconds)); $('idle').value = value; await tuSaveSettings({ idleSeconds: value }); }); $('skip-audible').addEventListener('change', () => { tuSaveSettings({ skipAudible: $('skip-audible').checked }); }); $('unload-current').addEventListener('click', async () => { const win = await browser.windows.getCurrent(); tuSetStatus('Unloading...'); try { const ok = await browser.runtime.sendMessage({ type: 'tu:unload-current', windowId: win.id }); if (ok === false) tuSetStatus('Nothing to unload here'); } catch (e) { // Expected when the tab switch closes the popup mid-request. } }); $('unload-others').addEventListener('click', async () => { const win = await browser.windows.getCurrent(); const [tab] = await browser.tabs.query({ active: true, currentWindow: true }); tuSetStatus('Unloading...'); try { const count = await browser.runtime.sendMessage({ type: 'tu:unload-others', windowId: win.id, tabId: tab ? tab.id : null }); tuSetStatus(count ? `Unloaded ${count} tab${count === 1 ? '' : 's'}` : 'Nothing to unload'); await tuRefreshCounts(); } catch (e) { tuSetStatus('Could not unload tabs'); } }); $('options').addEventListener('click', event => { event.preventDefault(); browser.runtime.openOptionsPage(); }); tuLoadForm(); tuLoadProtect(); tuRefreshCounts();