Skip to content

Commit

Permalink
Feat/cycle tabs (#14)
Browse files Browse the repository at this point in the history
* Add tab cycling

Add cycling behaviour for iterating recently audible tabs from
oldest-newest, or newest-oldest.

Add the hotkeys for this cycling behaviour.

* Fix tab cycling from toolbar
  • Loading branch information
Rainyan authored Nov 12, 2023
1 parent 86594ec commit 78bd92e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
40 changes: 33 additions & 7 deletions jump_to_audio_tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ function errorLog() {
console.error(arguments);
}

function jumpToLatestAudibleTab() {
let targetTabs = AUDIBLE_TABS.sort((a, b) => b.lastAccessed - a.lastAccessed);
if (targetTabs.length === 0) {
function negativeMod(dividend, divisor) {
return ((dividend % divisor) + divisor) % divisor;
}

function jumpToRecentlyAudibleTab(currentTab, offset) {
if (AUDIBLE_TABS.length === 0) {
return;
}
let targetTabId = targetTabs[0].id;

var index = AUDIBLE_TABS.findIndex((a) => a.id === currentTab.id);
if (index === -1) {
index = offset < 0 ? 0 : AUDIBLE_TABS.length - 1;
} else {
index = negativeMod(index + offset, AUDIBLE_TABS.length);
}
console.assert(index >= 0);
console.assert(index < AUDIBLE_TABS.length);

let targetTabId = AUDIBLE_TABS[index].id;
console.assert(targetTabId !== -1);

browser.tabs.update(targetTabId, { active: true }).then(() => {
Expand Down Expand Up @@ -50,13 +63,26 @@ function jumpToLatestAudibleTab() {
}

browser.commands.onCommand.addListener((command) => {
if (command === "jump-to-latest-audible-tab") {
jumpToLatestAudibleTab();
let offsets = {
"jump-to-latest-audible-tab-next": 1,
"jump-to-latest-audible-tab-prev": -1,
};
if (command in offsets) {
browser.tabs
.query({ active: true, currentWindow: true })
.catch((e) => {
console.error(e);
})
.then((tabs) => {
if (tabs.length === 1) {
jumpToRecentlyAudibleTab(tabs[0], offsets[command]);
}
});
}
});

browser.browserAction.onClicked.addListener((tab, info) => {
jumpToLatestAudibleTab();
jumpToRecentlyAudibleTab(tab, -1);
});

browser.contextMenus.onClicked.addListener((info, tab) => {
Expand Down
12 changes: 9 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version":2,
"name":"Jump to Audible Tabs",
"version":"0.3.0",
"version":"0.4.0",
"description":"Jump to tabs that are playing audio. Activate with the mouse right-click context menu.",
"icons":{
"48":"icons/Volume_Mute.svg",
Expand All @@ -28,11 +28,17 @@
}
},
"commands": {
"jump-to-latest-audible-tab": {
"jump-to-latest-audible-tab-prev": {
"suggested_key": {
"default": "Alt+J"
},
"description": "Jump to latest audible tab"
"description": "Jump to a recently audible tab, from newest to oldest"
},
"jump-to-latest-audible-tab-next": {
"suggested_key": {
"default": "Shift+Alt+J"
},
"description": "Jump to a recently audible tab, from oldest to newest"
}
}
}

0 comments on commit 78bd92e

Please sign in to comment.