-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidepanel.js
341 lines (272 loc) · 9.7 KB
/
sidepanel.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
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
console.log("sidepanel.js is loaded");
// the current active session
let activeSession = undefined;
let dropdown = undefined;
const defaultSessionSchema = {
id: Date.now(),
sessionName: "",
currentSession: 1,
startDate: new Date(),
savedDate: new Date(),
bookmarkData: [],
contentData: [],
};
// start new session
document.addEventListener("DOMContentLoaded", function onDOMContentLoaded() {
console.log("onDOMContentLoaded");
dropdown = document.getElementById("sessionDropdown");
dropdown.addEventListener("change", function () {
const selectedSessionId = parseInt(this.value); // Get the selected value from the dropdown
updateSessionView(selectedSessionId);
});
updateSessionName();
reloadSessionDropDown();
newSession();
});
function newSession() {
// reset active Session
const loadedNewSessionAt = new Date();
activeSession = {
id: Date.now(),
sessionName: "",
currentSession: 1,
startDate: loadedNewSessionAt,
savedDate: loadedNewSessionAt,
bookmarkData: [],
contentData: [],
};
// parse date and time
const dateOptions = { year: "2-digit", month: "2-digit", day: "2-digit" };
const timeOptions = { hour: "2-digit", minute: "2-digit", second: "2-digit" };
const formattedDate = loadedNewSessionAt.toLocaleDateString(
undefined,
dateOptions
);
const formattedTime = loadedNewSessionAt.toLocaleTimeString(
undefined,
timeOptions
);
const sessionNameDiv = document.getElementById("sessionName");
sessionNameDiv.innerText = `Session Name ${formattedDate} - ${formattedTime}`;
const startDateDiv = document.getElementById("startDate");
startDateDiv.innerHTML = `Session started at ${formattedDate} - ${formattedTime}.`;
// clear timeline
const timelineDiv = document.getElementById("timeline");
timelineDiv.innerHTML = "";
}
function renderBottomToolbar(render = true) {
bottom.innerHTML = "";
if (render === true) {
// add last saved div
const saveDateDiv = document.createElement("div");
saveDateDiv.setAttribute("id", "saveDate");
// add session save button
const saveButton = document.createElement("button");
saveButton.setAttribute("id", "saveButton");
saveButton.appendChild(document.createTextNode("Save"));
saveButton.setAttribute("class", "button-image");
saveButton.addEventListener("click", saveSession);
// and save and close button
const saveAndCloseButton = document.createElement("button");
saveAndCloseButton.setAttribute("id", "saveAndClose");
saveAndCloseButton.appendChild(document.createTextNode("Save + Close"));
saveAndCloseButton.setAttribute("class", "button-image");
saveAndCloseButton.addEventListener("click", saveAndCloseSession);
// delete
const deleteButton = document.createElement("button");
deleteButton.setAttribute("id", "delete");
deleteButton.appendChild(document.createTextNode("Delete"));
deleteButton.setAttribute("class", "button-image");
deleteButton.addEventListener("click", deleteSession);
const bottom = document.getElementById("bottom");
bottom.appendChild(saveDateDiv);
bottom.appendChild(saveButton);
bottom.appendChild(saveAndCloseButton);
bottom.appendChild(deleteButton);
}
}
function reloadSessionDropDown() {
chrome.runtime.sendMessage({ action: "getAllSessions" }, (response) => {
console.log(response.sessions);
if (response.sessions != undefined) {
if (response.sessions.length > 0) {
// remove children, if any, from earlier
if (dropdown.options.length > 0) {
for (i = dropdown.options.length; i >= 0; i--) {
dropdown.remove(i);
}
}
const option = document.createElement("option");
option.value = -1;
option.textContent = "Select other session";
dropdown.appendChild(option);
response.sessions.forEach((session) => {
const option = document.createElement("option");
option.value = session.id;
option.textContent = session.sessionName;
dropdown.appendChild(option);
});
// set dropdown to current active session id
document.querySelector("#sessionDropdown").value = activeSession.id;
dropdown.hidden = false;
} else {
dropdown.hidden = true;
}
}
});
}
function updateSessionView(selectedSessionId) {
console.log(selectedSessionId);
// IndexedDB session ids must be positive integers, so using negative here to know default label chosen
if (selectedSessionId === -1) {
return;
}
// update and save current session due to it no longer being active session
saveSession(0);
chrome.runtime.sendMessage(
{ action: "getSessionById", data: selectedSessionId },
(response) => {
//console.log(response.session)
const session = response.session;
// rerender timeline, bottomToolBar
const startDateDiv = document.getElementById("startDate");
startDateDiv.innerHTML = `Session started at ${session.startDate}.`;
activeSession = session;
activeSession.currentSession = 1;
document.getElementById("sessionName").innerText =
activeSession.sessionName;
rerenderTimeline(activeSession);
renderBottomToolbar(true);
}
);
}
function saveSession(active = 0) {
if (activeSession.bookmarkData.length || activeSession.contentData.length) {
// get current sessionName, update dateSaved, etc.
activeSession.savedDate = new Date();
activeSession.sessionName =
document.getElementById("sessionName").innerText;
// update UI
const saveDateDiv = document.getElementById("saveDate");
saveDateDiv.innerHTML = "Last saved at " + activeSession.savedDate;
chrome.runtime.sendMessage({ action: "saveSession", data: activeSession });
showToast("Saving session.");
// update session selector
reloadSessionDropDown();
}
}
function saveAndCloseSession() {
if (activeSession.bookmarkData.length || activeSession.contentData.length) {
// get current sessionName, update dateSaved, etc.
activeSession.savedDate = new Date();
activeSession.sessionName =
document.getElementById("sessionName").innerText;
// update UI
const saveDateDiv = document.getElementById("saveDate");
saveDateDiv.innerHTML = "Last saved at " + activeSession.savedDate;
// set currentSession = 0
activeSession.currentSession = 0;
chrome.runtime.sendMessage({ action: "saveSession", data: activeSession });
// update session selector, and bottomToolBar
reloadSessionDropDown();
renderBottomToolbar(false);
// close session
newSession();
showToast("Saving and closing session.");
}
}
function deleteSession() {
const id = activeSession.id;
chrome.runtime.sendMessage({ action: "deleteSession", id: id });
newSession();
reloadSessionDropDown();
renderBottomToolbar(false);
showToast("Deleting session.");
}
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.action === "contentCapture") {
// disallow duplicates
const isDuplicate = activeSession.contentData.some(
(item) =>
item.url === message.data.url &&
item.selectedText === message.data.selectedText &&
item.type === "content"
);
if (isDuplicate === true) {
return;
}
// append messages to sessionData, will later save when session complete
message.data.dateAdded = Date.now();
activeSession.contentData.push(message.data);
// rerender timeline, bottomToolBar
rerenderTimeline(activeSession);
renderBottomToolbar(true);
} else if (message.action === "bookmarkCreated") {
bookmark = message.data[0];
// disallow duplicates, check for bookmark type only
const isDuplicate = activeSession.bookmarkData.some(
(item) => item.url === bookmark.url && item.type === "bookmark"
);
if (isDuplicate === true) {
return;
}
// append messages to sessionData, will later save when session complete
bookmark.dateAdded = Date.now();
activeSession.bookmarkData.push(bookmark);
// rerender timeline, bottomToolBar
rerenderTimeline(activeSession);
renderBottomToolbar();
} else if (message.action === "setActiveSession") {
console.log(message.data);
activeSession = message.data;
}
});
function showToast(message) {
const toastContainer = document.getElementById("toast-container");
const toast = document.getElementById("toast");
// Set the toast message
toast.textContent = message;
// Show the toast
toastContainer.style.display = "block";
// Trigger fade in after a short delay
setTimeout(() => {
toast.style.opacity = "1";
}, 100);
// Hide the toast and reset opacity after 5 seconds
setTimeout(() => {
toast.style.opacity = "0";
setTimeout(() => {
toastContainer.style.display = "none";
}, 500); // Wait for fade out animation to complete
}, 5000);
}
function updateSessionName() {
sessionNameDiv = document.getElementById("sessionName");
sessionNameDiv.addEventListener("click", () => {
sessionNameDiv.contentEditable = "true";
sessionNameDiv.focus();
});
sessionNameDiv.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault();
sessionNameDiv.contentEditable = "false";
// Sanitize and set the content
sessionNameDiv.innerHTML = sanitizeInput(sessionNameDiv.textContent);
}
});
}
// Sanitize user input
function sanitizeInput(input) {
// Replace potentially harmful characters with safe ones
return input.replace(/[&<>"'\/]/g, (char) => {
const replacements = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/",
};
return replacements[char];
});
}