-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
125 lines (107 loc) · 3.38 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
document.addEventListener("DOMContentLoaded", function () {
const timeInput = document.querySelector("#reminder-time");
const taskInput = document.querySelector("#task-input");
const reminderBtn = document.querySelector("#reminder-btn");
const contentBtn = document.querySelector("#content-btn");
const tasksContainer = document.querySelector(".tasks-container");
renderTasks();
reminderBtn.addEventListener("click", async () => {
const reminderTime = timeInput.value;
const task = taskInput.value;
if (reminderTime && task) {
const taskId = uid();
const taskObject = {
taskId,
time: reminderTime,
task,
};
saveTask(taskObject);
} else {
alert("Please enter both a reminder time and a task.");
}
});
contentBtn.addEventListener("click", () => {
chrome.storage.sync.get("tasks", function (data) {
const tasks = data.tasks || [];
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const currentTab = tabs[0];
const currentUrl = currentTab.url;
if (currentUrl.includes("github.com")) {
chrome.tabs.sendMessage(currentTab.id, {
action: "contentToInject",
textToInject: tasks,
});
}
});
});
});
function renderTasks() {
// Retrieve tasks from Chrome storage
chrome.storage.sync.get("tasks", function (data) {
const tasks = data.tasks || [];
// Check if there are tasks to display
if (tasks.length > 0) {
tasksContainer.style.display = "block"; // Show the container
tasksContainer.innerHTML = ""; // Clear previous tasks
// Loop through tasks and render each one
for (const taskObject of tasks) {
const taskElement = document.createElement("div");
taskElement.classList.add("task");
taskElement.innerHTML = `
<p><strong>Task:</strong> ${taskObject.task}</p>
<p><strong>Reminder Time:</strong> ${new Date(
taskObject.time
).toLocaleDateString(undefined, {
day: "numeric",
month: "short",
year: "numeric",
hour: "numeric",
minute: "numeric",
})}</p>
<button id="remove-btn">Remove</button>
`;
const removeButton =
taskElement.querySelector("#remove-btn");
removeButton.addEventListener("click", function () {
removeTask(taskObject.taskId);
});
tasksContainer.appendChild(taskElement);
}
} else {
tasksContainer.style.display = "none"; // Hide the container when no tasks
}
});
}
function removeTask(id) {
chrome.storage.sync.get("tasks", function (data) {
const tasks = data.tasks || [];
const updatedTasks = tasks.filter((task) => task.taskId !== id);
chrome.storage.sync.set({ tasks: updatedTasks }, function () {
renderTasks();
});
});
}
function saveTask(taskObject) {
chrome.storage.sync.get("tasks", function (data) {
const tasks = data.tasks || [];
const newTasks = [...tasks, taskObject];
chrome.storage.sync.set({ tasks: newTasks }, function () {
// Clear the input fields
timeInput.value = "";
taskInput.value = "";
renderTasks();
setAlarm(taskObject);
});
});
}
function setAlarm(taskObject) {
const alarmName = taskObject.taskId;
const reminderTime = new Date(taskObject.time).getTime();
chrome.alarms.create(alarmName, {
when: reminderTime,
});
}
function uid() {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
});