This repository has been archived by the owner on Dec 11, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
background.js
356 lines (295 loc) · 9 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
/**
* Tab Snooze
*
* background.js
* Athyuttam Reddy Eleti
* @athyuttamre
*/
console.log("Welcome to background.js");
var snoozedTabs, settings;
init();
/**
* Initializes the extension by getting already snoozed
* tabs or starting a new list.
*/
function init() {
console.log("background.js initializing...");
// Load or set snoozedTabs
snoozedTabs = getSnoozedTabs();
if(!snoozedTabs) {
snoozedTabs = {};
snoozedTabs["tabCount"] = 0;
setSnoozedTabs(snoozedTabs);
}
// Load or set settings
settings = getSettings();
if(!settings) {
settings = {
"start-day": "9:00 AM",
"end-day": "6:00 PM",
"start-weekend": "10:00 AM",
"week-begin": 1,
"weekend-begin": 6,
"later-today": 3,
"someday": 3,
"open-new-tab": "true",
"badge": "true"
};
setSettings(settings);
localStorage.setItem("defaultSettings", JSON.stringify(settings));
}
// Set browserAction badge color
chrome.browserAction.setBadgeBackgroundColor({color: "#FED23B"});
updateBadgeText();
// Set popper loop
window.setInterval(popCheck, 10000);
}
/**
* Snoozes a tab for later.
*
* @param {Tab} tab A Tab object representing the current tab
* @param {int} popTime Time object representing when the tab should resurface
*/
function snooze(tab, popTime) {
// Get snoozed tabs
snoozedTabs = getSnoozedTabs();
// Remove tab
chrome.tabs.remove(tab.id);
// Add tab to snoozedTabs
addSnoozedTab(tab, popTime);
}
/**
* Checks every minute if there are tabs to be popped.
*/
function popCheck() {
if(!navigator.onLine) {
console.log("Offline, will popCheck later.");
return;
}
clearAllNotifications();
// Get snoozed tabs and settings
snoozedTabs = getSnoozedTabs();
settings = getSettings();
console.log("in popCheck, settings:", settings);
var timestamps = Object.keys(snoozedTabs).sort();
// Collect tabs to be popped and their times
var tabs = [];
var times = [];
for(var i = 0; i < timestamps.length - 1; i++) {
var time = timestamps[i];
var now = Date.now();
if(time < now) {
times.push(time);
tabs = tabs.concat(snoozedTabs[time]);
} else {
break;
}
}
if(tabs.length > 0) {
// Show notification
showNotification(tabs, function(id) {
chrome.notifications.onButtonClicked.addListener(function(nid, buttonIndex) {
// Pop tabs right now...
if(nid == id && buttonIndex == 0) {
if(settings["open-new-tab"] == "true") {
chrome.windows.getCurrent(function(w) {
popTabs(tabs, times, w);
});
} else {
chrome.windows.create({}, function(w) {
popTabs(tabs, times, w);
});
}
}
// ... or snooze them again
else if(nid == id && buttonIndex == 1) {
console.log("Snoozed for later!");
for(var i = 0; i < tabs.length; i++) {
var tab = tabs[i];
console.log(tab);
var popTime = new Date(tab.popTime);
console.log("popTime", popTime);
popTime.setHours(popTime.getHours() + 1);
console.log("new time", popTime);
changeSnoozeTime(tabs[i], popTime);
}
}
});
})
}
}
/**
* Shows the notification for the given tabs
* @param {Tab[]} tabs List of tabs waiting to be popped
* @param {Function} callback Callback function called after notification is shown
*/
function showNotification(tabs, callback) {
var tabCount = tabs.length;
var message = "" + tabCount + ((tabCount > 1) ? " tabs are back" : " tab is back");
chrome.notifications.create("", {
type: "basic",
priority: 1,
title: "Tab Snooze",
message: message,
iconUrl: chrome.extension.getURL("assets/icons/browserAction.png"),
buttons: [{
title: "Open Now"
}, {
title: "Later"
}]
}, function(id) {
callback(id);
});
}
/**
* Clears all notifications created by the extension.
*/
function clearAllNotifications() {
chrome.notifications.getAll(function(notifications) {
notifications = Object.keys(notifications);
for(var i = 0; i < notifications.length; i++) {
var id = notifications[i];
chrome.notifications.clear(id, function() {
});
}
});
}
/**
* Pops tabs that were supposed to resurface at given time
* @param {String} timestamp Timestamp of the tabs set
*/
function popTabs(tabs, times, w) {
/* NOTE: Creating tabs is asynchronous; It's possible for them
to fail and for us to delete the whole set from storage; FIX THIS. */
// Create tabs in window
for(var i = 0; i < tabs.length; i++) {
createTab(tabs[i], w);
}
// Delete keys
for(var i = 0; i < times.length; i++) {
delete snoozedTabs[times[i]];
}
// Update tabCount
snoozedTabs["tabCount"] -= tabs.length;
// Set badge text
updateBadgeText();
// Update snoozed tabs
setSnoozedTabs(snoozedTabs);
}
/**
* Adds a tab to the stored list of snoozed tabs.
* @param {Tab} tab A Tab object representing the snoozed tab
* @param {Time} popTime A Time object representing when the tab should pop
*/
function addSnoozedTab(tab, popTime) {
var fullTime = popTime.getTime();
if(!snoozedTabs[fullTime]) {
snoozedTabs[fullTime] = [];
}
// Add tab to snoozedTabs
snoozedTabs[fullTime].push({
url: tab.url,
title: tab.title,
favicon: tab.favIconUrl,
creationTime: (new Date()),
popTime: popTime
});
// Update tabCount
snoozedTabs["tabCount"] += 1;
// Set badge text
updateBadgeText();
// Update snoozedTabs
setSnoozedTabs(snoozedTabs);
}
/**
* Removes a tab from the given list of snoozed tabs.
* @param {Tab} tab Tab object to be removed
* @param {Tab[]} snoozedTabs The list of tabs the tab is removed from
*/
function removeSnoozedTab(tab, snoozedTabs) {
var popTime = (new Date(tab.popTime)).getTime();
var popSet = snoozedTabs[popTime];
console.log(tab);
console.log(popSet);
// Search for tab
var tabIndex = -1;
for(var i = 0; i < popSet.length; i++) {
if(popSet[i].creationTime == tab.creationTime) {
tabIndex = i;
break;
}
}
if(tabIndex < 0) {
console.log("Tab not found, returning");
return;
}
// Update pop set
popSet.splice(tabIndex, 1);
if(popSet.length == 0) {
delete snoozedTabs[popTime];
} else {
snoozedTabs[popTime] = popSet;
}
// Update tab count
snoozedTabs["tabCount"] -= 1;
}
/**
* Re-snoozes a tab to a new time
* @param {Tab} tab Tab object which is to be re-snoozed
* @param {Date} newTime Date object representing new snooze time
*/
function changeSnoozeTime(tab, newTime) {
if(newTime == tab.popTime) {
console.log("newTime same as oldTime, returning");
return;
}
// Load snoozedTabs
snoozedTabs = getSnoozedTabs();
// Remove tab from old alarm set
removeSnoozedTab(tab, snoozedTabs);
// Add tab to new alarm set
addSnoozedTab(tab, newTime);
}
/**
* Updates or hides the badge count of number of snoozed tabs.
*/
function updateBadgeText() {
var snoozedTabs = getSnoozedTabs();
var settings = getSettings();
// If badge==false, don't show badge text
if(settings["badge"] == "false") {
chrome.browserAction.setBadgeText({text: ""});
return;
}
if(!snoozedTabs) {
return;
}
// Update badge text
var snoozedCount = snoozedTabs["tabCount"];
var countString = (snoozedCount > 0) ? snoozedCount.toString() : "";
chrome.browserAction.setBadgeText({text: countString});
}
/**
* Creates a new tab in the given window.
* @param {Tab} tab Tab object representing the tab to be created
* @param {Window} w Window in which the tab needs to be created
*/
function createTab(tab, w) {
chrome.tabs.create({
windowId: w.id,
url: tab.url,
active: false
});
}
function getSnoozedTabs() {
return JSON.parse(localStorage.getItem("snoozedTabs"));
}
function setSnoozedTabs(newSnoozedTabs) {
localStorage.setItem("snoozedTabs", JSON.stringify(newSnoozedTabs));
}
function getSettings() {
return JSON.parse(localStorage.getItem("settings"));
}
function setSettings(newSettings) {
localStorage.setItem("settings", JSON.stringify(newSettings));
}