-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands for focusing audible/muted tabs and for muting/unmuting …
…tabs (#311) * Add command to focus the last tab that emitted sound * Add command to focus the next tab that is reproducing sound * Add command to focus the next muted tab * Add command to focus next tab that is playing audio * Add several commands for muting or unmuting tabs
- Loading branch information
1 parent
abca9f8
commit 2f31ecf
Showing
7 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import browser from "webextension-polyfill"; | ||
import { notify } from "../utils/notify"; | ||
import { getNextTabByIndex } from "../utils/tabUtils"; | ||
|
||
let tabLastSounded: number | undefined; | ||
|
||
export function setTabLastSounded(tabId: number) { | ||
tabLastSounded = tabId; | ||
} | ||
|
||
export async function focusTabLastSounded() { | ||
if (!tabLastSounded) | ||
return notify("No tab has emitted sound since startup.", { | ||
type: "warning", | ||
}); | ||
|
||
const tab = await browser.tabs.get(tabLastSounded); | ||
await browser.windows.update(tab.windowId!, { focused: true }); | ||
await browser.tabs.update(tabLastSounded, { active: true }); | ||
} | ||
|
||
export async function focusNextTabWithSound() { | ||
const tabsWithSound = await browser.tabs.query({ | ||
audible: true, | ||
muted: false, | ||
}); | ||
|
||
const nextTabWithSound = await getNextTabByIndex(tabsWithSound); | ||
if (!nextTabWithSound) return; | ||
|
||
await browser.windows.update(nextTabWithSound.windowId!, { focused: true }); | ||
await browser.tabs.update(nextTabWithSound.id, { active: true }); | ||
} | ||
|
||
export async function focusNextMutedTab() { | ||
const mutedTabs = await browser.tabs.query({ muted: true }); | ||
const nextMutedTab = await getNextTabByIndex(mutedTabs); | ||
if (!nextMutedTab) return; | ||
|
||
await browser.windows.update(nextMutedTab.windowId!, { focused: true }); | ||
await browser.tabs.update(nextMutedTab.id, { active: true }); | ||
} | ||
|
||
export async function focusNextAudibleTab() { | ||
const audibleTabs = await browser.tabs.query({ audible: true }); | ||
const nextAudibleTab = await getNextTabByIndex(audibleTabs); | ||
if (!nextAudibleTab) return; | ||
|
||
await browser.windows.update(nextAudibleTab.windowId!, { focused: true }); | ||
await browser.tabs.update(nextAudibleTab.id, { active: true }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import browser from "webextension-polyfill"; | ||
import { getCurrentTabId } from "../utils/getCurrentTab"; | ||
import { getTabIdForMarker } from "../misc/tabMarkers"; | ||
import { getNextTabByIndex } from "../utils/tabUtils"; | ||
import { notify } from "../utils/notify"; | ||
|
||
export async function muteTab(tabMarkers?: string[], mute = true) { | ||
if (tabMarkers) { | ||
const tabsToMute = await Promise.all(tabMarkers.map(getTabIdForMarker)); | ||
|
||
return Promise.all( | ||
tabsToMute.map(async (tabId) => | ||
browser.tabs.update(tabId, { muted: mute }) | ||
) | ||
); | ||
} | ||
|
||
const tabToMute = await getCurrentTabId(); | ||
return browser.tabs.update(tabToMute, { muted: mute }); | ||
} | ||
|
||
export async function muteNextTabWithSound() { | ||
const tabsWithSound = await browser.tabs.query({ | ||
audible: true, | ||
muted: false, | ||
}); | ||
|
||
const nextTabWithSound = await getNextTabByIndex(tabsWithSound); | ||
if (!nextTabWithSound) | ||
return notify("There are currently no tabs with sound", { | ||
type: "warning", | ||
}); | ||
|
||
await browser.tabs.update(nextTabWithSound.id, { muted: true }); | ||
} | ||
|
||
export async function unmuteNextMutedTab() { | ||
const mutedTabs = await browser.tabs.query({ muted: true }); | ||
const nextMutedTab = await getNextTabByIndex(mutedTabs); | ||
if (!nextMutedTab) | ||
return notify("There are currently no muted tabs", { | ||
type: "warning", | ||
}); | ||
|
||
await browser.tabs.update(nextMutedTab.id, { muted: false }); | ||
} | ||
|
||
export async function muteAllTabsWithSound() { | ||
const tabsWithSound = await browser.tabs.query({ | ||
audible: true, | ||
muted: false, | ||
}); | ||
|
||
await Promise.all( | ||
tabsWithSound.map(async (tab) => | ||
browser.tabs.update(tab.id, { muted: true }) | ||
) | ||
); | ||
} | ||
|
||
export async function unmuteAllMutedTabs() { | ||
const mutedTabs = await browser.tabs.query({ muted: true }); | ||
|
||
await Promise.all( | ||
mutedTabs.map(async (tab) => browser.tabs.update(tab.id, { muted: false })) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import browser from "webextension-polyfill"; | ||
import { getCurrentTab } from "./getCurrentTab"; | ||
|
||
/** | ||
* Given an array of tabs as a parameter, return the first tab in the array that | ||
* has a greater index than the current tab. If no such tab exists in the | ||
* current window cycle through all existing windows returning to the start of | ||
* the current window if necessary. | ||
*/ | ||
export async function getNextTabByIndex(tabs: browser.Tabs.Tab[]) { | ||
const currentTab = await getCurrentTab(); | ||
|
||
return ( | ||
tabs.find( | ||
(tab) => | ||
(tab.windowId === currentTab.windowId && | ||
tab.index > currentTab.index) || | ||
tab.windowId !== currentTab.windowId | ||
) ?? tabs[0] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters