-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
60 lines (55 loc) · 1.96 KB
/
Copy pathcommon.js
File metadata and controls
60 lines (55 loc) · 1.96 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
function updateClockAndDate() {
const now = new Date();
document.getElementById('clock').textContent = now.toLocaleTimeString([], {hour: '2-digit', minute: '2-digit', second: '2-digit'});
document.getElementById('date').textContent = now.toLocaleDateString([], {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'});
}
function addTask(taskInput, taskTime, taskPriority, callback) {
const task = {
id: Date.now(),
text: taskInput,
time: new Date(taskTime).getTime(),
priority: taskPriority
};
chrome.storage.sync.get({tasks: []}, function(data) {
const tasks = data.tasks;
tasks.push(task);
chrome.storage.sync.set({tasks: tasks}, function() {
if (callback) callback();
scheduleNotification(task);
});
});
}
function displayTasks(taskListElement) {
chrome.storage.sync.get({tasks: []}, function(data) {
taskListElement.innerHTML = '';
data.tasks.forEach(task => {
const li = document.createElement('li');
li.className = 'task';
li.innerHTML = `
${task.text} at ${new Date(task.time).toLocaleString()} (Priority: ${task.priority})
<button class="delete-btn" data-id="${task.id}" aria-label="Delete task">X</button>
`;
taskListElement.appendChild(li);
});
document.querySelectorAll('.delete-btn').forEach(button => {
button.addEventListener('click', function() {
const taskId = parseInt(this.getAttribute('data-id'));
deleteTask(taskId, function() {
displayTasks(taskListElement);
});
});
});
});
}
function deleteTask(taskId, callback) {
chrome.storage.sync.get({tasks: []}, function(data) {
const tasks = data.tasks.filter(task => task.id !== taskId);
chrome.storage.sync.set({tasks: tasks}, function() {
if (callback) callback();
chrome.alarms.clear(taskId.toString());
});
});
}
function scheduleNotification(task) {
chrome.alarms.create(task.id.toString(), {when: task.time});
}