forked from greatsuspender/thegreatsuspender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
340 lines (272 loc) · 10.1 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
/*global chrome, window, Image, console, gsStorage, setInterval */
var tgs = (function () {
"use strict";
var version = 4.74,
gsTimes = [];
function markTabUnsuspended(tabUrl) {
var tabProperties = gsStorage.fetchTabFromHistory(tabUrl);
//mark tab as unsuspended
//console.log("marking tab as unsuspended: " + tabUrl);
tabProperties.state = 'unsuspended';
gsStorage.saveTabToHistory(tabUrl, tabProperties);
}
function checkWhiteList(url) {
var whitelist = gsStorage.fetchWhitelist(),
whitelistedWords = whitelist.split(" "),
i,
ii = whitelistedWords.length;
for (i = 0; i < ii; i++) {
if (whitelistedWords[i].length > 0 && url.indexOf(whitelistedWords[i]) >= 0) {
return true;
}
}
return false;
}
function saveSuspendData(tab, previewUrl) {
var gsHistory = gsStorage.fetchGsHistory(),
tabProperties;
//console.log("attempting to suspend: " + tab.url);
if (previewUrl) {
gsStorage.setPreviewImage(tab.url, previewUrl);
}
tabProperties = {
date: new Date(),
title: tab.title,
url: tab.url,
state: 'suspended',
favicon: "chrome://favicon/" + tab.url,
pinned: tab.pinned,
index: tab.index,
windowId: tab.windowId
};
if (tab.incognito) {
tabProperties.favicon = tab.favIconUrl;
}
//add suspend information to start of history array
gsHistory.unshift(tabProperties);
//clean up old items
while (gsHistory.length > 100) {
gsHistory.pop();
}
gsStorage.setGsHistory(gsHistory);
}
function sendSuspendMessage(tab, callback) {
chrome.tabs.executeScript(tab.id, {file: "html2canvas.min.js"}, function () {
chrome.tabs.executeScript(tab.id, {file: "content_script.js"}, function () {
chrome.tabs.sendMessage(tab.id, {}, function (response) {
var previewUrl = response ? response.previewUrl : '';
callback(tab, previewUrl);
});
});
});
}
function suspendTab(tab) {
//don't allow suspending of already suspended tabs
if (tab.url.indexOf("chrome-extension:") >= 0 || tab.url.indexOf("chrome:") >= 0 || tab.url.indexOf("file:") >= 0) {
return;
}
//check whitelist
if (checkWhiteList(tab.url)) {
return;
}
var preview = gsStorage.fetchPreviewOption();
if (preview) {
sendSuspendMessage(tab, function (tab, previewUrl) {
saveSuspendData(tab, previewUrl);
chrome.tabs.update(tab.id, {url: gsStorage.generateSuspendedUrl(tab.url)});
});
} else {
saveSuspendData(tab, false);
chrome.tabs.update(tab.id, {url: gsStorage.generateSuspendedUrl(tab.url)});
}
}
function suspendSpecificTab(tab) {
if (!tab.active) {
suspendTab(tab);
//if tab is active then refresh timer for this tab
} else {
gsTimes[tab.id] = new Date();
}
}
function suspendActiveTab(window) {
var i,
ii = window.tabs.length;
for (i = 0; i < ii; i += 1) {
if (window.tabs[i].active) {
suspendTab(window.tabs[i]);
}
}
}
function suspendAllTabs(window) {
var i,
ii = window.tabs.length;
for (i = 0; i < ii; i += 1) {
if (window.tabs[i].url.indexOf("suspended.html") < 0) {
suspendTab(window.tabs[i]);
}
}
}
function unsuspendAllTabs(curWindow) {
var i,
ii = curWindow.tabs.length;
for (i = 0; i < ii; i += 1) {
//unsuspend if tab has been suspended
if (curWindow.tabs[i].url.indexOf("suspended.html") >= 0) {
chrome.tabs.sendMessage(curWindow.tabs[i].id, {action: "unsuspendTab"});
}
//reset timers for all tabs in this window
gsTimes[curWindow.tabs[i].id] = new Date();
}
}
function checkForTabsToAutoSuspend() {
var timeToSuspend = gsStorage.fetchTimeToSuspendOption();
if (timeToSuspend > 0) {
chrome.tabs.query({}, function (tabs) {
var i;
for (i in tabs) {
if (tabs.hasOwnProperty(i)) {
if (gsTimes[tabs[i].id]) {
//console.log("checking time for: " + tabs[i].url);
//console.log("time=" + (new Date() - gsTimes[tabs[i].id]));
if (new Date() - gsTimes[tabs[i].id] > timeToSuspend * 1000 * 60) {
//console.log("tab expired: " + tabs[i].url);
suspendSpecificTab(tabs[i]);
}
} else {
gsTimes[tabs[i].id] = new Date();
}
}
}
});
}
}
//add an initial time to all open tabs
function initialiseAllTabs() {
chrome.tabs.query({}, function (tabs) {
var i;
for (i in tabs) {
if (tabs.hasOwnProperty(i)) {
gsTimes[tabs[i].id] = new Date();
}
}
});
}
//check for tabs that have a state of 'suspended'
function checkForCrashedTabs() {
chrome.tabs.query({}, function (tabs) {
//first check to see if there are any suspended tabs already
var i,
ii,
possibleCrash = false,
openTabs = {},
gsHistory;
//if there is only one open tab then assume its a chrome restart (and don't restore)
if (tabs.length < 2) {
return;
}
for (i in tabs) {
if (tabs.hasOwnProperty(i)) {
if (tabs[i].url.indexOf('suspended.html') >= 0) {
return;
} else {
openTabs[tabs[i].url] = true;
}
}
}
gsHistory = gsStorage.fetchGsHistory();
for (i in gsHistory) {
if (gsHistory.hasOwnProperty(i)
&& gsHistory[i].state === 'suspended'
&& typeof (openTabs[gsHistory[i].url]) === 'undefined') {
possibleCrash = true;
}
}
//if it's possible that we have crashed then show the recovery tab
if (possibleCrash) {
chrome.tabs.create({url: chrome.extension.getURL("recovery.html")});
}
});
}
function checkForNewVersion() {
var lastVersion = gsStorage.fetchVersion(),
gsHistory,
oldGsHistory,
i,
ii,
upgraded = false;
oldGsHistory = gsStorage.fetchOldGsHistory();
//check for very old history migration
if (oldGsHistory !== null &&
(lastVersion === null || parseFloat(lastVersion) < version)) {
//merge old gsHistory with new one
gsHistory = gsStorage.fetchGsHistory();
ii = oldGsHistory.length;
for (i = 0; i < ii; i++) {
gsHistory.push(oldGsHistory[i]);
}
gsStorage.setGsHistory(gsHistory);
gsStorage.removeOldGsHistory();
}
//if they are installing for the first time
if (lastVersion === null && gsStorage.fetchGsHistory().length === 0) {
//show welcome screen
chrome.tabs.create({url: chrome.extension.getURL("welcome.html")});
gsStorage.setVersion(version);
gsStorage.setGsHistory([]);
upgraded = true;
//otherwise if they are upgrading
} else if (parseFloat(lastVersion) < version) {
//show new update screen
chrome.tabs.create({url: chrome.extension.getURL("update.html")});
gsStorage.setVersion(version);
upgraded = true;
}
return upgraded;
}
//handler for popup clicks
chrome.extension.onRequest.addListener(
function (request, sender, sendResponse) {
if (request.msg === "suspendOne") {
chrome.windows.getLastFocused({populate: true}, suspendActiveTab);
} else if (request.msg === "suspendAll") {
chrome.windows.getLastFocused({populate: true}, suspendAllTabs);
} else if (request.msg === "unsuspendAll") {
chrome.windows.getLastFocused({populate: true}, unsuspendAllTabs);
}
}
);
//handler for unsuspend
chrome.extension.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.action === "setUnsuspendedState") {
markTabUnsuspended(request.tabUrl);
}
}
);
//listen for tab create
chrome.tabs.onCreated.addListener(function (tab) {
gsTimes[tab.id] = new Date();
});
//listen for tab switching
chrome.tabs.onSelectionChanged.addListener(function (tabId, info) {
var unsuspend = gsStorage.fetchUnsuspendOnFocusOption();
if (unsuspend && gsTimes[tabId] && (new Date() - gsTimes[tabId] > 3000)) {
chrome.tabs.get(tabId, function (tab) {
if (tab.url.indexOf("suspended.html") >= 0) {
chrome.tabs.sendMessage(tab.id, {action: "unsuspendTab"});
gsTimes[tabId] = new Date(); //reset timer after automatic unsuspend
}
});
} else if (gsTimes[tabId]) {
gsTimes[tabId] = new Date();
}
});
initialiseAllTabs();
if (!checkForNewVersion()) {
checkForCrashedTabs();
}
//start timer
setInterval(function () {
checkForTabsToAutoSuspend();
}, 1000 * 60);
}());