-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
290 lines (265 loc) · 7.74 KB
/
popup.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
function clearTime(d) {
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
}
function today() {
let d = new Date();
clearTime(d);
return d.getTime();
}
function getDateString(timestamp) {
let dateObj = new Date(timestamp);
let year = dateObj.getFullYear();
let month = String(dateObj.getMonth() + 1).padStart(2, '0');
let date = String(dateObj.getDate()).padStart(2, '0');
return `${year}-${month}-${date}`;
}
async function getPages() {
let background = await runtimeGetBackgroundPage();
let pages = await background.getPages();
return pages;
}
async function setPages(pages) {
let background = await runtimeGetBackgroundPage();
await background.setPages(pages);
}
async function addPage(page) {
let background = await runtimeGetBackgroundPage();
await background.addPage(page);
}
async function deletePage(url) {
let background = await runtimeGetBackgroundPage();
await background.deletePage(url);
}
async function addToBookmark(page) {
let background = await runtimeGetBackgroundPage();
await background.addToBookmark(page);
}
function generateBookmark(page) {
return `<li class="bookmark-item" data-url="${page.url}">
<span class="bookmark-title">${page.title}</span>
<i class="bookmark-operator bookmark-operator-add"></i>
<i class="bookmark-operator bookmark-operator-delete"></i>
</li>`;
}
async function getPagesRecent() {
let now = today();
let pages = await getPages();
pages = pages.filter(p => p.notifyTime <= now);
return [{
pages
}];
}
async function getPagesByTime() {
let items = [];
let pages = await getPages();
pages.sort((a, b) => {
if (a.notifyTime > b.notifyTime) return 1;
if (a.notifyTime === b.notifyTime) return 0;
return -1;
});
let item;
let lastTime;
for (let page of pages) {
if (page.notifyTime !== lastTime) {
if (item) {
items.push(item);
}
lastTime = page.notifyTime;
item = {
title: getDateString(lastTime),
pages: []
}
}
item.pages.push(page)
}
if (item) {
items.push(item);
}
return items;
}
async function getPagesByType() {
let items = [];
let pages = await getPages();
let typeObj = {};
for (let page of pages) {
let obj = typeObj[page.type] || [];
obj.push(page);
typeObj[page.type] = obj;
}
for (let type in typeObj) {
items.push({
title: type,
pages: typeObj[type]
});
}
return items;
}
async function generateBookmarks() {
let type = $('.list-type-item.is-current').data('type');
let list;
if (type === 'time') {
list = await getPagesByTime();
} else if (type === 'type') {
list = await getPagesByType();
} else {
list = await getPagesRecent();
}
$('#bookmarks-container').empty();
for (let item of list) {
let section = $('<div class="bookmarks-section"></div>');
if (item.title) {
$(`<h2 class="bookmarks-section-title">${item.title}</h2>`).appendTo(section);
}
let ul = $('<ul class="bookmarks-group"></ul>');
for (let page of item.pages) {
$(`${generateBookmark(page)}`).appendTo(ul);
}
$(ul).appendTo(section);
$(section).appendTo($('#bookmarks-container'));
}
}
function toggleContentArea() {
$('.tab').each(function () {
let id = $(this).attr('id');
let isCurrent = $(this).hasClass('is-current');
let contentId = `#${id}-content`;
if (isCurrent) {
$(contentId).show();
if (contentId === '#tab-reading-content') {
toggleTypeListArea(false);
toggleBookmarkType();
}
} else {
$(contentId).hide();
}
});
}
function toggleTypeListArea(show) {
if (show) {
$('#type-selector').removeClass('triangle-down');
$('#type-selector').addClass('triangle-up');
$('#list-type-list').show();
} else {
$('#type-selector').removeClass('triangle-up');
$('#type-selector').addClass('triangle-down');
$('#list-type-list').hide();
}
}
async function toggleBookmarkType() {
let type = $('.list-type-item.is-current').data('type');
$('#type-name-span').text($('.list-type-item.is-current').text());
await generateBookmarks();
}
function initAddContent() {
$('#notify-time').val(getDateString(Date.now() + 7 * 24 * 60 * 60 * 1000));
$('#end-time').val(getDateString(Date.now() + 14 * 24 * 60 * 60 * 1000));
}
$('#tab-div').on('click', '.tab', async function (eve) {
let targetId = $(this).attr('id');
$('.tab').each(function () {
let id = $(this).attr('id');
if (id === targetId) {
$(this).addClass('is-current');
} else {
$(this).removeClass('is-current');
}
});
toggleContentArea();
});
$('#add-button').on('click', async function () {
let background = await runtimeGetBackgroundPage();
let tabsList = background.tabsList;
log('clicked');
log(tabsList);
let timestamp = Date.now();
let type = $('#type').val() || '';
let notifyTime = $('#notify-time').val();
if (notifyTime) {
notifyTime = new Date(notifyTime).getTime();
} else {
notifyTime = today() + 7 * 24 * 60 * 60 * 1000;
}
let endTime = $('#end-time').val();
if (endTime) {
endTime = new Date(endTime).getTime();
} else {
endTime = today() + 14 * 24 * 60 * 60 * 1000;
}
let addAll = $('#add-all').is(':checked');
let addTime = Date.now();
let tabs;
if (addAll) {
tabs = await tabsQuery({ currentWindow: true });
} else {
tabs = await tabsQuery({ active: true, currentWindow: true });
}
for (let tab of tabs) {
if (tab.incognito) {
continue;
}
if (tab.url.match(/^chrome/gi)) {
continue;
}
let {
id,
windowId,
url,
title
} = tab;
let tData = tabsList.find(t => t.tabId === id && t.windowId === windowId) || {};
await addPage({
title,
url,
scrollTop: tData.scrollTop,
type,
notifyTime,
addTime,
endTime,
tag: timestamp
});
}
$('#tab-reading').click();
});
$('#type-selector').on('click', function () {
let isShown = $(this).hasClass('triangle-up');
toggleTypeListArea(!isShown);
});
$('#list-type-list').on('click', '.list-type-item', function () {
let type = $(this).data('type');
$('.list-type-item').each(function () {
let cType = $(this).data('type');
if (type === cType) {
$(this).addClass('is-current');
} else {
$(this).removeClass('is-current');
}
});
toggleTypeListArea(false);
toggleBookmarkType();
});
$('#bookmarks-container').on('click', '.bookmark-title', async function () {
let url = $(this).parent().data('url');
await tabsCreate({
url
});
});
$('#bookmarks-container').on('click', '.bookmark-operator-delete', async function () {
let url = $(this).parent().data('url');
await deletePage(url);
await generateBookmarks();
});
$('#bookmarks-container').on('click', '.bookmark-operator-add', async function () {
let url = $(this).parent().data('url');
let pages = await getPages();
let page = pages.find(p => p.url === url);
await addToBookmark(page);
await generateBookmarks();
});
async function initFunc() {
toggleContentArea();
initAddContent();
}
initFunc();