Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/cycle tabs #14

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
}
Loading