Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: aef781b0c9
Fetching contributors…

Cannot retrieve contributors at this time

162 lines (149 sloc) 4.462 kb
<!DOCTYPE html>
<!--
ctrlSwitcher
Copyright 2010-2012 Paul Kehrer
Special thanks to:
Brian Kim (icon) (http://hippotrouble.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-->
<html>
<head>
<title>ctrlSwitcher</title>
<script type="text/javascript">
safari.application.addEventListener("message",captureMessageEvent,false);
safari.extension.settings.addEventListener("change", settingChanged, false);
function captureMessageEvent(event) {
if (event.name == 'switchTabs') {
switchTab(event.message);
} else if (event.name == 'getKeyCombo') {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("keyCombo", safari.extension.settings.getItem('keyCombo'));
}
}
function switchTab(keyCode) {
var currentWindow = safari.application.activeBrowserWindow;
if(safari.extension.settings.lastTab == keyCode) {
var lastTabIndex = currentWindow.tabs.length-1;
currentWindow.tabs[lastTabIndex].activate();
return;
}
var tab;
if (keyCode == 37 || keyCode == 39) {
var arrowKeys = safari.extension.settings.getItem('arrowKeys');
if (arrowKeys == true) {
tab = backForwardTab(keyCode);
} else {
return;
}
}
switch(keyCode) {
case 49: //1
case 50: //2
case 51: //3
case 52: //4
case 53: //5
case 54: //6
case 55: //7
case 56: //8
case 57: //9
tab = keyCode - 49;
break;
case 48: //0
//tabs are zero indexed.
tab = 9;
break;
case 229: //this keycode fires for some reason the first time you push ctrl-q...need to investigate why. for now, let's capture.
case 81: //q
tab = 10;
break;
case 87: //w
tab = 11;
break;
case 69: //e
tab = 12;
break;
case 82: //r
tab = 13;
break;
case 84: //t
tab = 14;
break;
case 89: //y
tab = 15;
break;
case 85: //u
tab = 16;
break;
case 73: //i
tab = 17;
break;
case 79: //o
tab = 18;
break;
case 80: //p
tab = 19;
break;
break;
default:
//do nothing
}
currentWindow.tabs[tab].activate();
}
function backForwardTab(keyCode) {
var currentWindow = safari.application.activeBrowserWindow;
var numTabs = currentWindow.tabs.length;
var activeTab = activeTabIndex();
var tab;
if (keyCode == 37) {
if (activeTab == 0) {
tab = numTabs - 1;
} else {
tab = activeTab - 1;
}
} else {
if (activeTab == (numTabs - 1)) {
tab = 0;
} else {
tab = activeTab + 1;
}
}
return tab;
}
function activeTabIndex() {
var activeTab = safari.application.activeBrowserWindow.activeTab;
for (var i = 0; i < safari.application.activeBrowserWindow.tabs.length; i++) {
var tab = safari.application.activeBrowserWindow.tabs[i];
if (tab === activeTab) {
return i;
}
}
return 0; //if for some crazy reason this doesn't match
}
function settingChanged(event) {
if(event.key == 'keyCombo') {
dispatchAll('keyCombo',event.newValue);
}
}
function dispatchAll(messageName, messageData) {
for (var i=0;i<safari.application.browserWindows.length;i++) {
var browserWindow = safari.application.browserWindows[i];
for (var j=0;j<browserWindow.tabs.length;j++) {
var tab = browserWindow.tabs[j];
tab.page.dispatchMessage(messageName, messageData);
}
}
}
</script>
</head>
<body>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.