This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
161 lines (149 loc) · 4.36 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!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>