-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
211 lines (188 loc) · 7.02 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
document.addEventListener("DOMContentLoaded", function () {
// Get the current tab's URL
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
let currentUrl = tabs[0].url;
// Populate the URL input field with the current URL
document.getElementById("url-input").value = currentUrl;
});
// Display stored URLs
displayStoredURLs();
// Handle form submission
document
.getElementById("reminder-form")
.addEventListener("submit", function (event) {
event.preventDefault(); // Prevent default form submission
// Get the form values
let url = document.getElementById("url-input").value;
// Save the reminder
saveReminder(url);
});
// Handle click event on delete button
document
.getElementById("delete-storage")
.addEventListener("click", function (event) {
event.preventDefault(); // Prevent default form submission
event.stopPropagation(); // Stop event propagation to prevent triggering other event listeners
// Clear local storage
chrome.storage.local.remove("reminders", function () {
// Refresh displayed URLs
displayStoredURLs();
});
});
});
// Function to snooze the notification
function snoozeNotification() {
// Close the current notification
chrome.notifications.clear(null);
// Create a new notification after 30 minutes
setTimeout(function () {
showNotification("Reminder to visit", "Snoozed notification.");
}, 30 * 60 * 1000); // 30 minutes in milliseconds
}
// Function to display stored URLs
function displayStoredURLs() {
chrome.storage.local.get("reminders", function (data) {
let storedURLs = data.reminders || [];
// Sort reminders by reminderTime
storedURLs.sort((a, b) => a.reminderTime - b.reminderTime);
let urlList = document.getElementById("url-list");
urlList.innerHTML = ""; // Clear the existing list
storedURLs.forEach(function (reminder, index) {
// Pass index
let reminderItem = createReminderItem(reminder, index); // Pass index
urlList.appendChild(reminderItem);
});
// Toggle the visibility of the reminder-list-container based on whether there are any reminders
let reminderListContainer = document.getElementById(
"reminder-list-container"
);
reminderListContainer.style.display =
storedURLs.length > 0 ? "block" : "none";
});
}
function createReminderItem(reminder, index) {
// Create elements
let li = document.createElement("li");
let serialNumberSpan = document.createElement("span"); // Add span for serial number
let websiteSpan = document.createElement("span");
let dateTimeSpan = document.createElement("span");
let deleteButton = document.createElement("button");
let deleteIcon = document.createElement("i");
// Set content and attributes
serialNumberSpan.textContent = index + 1 + ". "; // Set serial number
// Extract the domain name from the URL
let domain = extractDomain(reminder.url);
websiteSpan.textContent = domain;
dateTimeSpan.textContent = new Date(reminder.reminderTime).toLocaleString();
deleteIcon.classList.add("fas", "fa-times"); // Font Awesome icon class
deleteButton.appendChild(deleteIcon);
deleteButton.classList.add("delete-button");
deleteButton.addEventListener("click", function (event) {
event.stopPropagation(); // Stop event propagation to prevent triggering other event listeners
deleteReminder(reminder);
});
// Append elements
li.appendChild(serialNumberSpan); // Append serial number
li.appendChild(websiteSpan);
li.appendChild(dateTimeSpan);
li.appendChild(deleteButton);
// Add classes for styling
li.classList.add("reminder-item");
serialNumberSpan.classList.add("serial-number"); // Add class for serial number
websiteSpan.classList.add("website");
dateTimeSpan.classList.add("reminder-time");
// Add click event listener to the reminder item
li.addEventListener("click", function () {
event.stopPropagation();
openUrlInNewTab(reminder.url);
});
return li;
}
// Function to open URL in a new tab
function openUrlInNewTab(url) {
chrome.tabs.create({ url: url });
}
// Delete reminder
function deleteReminder(reminder) {
// Get the stored reminders
chrome.storage.local.get("reminders", function (data) {
let reminders = data.reminders || [];
// Find the index of the reminder to delete
let index = reminders.findIndex(
(r) => r.url === reminder.url && r.reminderTime === reminder.reminderTime
);
if (index !== -1) {
// Remove the reminder from the array
reminders.splice(index, 1);
// Update the reminders in storage
chrome.storage.local.set({ reminders: reminders }, function () {
// Refresh displayed URLs
displayStoredURLs();
});
}
});
}
// Function to save a reminder
function saveReminder(url) {
// Get the selected date and time from the datetime picker
let reminderTime = new Date(document.getElementById("reminder-time").value);
// Check if a valid date and time are selected
if (!isNaN(reminderTime)) {
// Convert reminder time to Unix timestamp (milliseconds)
let reminderTimeUnix = reminderTime.getTime();
// Save the reminder data in chrome.storage.local
chrome.storage.local.get("reminders", function (data) {
let reminders = data.reminders || [];
reminders.push({
url: url,
reminderTime: reminderTimeUnix, // Save as Unix timestamp (milliseconds)
});
chrome.storage.local.set({ reminders: reminders }, function () {
// Display the updated list of stored URLs
displayStoredURLs();
});
});
} else {
// Display an error message if no date and time are selected
alert("Please select a valid date and time for the reminder.");
}
}
// Function to extract domain from URL
function extractDomain(url) {
let domain;
// Find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split("/")[2];
} else {
domain = url.split("/")[0];
}
// Find & remove port number
domain = domain.split(":")[0];
// Find & remove "?"
domain = domain.split("?")[0];
// Remove "www." prefix if present
if (domain.startsWith("www.")) {
domain = domain.substring(4);
}
return domain;
}
document.addEventListener("DOMContentLoaded", function () {
// Toggle visibility of reminders list when button is clicked
document
.getElementById("toggle-reminders")
.addEventListener("click", function () {
var remindersList = document.getElementById("url-list");
remindersList.classList.toggle("hidden");
var icon = document.querySelector("#toggle-reminders i");
icon.classList.toggle("fa-chevron-down");
icon.classList.toggle("fa-chevron-up");
});
// Set initial icon based on initial visibility state
var icon = document.querySelector("#toggle-reminders i");
var remindersList = document.getElementById("url-list");
if (!remindersList.classList.contains("hidden")) {
icon.classList.add("fa-chevron-up");
} else {
icon.classList.add("fa-chevron-down");
}
});