-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathpopup.js
More file actions
463 lines (381 loc) · 14.3 KB
/
popup.js
File metadata and controls
463 lines (381 loc) · 14.3 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
const snoozeListDiv = document.getElementById("snooze-list");
const customModal = document.getElementById("customModal");
const recurringModal = document.getElementById("recurringModal");
function getSmartSnoozeOptions() {
const now = new Date();
const hour = now.getHours();
const day = now.getDay(); // 0 is Sunday, 6 is Saturday
const options = [];
// Always show quick options
options.push({
id: 'snooze10min',
text: 'Snooze for 10 minutes',
hours: 0.17
});
options.push({
id: 'snooze1hour',
text: 'Snooze for 1 hour',
hours: 1
});
// Context-aware options
if (hour < 12) {
// Morning
options.push({
id: 'snoozeAfternoon',
text: 'Snooze until 2 PM',
hours: getHoursUntil(14, 0)
});
} else if (hour < 17) {
// Afternoon
options.push({
id: 'snoozeEvening',
text: 'Snooze until this evening (6 PM)',
hours: getHoursUntil(18, 0)
});
} else {
// Evening/Night
options.push({
id: 'snoozeNextMorning',
text: 'Snooze until tomorrow morning (9 AM)',
hours: getHoursUntil(9, 0, 1)
});
}
// Weekend options
if (day < 5) { // Monday-Friday
options.push({
id: 'snoozeWeekend',
text: 'Snooze until Saturday morning',
hours: getHoursUntilNextDay(6, 10, 0) // Saturday at 10 AM
});
} else {
options.push({
id: 'snoozeNextWeek',
text: 'Snooze until Monday morning',
hours: getHoursUntilNextDay(1, 9, 0) // Monday at 9 AM
});
}
return options;
}
function getHoursUntil(targetHour, targetMinute, addDays = 0) {
const now = new Date();
const target = new Date(now);
target.setHours(targetHour, targetMinute, 0, 0);
if (addDays > 0 || target <= now) {
target.setDate(target.getDate() + (addDays || 1));
}
return (target - now) / (1000 * 60 * 60);
}
function getHoursUntilNextDay(targetDay, targetHour, targetMinute) {
const now = new Date();
const target = new Date(now);
target.setHours(targetHour, targetMinute, 0, 0);
while (target.getDay() !== targetDay || target <= now) {
target.setDate(target.getDate() + 1);
}
return (target - now) / (1000 * 60 * 60);
}
function setupModalHandlers() {
// Custom datetime picker handlers
document.getElementById("snoozeCustom").addEventListener("click", () => {
const dateTimeInput = document.getElementById("customDateTime");
// Set min datetime to now
const now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
dateTimeInput.min = now.toISOString().slice(0, 16);
// Set default value to tomorrow same time
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setMinutes(tomorrow.getMinutes() - tomorrow.getTimezoneOffset());
dateTimeInput.value = tomorrow.toISOString().slice(0, 16);
customModal.style.display = "block";
});
// Recurring snooze handlers
document.getElementById("snoozeRecurring").addEventListener("click", () => {
const timeInput = document.getElementById("recurringTime");
timeInput.value = "09:00";
recurringModal.style.display = "block";
});
// Handle modal buttons
document.querySelectorAll(".modal-cancel").forEach(button => {
button.addEventListener("click", () => {
customModal.style.display = "none";
recurringModal.style.display = "none";
});
});
// Handle custom datetime confirmation
document.querySelector("#customModal .modal-confirm").addEventListener("click", async () => {
const dateTimeInput = document.getElementById("customDateTime");
const selectedTime = new Date(dateTimeInput.value).getTime();
if (selectedTime <= Date.now()) {
alert("Please select a future date and time.");
return;
}
const hoursFromNow = (selectedTime - Date.now()) / (1000 * 60 * 60);
await snoozeTab(hoursFromNow);
customModal.style.display = "none";
});
// Handle recurring snooze confirmation
document.querySelector("#recurringModal .modal-confirm").addEventListener("click", handleRecurringSnooze);
}
async function handleRecurringSnooze() {
const timeInput = document.getElementById("recurringTime");
const selectedDays = Array.from(document.querySelectorAll(".days-selector input:checked"))
.map(checkbox => parseInt(checkbox.value));
if (selectedDays.length === 0) {
alert("Please select at least one day for recurring snooze.");
return;
}
const [hours, minutes] = timeInput.value.split(":").map(Number);
const [currentTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!currentTab || !currentTab.url) {
alert("No active tab found to snooze.");
return;
}
const recurringConfig = {
url: currentTab.url,
title: currentTab.title,
time: timeInput.value,
days: selectedDays,
type: "recurring"
};
const configId = `recurring-${Date.now()}`;
await chrome.storage.local.set({
[configId]: recurringConfig
});
const nextTime = getNextOccurrence(hours, minutes, selectedDays);
const alarmName = `snooze-${currentTab.id}-${nextTime.getTime()}`;
await chrome.storage.local.set({
[alarmName]: {
url: currentTab.url,
snoozeTime: nextTime.getTime(),
title: currentTab.title,
recurringId: configId
}
});
chrome.alarms.create(alarmName, { when: nextTime.getTime() });
chrome.tabs.remove(currentTab.id);
recurringModal.style.display = "none";
alert("Recurring snooze set successfully!");
}
function updateSnoozeGrid() {
const snoozeGrid = document.querySelector('.snooze-grid');
snoozeGrid.innerHTML = ''; // Clear existing buttons
const options = getSmartSnoozeOptions();
options.forEach(option => {
const button = document.createElement('button');
button.id = option.id;
button.textContent = option.text;
button.addEventListener('click', () => snoozeTab(option.hours));
snoozeGrid.appendChild(button);
});
// Add custom and recurring options
const customButton = document.createElement('button');
customButton.id = 'snoozeCustom';
customButton.className = 'custom-snooze';
customButton.textContent = 'Pick Date & Time';
snoozeGrid.appendChild(customButton);
const recurringButton = document.createElement('button');
recurringButton.id = 'snoozeRecurring';
recurringButton.className = 'custom-snooze';
recurringButton.textContent = 'Set Recurring Snooze';
snoozeGrid.appendChild(recurringButton);
// Reattach event listeners for custom and recurring buttons
setupModalHandlers();
}
// Initialize when the popup opens
document.addEventListener('DOMContentLoaded', () => {
updateSnoozeGrid();
listSnoozed();
});
// Update the snooze grid every minute to keep times current
setInterval(updateSnoozeGrid, 60000);
async function snoozeTab(hours) {
const [currentTab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!currentTab || !currentTab.url) {
alert("No active tab found to snooze.");
return;
}
const snoozeTime = Date.now() + hours * 60 * 60 * 1000; // Calculate snooze time in milliseconds
const alarmName = `snooze-${currentTab.id}-${snoozeTime}`;
// Save tab details in local storage
await chrome.storage.local.set({
[alarmName]: { url: currentTab.url, snoozeTime, title: currentTab.title },
});
// Create an alarm
chrome.alarms.create(alarmName, { when: snoozeTime });
// Close the current tab
chrome.tabs.remove(currentTab.id);
alert(`Tab snoozed for ${hours} hours.`);
}
async function removeSnooze(alarmName) {
const item = await chrome.storage.local.get(alarmName);
const removeModal = document.getElementById("removeModal");
const removeRecurringModal = document.getElementById("removeRecurringModal");
if (item[alarmName]?.recurringId) {
// Handle recurring snooze removal
const modal = removeRecurringModal;
modal.style.display = "block";
return new Promise((resolve) => {
const handleClick = async (action) => {
modal.style.display = "none";
if (action === 'cancel') {
resolve();
return;
}
// First clear the alarm and remove storage entries
await chrome.alarms.clear(alarmName);
await chrome.storage.local.remove(alarmName);
if (action === 'removeAll' || action === 'removeAllAndOpen') {
await chrome.storage.local.remove(item[alarmName].recurringId);
}
// Then open the tab if requested
if (action === 'removeAllAndOpen' || action === 'removeSingleAndOpen') {
await chrome.tabs.create({ url: item[alarmName].url });
}
listSnoozed();
resolve();
};
modal.querySelector('.modal-confirm').onclick = () => handleClick('removeAllAndOpen');
modal.querySelector('.modal-remove-only').onclick = () => handleClick('removeAll');
modal.querySelector('.modal-remove-single').onclick = () => handleClick('removeSingleAndOpen');
modal.querySelector('.modal-cancel').onclick = () => handleClick('cancel');
});
} else {
// Handle regular snooze removal
const modal = removeModal;
modal.style.display = "block";
return new Promise((resolve) => {
const handleClick = async (action) => {
modal.style.display = "none";
if (action === 'cancel') {
resolve();
return;
}
// First clear the alarm and remove storage entry
await chrome.alarms.clear(alarmName);
await chrome.storage.local.remove(alarmName);
// Then open the tab if requested
if (action === 'removeAndOpen') {
await chrome.tabs.create({ url: item[alarmName].url });
}
listSnoozed();
resolve();
};
modal.querySelector('.modal-confirm').onclick = () => handleClick('removeAndOpen');
modal.querySelector('.modal-remove-only').onclick = () => handleClick('removeOnly');
modal.querySelector('.modal-cancel').onclick = () => handleClick('cancel');
});
}
}
function listSnoozed() {
// first clear the list
snoozeListDiv.innerHTML = "";
chrome.storage.local.get(null, (items) => {
// Create tabs container
const tabsContainer = document.createElement("div");
tabsContainer.className = "tabs-container";
snoozeListDiv.appendChild(tabsContainer);
// Count regular and recurring snoozes
const regularSnoozes = Object.entries(items).filter(([key, value]) =>
key.startsWith("snooze-") && !value.recurringId
);
const recurringSnoozes = Object.entries(items).filter(([key, value]) =>
key.startsWith("snooze-") && value.recurringId
);
// Create tab buttons
const tabButtons = document.createElement("div");
tabButtons.className = "tab-buttons";
tabButtons.innerHTML = `
<button class="tab-button active" data-tab="regular">
One-time
<span class="count">${regularSnoozes.length}</span>
</button>
<button class="tab-button" data-tab="recurring">
Recurring
<span class="count">${recurringSnoozes.length}</span>
</button>
`;
tabsContainer.appendChild(tabButtons);
// Create tab content containers
const regularTabContent = document.createElement("div");
regularTabContent.className = "tab-content active";
regularTabContent.id = "regular-tab";
const recurringTabContent = document.createElement("div");
recurringTabContent.className = "tab-content";
recurringTabContent.id = "recurring-tab";
// Add empty states
if (regularSnoozes.length === 0) {
regularTabContent.innerHTML = `
<div class="empty-state">
<p>No snoozed tabs</p>
<p class="empty-subtitle">Choose a time above to snooze your current tab</p>
</div>
`;
}
if (recurringSnoozes.length === 0) {
recurringTabContent.innerHTML = `
<div class="empty-state">
<p>No recurring tabs</p>
<p class="empty-subtitle">Set up recurring snoozes for tabs you need regularly</p>
</div>
`;
}
// Add tab content
tabsContainer.appendChild(regularTabContent);
tabsContainer.appendChild(recurringTabContent);
// Add click handlers for tabs
tabButtons.querySelectorAll('.tab-button').forEach(button => {
button.addEventListener('click', () => {
tabButtons.querySelector('.active').classList.remove('active');
button.classList.add('active');
const tabId = button.dataset.tab;
tabsContainer.querySelectorAll('.tab-content').forEach(content => {
content.classList.remove('active');
});
tabsContainer.querySelector(`#${tabId}-tab`).classList.add('active');
});
});
// Populate tabs with items
const addItemToTab = (key, value, container) => {
const { url, snoozeTime, title, recurringId } = value;
const snoozeTimeStr = new Date(snoozeTime).toLocaleString();
const listItem = document.createElement("div");
listItem.className = "snoozed-tab";
const recurringIndicator = recurringId ? "🔄 " : "";
const data = title || url;
listItem.innerHTML = `
<span title="${data}" class="tab-info">
<div class="tab-title">${recurringIndicator}${data}</div>
<div class="tab-time">${snoozeTimeStr}</div>
</span>
`;
const removeButton = document.createElement("button");
removeButton.className = "remove-btn";
removeButton.innerHTML = '<span class="close-icon"></span>';
removeButton.title = "Remove snooze"; // Add tooltip
removeButton.addEventListener("click", () => removeSnooze(key));
listItem.appendChild(removeButton);
container.appendChild(listItem);
};
// Sort and add items to appropriate tabs
regularSnoozes
.sort(([,a], [,b]) => a.snoozeTime - b.snoozeTime)
.forEach(([key, value]) => addItemToTab(key, value, regularTabContent));
recurringSnoozes
.sort(([,a], [,b]) => a.snoozeTime - b.snoozeTime)
.forEach(([key, value]) => addItemToTab(key, value, recurringTabContent));
});
}
// Helper function to get next occurrence for recurring snooze
function getNextOccurrence(hours, minutes, selectedDays) {
const now = new Date();
const result = new Date();
result.setHours(hours, minutes, 0, 0);
if (result <= now) {
result.setDate(result.getDate() + 1);
}
while (!selectedDays.includes(result.getDay())) {
result.setDate(result.getDate() + 1);
}
return result;
}