-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
79 lines (59 loc) · 2.43 KB
/
popup.js
File metadata and controls
79 lines (59 loc) · 2.43 KB
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
const AZ_PROBLEM_KEY = "AZ_PROBLEM_KEY";
const assetsURLMap = {
"play": chrome.runtime.getURL("assets/play.png"),
"delete": chrome.runtime.getURL("assets/delete.png")
}
const bookmarkSection = document.getElementById("bookmarks");
document.addEventListener("DOMContentLoaded", () => {
chrome.storage.sync.get([AZ_PROBLEM_KEY], (data) => {
const currentBookmarks = data[AZ_PROBLEM_KEY] || [];
viewBookmarks(currentBookmarks);
});
});
function viewBookmarks(bookmarks) {
bookmarkSection.innerHTML = "";
if(bookmarks.length === 0) {
bookmarkSection.innerHTML = "<i>No Bookmarks to Show</i>";
return;
}
bookmarks.forEach(bookmark => addNewBookmark(bookmark));
}
function addNewBookmark(bookmark){
const newBookmark = document.createElement('div');
const bookmarkTitle = document.createElement('div');
const bookmarkControls = document.createElement('div');
bookmarkTitle.textContent = bookmark.name;
bookmarkTitle.classList.add("bookmark-title");
setControlAttributes(assetsURLMap["play"], onPlay, bookmarkControls);
setControlAttributes(assetsURLMap["delete"], onDelete, bookmarkControls);
bookmarkControls.classList.add("bookmark-controls");
newBookmark.classList.add("bookmark");
newBookmark.append(bookmarkTitle);
newBookmark.append(bookmarkControls);
newBookmark.setAttribute("url", bookmark.url);
newBookmark.setAttribute("bookmark-id", bookmark.id);
bookmarkSection.appendChild(newBookmark);
}
function setControlAttributes(src, handler, parentDiv) {
const controlElement = document.createElement("img");
controlElement.src = src;
controlElement.addEventListener("click", handler);
parentDiv.appendChild(controlElement);
}
function onPlay(event) {
const problemUrl = event.target.parentNode.parentNode.getAttribute("url");
window.open(problemUrl, "_blank");
}
function onDelete(event){
const bookmarkItem = event.target.parentNode.parentNode;
const idToRemove = bookmarkItem.getAttribute("bookmark-id");
bookmarkItem.remove();
deleteItemFromStorage(idToRemove);
}
function deleteItemFromStorage(idToRemove){
chrome.storage.sync.get([AZ_PROBLEM_KEY], (data) => {
const currentBookmarks = data[AZ_PROBLEM_KEY] || [];
const updatedBookmarks = currentBookmarks.filter((bookmark) => bookmark.id !== idToRemove);
chrome.storage.sync.set({AZ_PROBLEM_KEY : updatedBookmarks});
})
}