-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
273 lines (217 loc) · 9.43 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
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
import { getActiveTabURL } from "./utils.js";
import { getTime } from "./utils.js";
const showEditModal = (bookmark) => {
const editModal = document.getElementById("editModal");
const editNoteText = document.getElementById("editNoteText");
const container = document.getElementsByClassName("container")[0];
// Set the textarea value to the existing note text
editNoteText.value = bookmark.desc;
// Show the modal
editModal.style.display = "block";
// Increase the container size
container.classList.add("enlarged");
// Add the event listener to save the edited note
document.getElementById("saveEditNote").onclick = () => {
saveEditedNote(bookmark, editNoteText.value);
// Reset the container size
container.classList.remove("enlarged");
};
// Add the event listener to close the modal on click of the X button
document.getElementById("editModalClose").onclick = () => {
// Hide the modal
editModal.style.display = "none";
// Reset the container size
container.classList.remove("enlarged");
};
};
const saveEditedNote = async (bookmark, editedText) => {
// Update the bookmark's description with the edited text
bookmark.desc = editedText;
// Save the updated bookmark to the storage
const activeTab = await getActiveTabURL();
const currentVideo = new URLSearchParams(activeTab.url.split("?")[1]).get("v");
chrome.storage.sync.get([currentVideo], (data) => {
let currentBookmarks = data[currentVideo] ? JSON.parse(data[currentVideo]) : [];
// Update the note for the corresponding bookmark
for (let i = 0; i < currentBookmarks.length; i++) {
if (currentBookmarks[i].time === bookmark.time) {
currentBookmarks[i].desc = editedText;
break;
}
}
// Update the stored bookmarks with the new note
chrome.storage.sync.set({ [currentVideo]: JSON.stringify(currentBookmarks) }, () => {
console.log("Bookmark note updated.");
});
// Refresh the bookmarks view
viewBookmarks(currentBookmarks);
});
// Hide the modal
document.getElementById("editModal").style.display = "none";
};
const showFullNote = (bookmarkNoteElement, note) => {
const fullNote = document.createElement("div");
fullNote.innerHTML = note;
fullNote.className = "full-note";
fullNote.style.display = "none";
bookmarkNoteElement.appendChild(fullNote);
bookmarkNoteElement.addEventListener("click", () => {
if (fullNote.style.display === "none") {
fullNote.style.display = "block";
} else {
fullNote.style.display = "none";
}
});
};
const addNewBookmark = (bookmarksElement, bookmark) => {
const bookmarkTimestampElement = document.createElement("div");
const bookmarkNoteElement = document.createElement("div");
const newBookmarkElement = document.createElement("div");
const controlsElement = document.createElement("div");
bookmarkTimestampElement.textContent = getTime(bookmark.time);
bookmarkTimestampElement.className = "bookmark-timestamp";
bookmarkNoteElement.textContent = bookmark.desc;
bookmarkNoteElement.className = "bookmark-note";
showFullNote(bookmarkNoteElement, bookmark.desc);
controlsElement.className = "bookmark-controls";
setBookmarkAttributes("play", onPlay, controlsElement);
setBookmarkAttributes("edit", onEdit, controlsElement);
// Create a closure for the onDelete event listener
const onDeleteClosure = (e) => {
onDelete(e, bookmark.time);
};
setBookmarkAttributes("delete", onDeleteClosure, controlsElement);
newBookmarkElement.id = "bookmark-" + bookmark.time.toFixed(3);
newBookmarkElement.className = "bookmark";
newBookmarkElement.setAttribute("timestamp", bookmark.time);
newBookmarkElement.appendChild(bookmarkTimestampElement);
newBookmarkElement.appendChild(bookmarkNoteElement);
newBookmarkElement.appendChild(controlsElement);
bookmarksElement.appendChild(newBookmarkElement);
};
const viewBookmarks = (currentBookmarks = []) => {
const bookmarksElement = document.getElementById("bookmarks");
bookmarksElement.innerHTML = "";
const bookmarkElements = [];
if (currentBookmarks.length > 0) {
for (const bookmark of currentBookmarks) {
const newBookmarkElement = document.createElement("div");
const bookmarkTimestampElement = document.createElement("div");
const bookmarkNoteElement = document.createElement("div");
const controlsElement = document.createElement("div");
bookmarkTimestampElement.textContent = getTime(bookmark.time);
bookmarkTimestampElement.className = "bookmark-timestamp";
bookmarkNoteElement.textContent = bookmark.desc;
bookmarkNoteElement.className = "bookmark-note";
showFullNote(bookmarkNoteElement, bookmark.desc);
controlsElement.className = "bookmark-controls";
setBookmarkAttributes("play", onPlay, controlsElement);
setBookmarkAttributes("edit", onEdit, controlsElement);
// Create a closure for the onDelete event listener
const onDeleteClosure = (e) => {
onDelete(e, bookmark.time);
};
setBookmarkAttributes("delete", onDeleteClosure, controlsElement);
newBookmarkElement.id = "bookmark-" + bookmark.time.toFixed(3);
newBookmarkElement.className = "bookmark";
newBookmarkElement.setAttribute("timestamp", bookmark.time);
newBookmarkElement.appendChild(bookmarkTimestampElement);
newBookmarkElement.appendChild(bookmarkNoteElement);
newBookmarkElement.appendChild(controlsElement);
bookmarkElements.push(newBookmarkElement);
}
} else {
bookmarksElement.innerHTML = '<i class="row">No bookmarks to show.</i>';
}
// Append all bookmark elements to the container
for (const bookmarkElement of bookmarkElements) {
bookmarksElement.appendChild(bookmarkElement);
}
// Add "Delete all" button if there are at least 2 bookmarks
if (currentBookmarks.length >= 2) {
const deleteAllButton = document.createElement("button");
deleteAllButton.textContent = "Delete all";
deleteAllButton.id = "delete-all";
deleteAllButton.className = "delete-all-button";
bookmarksElement.appendChild(deleteAllButton);
deleteAllButton.addEventListener("click", onDeleteAll);
}
};
const onPlay = async e => {
const bookmarkTime = e.target.parentNode.parentNode.getAttribute("timestamp");
const activeTab = await getActiveTabURL();
chrome.tabs.sendMessage(activeTab.id, {
type: "PLAY",
value: bookmarkTime,
});
};
const onEdit = async e => {
const bookmarkTime = e.target.parentNode.parentNode.getAttribute("timestamp");
const activeTab = await getActiveTabURL();
const currentVideo = new URLSearchParams(activeTab.url.split("?")[1]).get("v");
chrome.storage.sync.get([currentVideo], (data) => {
let currentBookmarks = data[currentVideo] ? JSON.parse(data[currentVideo]) : [];
// Find the corresponding bookmark
const bookmarkToEdit = currentBookmarks.find(bookmark => bookmark.time === parseFloat(bookmarkTime));
if (bookmarkToEdit) {
showEditModal(bookmarkToEdit);
}
});
};
const onDelete = async (e, bookmarkTime) => {
const activeTab = await getActiveTabURL();
const queryParameters = activeTab.url.split("?")[1];
const urlParameters = new URLSearchParams(queryParameters);
const currentVideo = urlParameters.get("v");
const bookmarkElementToDelete = document.getElementById(
"bookmark-" + bookmarkTime.toFixed(3)
);
bookmarkElementToDelete.parentNode.removeChild(bookmarkElementToDelete);
// Fetch the stored bookmarks
chrome.storage.sync.get([currentVideo], (data) => {
let currentBookmarks = data[currentVideo] ? JSON.parse(data[currentVideo]) : [];
// Filter out the deleted bookmark
currentBookmarks = currentBookmarks.filter(bookmark => Math.abs(bookmark.time - bookmarkTime) > 0.001);
// Update the stored bookmarks
chrome.storage.sync.set({ [currentVideo]: JSON.stringify(currentBookmarks) }, () => {
console.log("Bookmark deleted and storage updated.");
});
});
chrome.tabs.sendMessage(activeTab.id, {
type: "DELETE",
value: bookmarkTime,
});
};
const onDeleteAll = async () => {
const activeTab = await getActiveTabURL();
const currentVideo = new URLSearchParams(activeTab.url.split("?")[1]).get("v");
// Clear all stored bookmarks for the current video
chrome.storage.sync.set({ [currentVideo]: JSON.stringify([]) }, () => {
console.log("All bookmarks deleted and storage updated.");
});
// Refresh the bookmarks view
viewBookmarks([]);
};
const setBookmarkAttributes = (src, eventListener, controlParentElement) => {
const controlElement = document.createElement("img");
controlElement.src = "assets/" + src + ".png";
controlElement.title = src;
controlElement.addEventListener("click", eventListener);
controlParentElement.appendChild(controlElement);
return eventListener;
};
document.addEventListener("DOMContentLoaded", async () => {
const activeTab = await getActiveTabURL();
const queryParameters = activeTab.url.split("?")[1];
const urlParameters = new URLSearchParams(queryParameters);
const currentVideo = urlParameters.get("v");
if (activeTab.url.includes("youtube.com/watch") && currentVideo) {
chrome.storage.sync.get([currentVideo], (data) => {
const currentVideoBookmarks = data[currentVideo] ? JSON.parse(data[currentVideo]) : [];
viewBookmarks(currentVideoBookmarks);
});
} else {
const container = document.getElementsByClassName("container")[0];
container.innerHTML = '<div class="title">This is not a YouTube video page.</div>';
}
});