|
| 1 | +import browser from "webextension-polyfill"; |
| 2 | +import { notify } from "../utils/notify"; |
| 3 | +import { getNextTabByIndex } from "../utils/tabUtils"; |
| 4 | + |
| 5 | +let tabLastSounded: number | undefined; |
| 6 | + |
| 7 | +export function setTabLastSounded(tabId: number) { |
| 8 | + tabLastSounded = tabId; |
| 9 | +} |
| 10 | + |
| 11 | +export async function focusTabLastSounded() { |
| 12 | + if (!tabLastSounded) |
| 13 | + return notify("No tab has emitted sound since startup.", { |
| 14 | + type: "warning", |
| 15 | + }); |
| 16 | + |
| 17 | + const tab = await browser.tabs.get(tabLastSounded); |
| 18 | + await browser.windows.update(tab.windowId!, { focused: true }); |
| 19 | + await browser.tabs.update(tabLastSounded, { active: true }); |
| 20 | +} |
| 21 | + |
| 22 | +export async function focusNextTabWithSound() { |
| 23 | + const tabsWithSound = await browser.tabs.query({ |
| 24 | + audible: true, |
| 25 | + muted: false, |
| 26 | + }); |
| 27 | + |
| 28 | + const nextTabWithSound = await getNextTabByIndex(tabsWithSound); |
| 29 | + if (!nextTabWithSound) return; |
| 30 | + |
| 31 | + await browser.windows.update(nextTabWithSound.windowId!, { focused: true }); |
| 32 | + await browser.tabs.update(nextTabWithSound.id, { active: true }); |
| 33 | +} |
| 34 | + |
| 35 | +export async function focusNextMutedTab() { |
| 36 | + const mutedTabs = await browser.tabs.query({ muted: true }); |
| 37 | + const nextMutedTab = await getNextTabByIndex(mutedTabs); |
| 38 | + if (!nextMutedTab) return; |
| 39 | + |
| 40 | + await browser.windows.update(nextMutedTab.windowId!, { focused: true }); |
| 41 | + await browser.tabs.update(nextMutedTab.id, { active: true }); |
| 42 | +} |
| 43 | + |
| 44 | +export async function focusNextAudibleTab() { |
| 45 | + const audibleTabs = await browser.tabs.query({ audible: true }); |
| 46 | + const nextAudibleTab = await getNextTabByIndex(audibleTabs); |
| 47 | + if (!nextAudibleTab) return; |
| 48 | + |
| 49 | + await browser.windows.update(nextAudibleTab.windowId!, { focused: true }); |
| 50 | + await browser.tabs.update(nextAudibleTab.id, { active: true }); |
| 51 | +} |
0 commit comments