-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbg.js
95 lines (77 loc) · 2.09 KB
/
bg.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
var accessTimes = {};
var OLD_AGE = 60000*60*4;
var GC_INTERVAL = 60000;
var UPDATE_INTERVAL = 60000;
var MAX_HISTORY = 10;
var lastRemoved = [];
// load configuration
function loadConfig(){
var conf_age = localStorage["old_age_mins"];
if (conf_age) {
OLD_AGE = conf_age * 60000;
}
}
// update access time of a tab
function updateAccess( tabId ) {
accessTimes[tabId] = new Date();
}
// store removed tab to the history list
function rememberRemoval( tab ) {
lastRemoved.unshift(tab);
if (lastRemoved.length > MAX_HISTORY) {
lastRemoved.pop();
}
}
// return last removed tabs
function getLast() {
return lastRemoved;
}
// load config at startup
loadConfig();
// update time of all tabs on plugin load
chrome.tabs.query( {}, function( tabs ) {
for (var i in tabs) {
var tab = tabs[i];
updateAccess(tab.id);
}
});
// handle new tab event
chrome.tabs.onCreated.addListener( function( tab ) {
updateAccess(tab.id);
});
// handle active tab event
chrome.tabs.onActivated.addListener( function( activeInfo ) {
updateAccess(activeInfo.tabId);
});
// handle tab removal event
chrome.tabs.onRemoved.addListener( function( tabId, removeInfo ) {
delete accessTimes[tabId];
});
// close all old inactive and unpinned tabs
function garbageCollect() {
// remove
for (var tabIdStr in accessTimes) {
var tabId = parseInt(tabIdStr,10);
var accessTime = accessTimes[tabId];
var now = new Date();
if ( (now - accessTime) >= OLD_AGE ) {
chrome.tabs.get(tabId, function(tab) {
if (!tab.pinned && !tab.active) {
chrome.tabs.remove([tab.id]);
rememberRemoval(tab);
}
});
}
}
}
// update access time for active tab
function updateActive() {
chrome.tabs.query( {active: true}, function callback(tabs) {
for (var i in tabs) {
var tab = tabs[i];
updateAccess( tab.id );
}
});
}
setInterval( garbageCollect, GC_INTERVAL );
setInterval( updateActive, UPDATE_INTERVAL );