-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.js
53 lines (46 loc) · 1.12 KB
/
manager.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
//init
var total_tabs = {};
var total_tabs_len = 0;
var max_tabs = 20;
chrome.tabs.query({},function(data){
for(i in data){
tab = data[i];
tab.last_used = tab.id; //not very accurate
total_tabs[tab.id] = tab;
total_tabs_len++;
}
});
//bind shit
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
delete total_tabs[tabId];
total_tabs_len--;
});
chrome.tabs.onHighlighted.addListener(function(highlightInfo) {
if (typeof total_tabs[highlightInfo.tabIds[0]] !== 'undefined')
total_tabs[highlightInfo.tabIds[0]].last_used = getTime();
});
chrome.tabs.onCreated.addListener(function(tab) {
tab.last_used = getTime();
total_tabs[tab.id] = tab;
total_tabs_len++;
if(total_tabs_len > max_tabs){
chrome.tabs.remove(tab.id, redirect);
}
});
function redirect(){
//lru here..
min_time= getTime();
id = 0;
w = 0;
for(i in total_tabs){
if(min_time > total_tabs[i].last_used){
id = total_tabs[i].index;
w = total_tabs[i].windowId;
min_time = total_tabs[i].last_used;
}
}
chrome.tabs.highlight({windowId:w,tabs:[id]},function(){});
}
function getTime(){
return (new Date().getTime())/1000;//sec
}