-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdestroyer.js
220 lines (182 loc) · 5.42 KB
/
destroyer.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
/* globals $, chrome */
var usedOn = {};
var openedOn = {};
var accessed = {};
var activeTabId;
var timeout;
var activeInterval = 2500;
function _debug() {
// console.log.apply(console, arguments);
}
function _getMax() {
return parseInt(localStorage.max || 20);
}
function _getAlgo() {
return localStorage.algo || 'used';
}
function _markActive(tabId) {
_debug('marked active', tabId);
usedOn[tabId] = new Date().getTime();
accessed[tabId] += 1;
}
function _handleTabActivated(data) {
var tabId = data.tabId;
activeTabId = tabId;
_debug('activated', tabId);
clearTimeout(timeout);
// after 3 seconds mark this tab as active
// this is so if you are quickly switching tabs
// they are not considered active
timeout = setTimeout(function() {
_markActive(tabId);
}, activeInterval);
}
function _handleTabRemoved(tabId) {
clearTimeout(timeout);
_debug('removed', tabId);
delete usedOn[tabId];
delete openedOn[tabId];
delete accessed[tabId];
}
function _handleTabReplaced(newTabId, oldTabId) {
if (usedOn[oldTabId]) {
usedOn[newTabId] = usedOn[oldTabId];
}
if (openedOn[oldTabId]) {
openedOn[newTabId] = openedOn[oldTabId];
}
if (accessed[oldTabId]) {
accessed[newTabId] = accessed[oldTabId];
}
delete usedOn[oldTabId];
delete openedOn[oldTabId];
delete accessed[oldTabId];
}
function _removeTab(tabId) {
_debug('_removeTab', tabId);
if (tabId) {
chrome.tabs.remove(tabId, function() {});
// _handleTabRemoved(tabId);
}
}
function _getLowestIn(data, tabs) {
var lowest;
var lowestIndex;
var tabId;
var value;
for (var i = 0; i < tabs.length; i++) {
tabId = tabs[i].id;
// never close the currently active tab
if (tabId === activeTabId) {
continue;
}
// if you have never been to this tab then skip it
if (!usedOn.hasOwnProperty(tabId) || !data.hasOwnProperty(tabId)) {
continue;
}
value = data[tabId] || 0;
if (lowest === undefined) {
lowest = value;
}
if (value <= lowest) {
lowestIndex = i;
lowest = value;
}
}
return lowestIndex;
}
function _removeLeastAccessed(tabs) {
var removeTabIndex = _getLowestIn(accessed, tabs);
if (removeTabIndex >= 0) {
_removeTab(tabs[removeTabIndex].id);
tabs.splice(removeTabIndex, 1);
}
return tabs;
}
function _removeOldest(tabs) {
var removeTabIndex = _getLowestIn(openedOn, tabs);
if (removeTabIndex >= 0) {
_removeTab(tabs[removeTabIndex].id);
tabs.splice(removeTabIndex, 1);
}
return tabs;
}
function _removeLeastRecentlyUsed(tabs) {
var removeTabIndex = _getLowestIn(usedOn, tabs);
if (removeTabIndex >= 0) {
_removeTab(tabs[removeTabIndex].id);
tabs.splice(removeTabIndex, 1);
}
return tabs;
}
function _removeTabs(tabs) {
var length = tabs.length;
_debug('there are', tabs.length, 'tabs open');
_debug('max is', _getMax());
while (length >= _getMax()) {
_debug('removing a tab with length', length);
switch (_getAlgo()) {
case 'oldest':
tabs = _removeOldest(tabs);
break;
case 'accessed':
tabs = _removeLeastAccessed(tabs);
break;
default:
tabs = _removeLeastRecentlyUsed(tabs);
break;
}
length -= 1;
}
}
function _handleTabAdded(data) {
var tabId = data.id || data;
_debug('added', tabId);
// find tab to remove
chrome.tabs.query({currentWindow: true}, function(tabs) {
tabs = tabs.filter(function(tab) {
return !tab.pinned && tab.id != tabId;
});
_debug('Total tabs', tabs.length);
_debug('Max tabs', _getMax());
if (tabs.length >= _getMax()) {
// If this is set to block just immediately remove this tab before
// even adding info about it
if (_getAlgo() === 'block') {
_removeTab(tabId);
return;
}
_removeTabs(tabs);
}
openedOn[tabId] = new Date().getTime();
accessed[tabId] = 0;
});
}
function _bindEvents() {
chrome.tabs.onActivated.addListener(_handleTabActivated);
chrome.tabs.onCreated.addListener(_handleTabAdded);
chrome.tabs.onAttached.addListener(_handleTabAdded);
chrome.tabs.onRemoved.addListener(_handleTabRemoved);
chrome.tabs.onDetached.addListener(_handleTabRemoved);
chrome.tabs.onReplaced.addListener(_handleTabReplaced);
}
function _init() {
// on startup loop through all existing tabs and set them to active
// this is only needed so that if you first install the extension
// or bring a bunch of tabs in on startup it will work
//
// setting the time to their tab id ensures they will be closed in
// the order they were opened in and there is no way to figure
// out what time a tab was opened from chrome apis
chrome.tabs.query({}, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
if (!usedOn.hasOwnProperty(tabs[i].id)) {
openedOn[tabs[i].id] = tabs[i].id;
usedOn[tabs[i].id] = tabs[i].id;
accessed[tabs[i].id] = 0;
}
}
_bindEvents();
});
}
$.ready(_init);