-
Notifications
You must be signed in to change notification settings - Fork 36
/
background.js
364 lines (297 loc) · 7.84 KB
/
background.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
var mru = [];
var slowSwitchOngoing = false;
var fastSwitchOngoing = false;
var intSwitchCount = 0;
var lastIntSwitchIndex = 0;
var altPressed = false;
var wPressed = false;
var domLoaded = false
var quickActive = 0;
var slowActive = 0;
var prevTimestamp = 0;
var slowtimerValue = 1500;
var fasttimerValue = 200;
var timer;
var slowswitchForward = false;
var initialized = false;
var loggingOn = false;
var CLUTlog = function(str) {
if(loggingOn) {
console.log(str);
}
}
function onInstall() {
CLUTlog("Extension Installed");
chrome.windows.create({url:"http://www.harshay-buradkar.com/clut_update6.html"});
}
function onUpdate() {
CLUTlog("Extension Updated");
chrome.windows.create({url:"http://www.harshay-buradkar.com/clut_update6.html"});
}
function getVersion() {
var details = chrome.app.getDetails();
return details.version;
}
// Check if the version has changed.
var currVersion = getVersion();
var prevVersion = localStorage['version']
CLUTlog("prev version: "+prevVersion);
CLUTlog("curr version: "+currVersion);
if (currVersion != prevVersion) {
// Check if we just installed this extension.
if (typeof prevVersion == 'undefined') {
onInstall();
} else {
onUpdate();
}
localStorage['version'] = currVersion;
}
var processCommand = function(command) {
CLUTlog('Command recd:' + command);
var fastswitch = true;
slowswitchForward = false;
if(command == "alt_switch_fast") {
fastswitch = true;
quickSwitchActiveUsage();
} else if(command == "alt_switch_slow_backward") {
fastswitch = false;
slowswitchForward = false;
slowSwitchActiveUsage();
} else if(command == "alt_switch_slow_forward") {
fastswitch = false;
slowswitchForward = true;
slowSwitchActiveUsage();
}
if(!slowSwitchOngoing && !fastSwitchOngoing) {
if(fastswitch) {
fastSwitchOngoing = true;
} else {
slowSwitchOngoing = true;
}
CLUTlog("CLUT::START_SWITCH");
intSwitchCount = 0;
doIntSwitch();
} else if((slowSwitchOngoing && !fastswitch) || (fastSwitchOngoing && fastswitch)){
CLUTlog("CLUT::DO_INT_SWITCH");
doIntSwitch();
} else if(slowSwitchOngoing && fastswitch) {
endSwitch();
fastSwitchOngoing = true;
CLUTlog("CLUT::START_SWITCH");
intSwitchCount = 0;
doIntSwitch();
} else if(fastSwitchOngoing && !fastswitch) {
endSwitch();
slowSwitchOngoing = true;
CLUTlog("CLUT::START_SWITCH");
intSwitchCount = 0;
doIntSwitch();
}
if(timer) {
if(fastSwitchOngoing || slowSwitchOngoing) {
clearTimeout(timer);
}
}
if(fastswitch) {
timer = setTimeout(function() {endSwitch()},fasttimerValue);
} else {
timer = setTimeout(function() {endSwitch()},slowtimerValue);
}
};
chrome.commands.onCommand.addListener(processCommand);
chrome.browserAction.onClicked.addListener(function(tab) {
CLUTlog('Click recd');
processCommand('alt_switch_fast');
});
chrome.runtime.onStartup.addListener(function () {
CLUTlog("on startup");
initialize();
});
chrome.runtime.onInstalled.addListener(function () {
CLUTlog("on startup");
initialize();
});
var doIntSwitch = function() {
CLUTlog("CLUT:: in int switch, intSwitchCount: "+intSwitchCount+", mru.length: "+mru.length);
if (intSwitchCount < mru.length && intSwitchCount >= 0) {
var tabIdToMakeActive;
//check if tab is still present
//sometimes tabs have gone missing
var invalidTab = true;
var thisWindowId;
if(slowswitchForward) {
decrementSwitchCounter();
} else {
incrementSwitchCounter();
}
tabIdToMakeActive = mru[intSwitchCount];
chrome.tabs.get(tabIdToMakeActive, function(tab) {
if(tab) {
thisWindowId = tab.windowId;
invalidTab = false;
chrome.windows.update(thisWindowId, {"focused":true});
chrome.tabs.update(tabIdToMakeActive, {active:true, highlighted: true});
lastIntSwitchIndex = intSwitchCount;
//break;
} else {
CLUTlog("CLUT:: in int switch, >>invalid tab found.intSwitchCount: "+intSwitchCount+", mru.length: "+mru.length);
removeItemAtIndexFromMRU(intSwitchCount);
if(intSwitchCount >= mru.length) {
intSwitchCount = 0;
}
doIntSwitch();
}
});
}
}
var endSwitch = function() {
CLUTlog("CLUT::END_SWITCH");
slowSwitchOngoing = false;
fastSwitchOngoing = false;
var tabId = mru[lastIntSwitchIndex];
putExistingTabToTop(tabId);
printMRUSimple();
}
chrome.tabs.onActivated.addListener(function(activeInfo){
if(!slowSwitchOngoing && !fastSwitchOngoing) {
var index = mru.indexOf(activeInfo.tabId);
//probably should not happen since tab created gets called first than activated for new tabs,
// but added as a backup behavior to avoid orphan tabs
if(index == -1) {
CLUTlog("Unexpected scenario hit with tab("+activeInfo.tabId+").")
addTabToMRUAtFront(activeInfo.tabId)
} else {
putExistingTabToTop(activeInfo.tabId);
}
}
});
chrome.tabs.onCreated.addListener(function(tab) {
CLUTlog("Tab create event fired with tab("+tab.id+")");
addTabToMRUAtBack(tab.id);
});
chrome.tabs.onRemoved.addListener(function(tabId, removedInfo) {
CLUTlog("Tab remove event fired from tab("+tabId+")");
removeTabFromMRU(tabId);
});
var addTabToMRUAtBack = function(tabId) {
var index = mru.indexOf(tabId);
if(index == -1) {
//add to the end of mru
mru.splice(-1, 0, tabId);
}
}
var addTabToMRUAtFront = function(tabId) {
var index = mru.indexOf(tabId);
if(index == -1) {
//add to the front of mru
mru.splice(0, 0,tabId);
}
}
var putExistingTabToTop = function(tabId){
var index = mru.indexOf(tabId);
if(index != -1) {
mru.splice(index, 1);
mru.unshift(tabId);
}
}
var removeTabFromMRU = function(tabId) {
var index = mru.indexOf(tabId);
if(index != -1) {
mru.splice(index, 1);
}
}
var removeItemAtIndexFromMRU = function(index) {
if(index < mru.length) {
mru.splice(index, 1);
}
}
var incrementSwitchCounter = function() {
intSwitchCount = (intSwitchCount+1)%mru.length;
}
var decrementSwitchCounter = function() {
if(intSwitchCount == 0) {
intSwitchCount = mru.length - 1;
} else {
intSwitchCount = intSwitchCount - 1;
}
}
var initialize = function() {
if(!initialized) {
initialized = true;
chrome.windows.getAll({populate:true},function(windows){
windows.forEach(function(window){
window.tabs.forEach(function(tab){
mru.unshift(tab.id);
});
});
CLUTlog("MRU after init: "+mru);
});
}
}
var printTabInfo = function(tabId) {
var info = "";
chrome.tabs.get(tabId, function(tab) {
info = "Tabid: "+tabId+" title: "+tab.title;
});
return info;
}
var str = "MRU status: \n";
var printMRU = function() {
str = "MRU status: \n";
for(var i = 0; i < mru.length; i++) {
chrome.tabs.get(mru[i], function(tab) {
});
}
CLUTlog(str);
}
var printMRUSimple = function() {
CLUTlog("mru: "+mru);
}
var generatePrintMRUString = function() {
chrome.tabs.query(function() {
});
str += (i + " :("+tab.id+")"+tab.title);
str += "\n";
}
initialize();
var quickSwitchActiveUsage = function() {
if(domLoaded) {
if(quickActive == -1) {
return;
} else if(quickActive < 5){
quickActive++;
} else if(quickActive >= 5) {
_gaq.push(['_trackEvent', 'activeUsage', 'quick']);
quickActive = -1;
}
}
}
var slowSwitchActiveUsage = function() {
if(domLoaded) {
if(slowActive == -1) {
return;
} else if(slowActive < 5){
slowActive++;
} else if(slowActive >= 5) {
_gaq.push(['_trackEvent', 'activeUsage', 'slow']);
slowActive = -1;
}
}
}
//check for actual active users
var _AnalyticsCode = 'UA-51920707-1';
var _gaq = _gaq || [];
_gaq.push(['_setAccount', _AnalyticsCode]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
//ga.src = 'https://ssl.google-analytics.com/u/ga_debug.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
document.addEventListener('DOMContentLoaded', function () {
domLoaded = true;
});