Skip to content

Instantly share code, notes, and snippets.

@staydecent
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staydecent/65b7402276b5698b4fb2 to your computer and use it in GitHub Desktop.
Save staydecent/65b7402276b5698b4fb2 to your computer and use it in GitHub Desktop.
Command Tabs Opera Extension
// on proper key press
chrome.runtime.onMessage.addListener(function(request, sender) {
if (!request.tabIndex) { return; }
var tabIndex = request.tabIndex;
chrome.tabs.query({currentWindow: true, index: tabIndex}, function(tab) {
if (!tab.length) { return; }
chrome.tabs.update(tab[0].id, {active: true});
});
});
// Command Tabs
var commandIsPressed = false;
var commandKeyCode = 91;
var numberKeyCodes = {
49: 1,
50: 2,
51: 3,
52: 4,
53: 5,
54: 6,
55: 7,
56: 8,
57: 9
};
window.addEventListener('keydown', keyDown, false);
window.addEventListener('keyup', keyUp, false);
function keyDown(e) {
if (e.which === commandKeyCode) {
commandIsPressed = true;
}
if (!commandIsPressed) { return; }
// 1-9 number keys
if (e.which > 48 && e.which < 58) {
var tabIndex = numberKeyCodes[e.which] - 1; // 0-indexed
chrome.runtime.sendMessage({tabIndex: tabIndex});
}
}
function keyUp(e) {
if (e.which === commandKeyCode) {
commandIsPressed = false;
}
}
{
"name": "Command Tabs",
"version": "0.1.0",
"manifest_version": 2,
"description": "Focus tabs based on their index + the Command key.",
"developer": {
"name": "Adrian Unger",
"url": "http://staydecent.ca"
},
"background": {"scripts": ["background.js"]},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["command-tabs.js"]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment