-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
101 lines (90 loc) · 2.56 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
// soon emoji (U+1F51C), hole emoij (U+1F573, U+FE0F)
const BOOKMARK_FOLDER_TITLE = "\uD83D\uDD1C\uD83D\uDD73\uFE0F";
const BOOKMARK_FOLDER_KEY = "bookmark_folder";
const DEBUG = false;
function log(str) {
if (DEBUG) {
console.log(str);
}
}
/**
* Returns the bookmark folder's id from storage, after checking that the folder exists,
* and creating it if necessary.
*/
function getBookmarkFolderId(callback) {
// Fetch persisted id from storage.
chrome.storage.sync.get(BOOKMARK_FOLDER_KEY, (items) => {
var folderId = chrome.runtime.lastError ? null : items[BOOKMARK_FOLDER_KEY];
if (!folderId) {
// Nothing in storage: create a folder.
createAndStoreBookmarkFolder(callback);
} else {
// Found an id in storage: check if it exists.
chrome.bookmarks.get(folderId, (folder) => {
if (chrome.runtime.lastError) {
// It doesn't exist -- let's create a new one.
createAndStoreBookmarkFolder(callback);
} else {
callback(folderId);
}
});
}
});
}
function createAndStoreBookmarkFolder(callback) {
chrome.bookmarks.create({ "title": BOOKMARK_FOLDER_TITLE }, (newFolder) => {
chrome.storage.sync.set({[BOOKMARK_FOLDER_KEY]: newFolder.id}, () => {
callback(newFolder.id);
});
});
}
function debugDuration(duration) {
var seconds;
if (duration == "day") {
seconds = 2;
} else if (duration == "week") {
seconds = 5;
} else if (duration == "month") {
seconds = 10;
}
return moment().add(seconds, "s");
}
function dateForDuration(duration) {
if (DEBUG) {
return debugDuration(duration);
}
var unit;
if (duration == "day") {
unit = "d";
} else if (duration == "week") {
unit = "w";
} else if (duration == "month") {
unit = "m";
}
return moment().add(1, unit);
}
// (2-24) Tab Title
function bookmarkTitle(tabTitle, dateToForget) {
var monthAndDay = dateToForget.format("M-D");
return "(" + monthAndDay + ") " + tabTitle;
}
// Called from popup.js
function addBookmark(tab, duration) {
var dateToForget = dateForDuration(duration);
getBookmarkFolderId((folderId) => {
var title = bookmarkTitle(tab.title, dateToForget);
var bookmark = {
"parentId": folderId,
"title": title,
"url": tab.url
};
chrome.bookmarks.create(bookmark, (newNode) => {
chrome.alarms.create(newNode.id, { "when": dateToForget.valueOf() });
});
});
}
chrome.alarms.onAlarm.addListener((alarm) => {
var bookmarkId = alarm.name;
log("Alarm fired: " + bookmarkId);
chrome.bookmarks.remove(bookmarkId);
});