-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
122 lines (108 loc) · 3.91 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
document.addEventListener("DOMContentLoaded", function () {
const todoForm = document.getElementById("todo-form");
const todoInput = document.getElementById("todo-input");
const dateInput = document.getElementById("date-input");
const timeInput = document.getElementById("time-input");
const todoList = document.getElementById("todo-list");
const clearCompletedBtn = document.getElementById("clear-completed");
const alertsContainer = document.getElementById("alerts");
// Retrieve tasks from storage
chrome.storage.sync.get("tasks", function (data) {
const savedTasks = data.tasks || [];
savedTasks.forEach(function (task) {
addTaskToDOM(task.text, task.date, task.time, task.completed);
});
});
todoForm.addEventListener("submit", function (event) {
event.preventDefault();
const taskText = todoInput.value.trim();
const taskDate = dateInput.value.trim();
const taskTime = timeInput.value.trim();
if (taskText !== "") {
const task = {
text: taskText,
date: taskDate,
time: taskTime,
completed: false
};
addTaskToDOM(taskText, taskDate, taskTime, false);
saveTask(task);
todoInput.value = "";
dateInput.value = "";
timeInput.value = "";
}
});
function addTaskToDOM(text, date, time, completed) {
const listItem = document.createElement("li");
listItem.classList.add("todo-item");
if (completed) {
listItem.classList.add("completed");
}
const taskText = document.createElement("span");
taskText.classList.add("todo-text");
taskText.textContent = text;
const taskDateTime = document.createElement("span");
taskDateTime.classList.add("todo-datetime");
taskDateTime.textContent = `${date} ${time}`;
const deleteButton = document.createElement("button");
deleteButton.classList.add("button-delete");
deleteButton.textContent = "X";
listItem.appendChild(taskText);
listItem.appendChild(taskDateTime);
listItem.appendChild(deleteButton);
todoList.appendChild(listItem);
// Add event listener to mark task as completed
listItem.addEventListener("click", function (event) {
if (event.target === deleteButton) {
deleteTask(listItem);
} else {
listItem.classList.toggle("completed");
const taskIndex = Array.from(todoList.children).indexOf(listItem);
updateTaskStatus(taskIndex, !completed);
}
});
}
function saveTask(task) {
chrome.storage.sync.get("tasks", function (data) {
const savedTasks = data.tasks || [];
savedTasks.push(task);
chrome.storage.sync.set({ tasks: savedTasks });
});
}
function updateTaskStatus(index, completed) {
chrome.storage.sync.get("tasks", function (data) {
const savedTasks = data.tasks || [];
savedTasks[index].completed = completed;
chrome.storage.sync.set({ tasks: savedTasks });
});
}
function deleteTask(listItem) {
const taskIndex = Array.from(todoList.children).indexOf(listItem);
chrome.storage.sync.get("tasks", function (data) {
const savedTasks = data.tasks || [];
savedTasks.splice(taskIndex, 1);
chrome.storage.sync.set({ tasks: savedTasks });
listItem.remove();
});
}
clearCompletedBtn.addEventListener("click", function () {
chrome.storage.sync.get("tasks", function (data) {
const savedTasks = data.tasks || [];
const updatedTasks = savedTasks.filter(function (task) {
return !task.completed;
});
chrome.storage.sync.set({ tasks: updatedTasks });
todoList.innerHTML = "";
updatedTasks.forEach(function (task) {
addTaskToDOM(task.text, task.date, task.time, task.completed);
});
});
});
// Load Theme Preference
// Retrieve theme preference from storage
chrome.storage.sync.get("theme", function (data) {
if (data.theme === "dark") {
document.body.classList.add("dark-mode");
}
});
});