diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..2de3e93 --- /dev/null +++ b/manifest.json @@ -0,0 +1,30 @@ +{ + "manifest_version": 2, + + "name": "Tab Shortcuts", + "description": "This extension allows shortcuts for next / previous tabs to be reassigned", + "version": "0.1", + + "permissions": [ + "tabs" + ], + + "commands" : { + "next-tab" : { + "suggested_key" : { "default" : "Ctrl+J" }, + "description" : "Go to next tab" + }, + "prev-tab" : { + "suggested_key" : { "default" : "Ctrl+K" }, + "description" : "Go to previous tab" + }, + "remap-downloads" : { + "suggested_key" : { "default" : "Ctrl+Shift+J" }, + "description" : "New downloads shortcut" + } + }, + + "background" : { + "scripts" : ["script.js"] + } +} diff --git a/script.js b/script.js new file mode 100644 index 0000000..e164225 --- /dev/null +++ b/script.js @@ -0,0 +1,43 @@ +var DL_URL = "chrome://downloads/"; + +function change_tab(next_prev) { + chrome.tabs.query({currentWindow: true, active: true}, function(tabs) { + for (i in tabs) { + var tab = tabs[i]; + chrome.tabs.query({windowId:tab.windowId}, function(alltabs) { + chrome.tabs.query({index: (next_prev(tab.index) + alltabs.length) % alltabs.length, + windowId:tab.windowId}, function(tabs) { + for (i in tabs) { + chrome.tabs.update(tabs[i].id, {active:true}); + } + }); + }); + } + }); +} + +function open_downloads() { + chrome.tabs.query({currentWindow: true, url: DL_URL}, function (tabs) { + var done = false; + for (i in tabs) { + chrome.tabs.update(tabs[i].id, {active:true}); + done = true; + } + // if there is no existing downloads tab + if (done == false) { + chrome.tabs.create({url: DL_URL}); + } + }); +} + +chrome.commands.onCommand.addListener(function(command) { + if (command == 'next-tab') { + change_tab(function(x) { return x + 1 }); + } + if (command == 'prev-tab') { + change_tab(function(x) { return x - 1 }); + } + if (command == 'remap-downloads') { + open_downloads(); + } +});