-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
111 lines (92 loc) · 3.67 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
const createBookmarkElement = (bookmark) => {
const bookmarkElement = document.createElement("div");
bookmarkElement.className = "bookmark";
bookmarkElement.setAttribute("timestamp", bookmark.time);
bookmarkElement.setAttribute("videoId", bookmark.videoId);
const bookmarkContentElement = document.createElement("div");
bookmarkContentElement.className = "bookmark-content";
const thumbnailElement = document.createElement("img");
thumbnailElement.className = "bookmark-thumbnail";
thumbnailElement.src = `https://img.youtube.com/vi/${bookmark.videoId}/default.jpg`;
const videoTitleElement = document.createElement("div");
videoTitleElement.className = "bookmark-video-title";
// title would be like "How To Think Like A Programmer - YouTube", always strip
// out the " - YouTube" part
const videoTitle = bookmark.title.replace(" - YouTube", "");
videoTitleElement.textContent = videoTitle;
const bookmarkTitleElement = document.createElement("div");
bookmarkTitleElement.className = "bookmark-title";
bookmarkTitleElement.textContent = bookmark.desc;
const controlsElement = document.createElement("div");
controlsElement.className = "bookmark-controls";
const playButtonElement = createControlButton("play", onPlay);
const deleteButtonElement = createControlButton("delete", onDelete);
controlsElement.appendChild(playButtonElement);
controlsElement.appendChild(deleteButtonElement);
bookmarkElement.appendChild(bookmarkContentElement);
bookmarkElement.appendChild(thumbnailElement);
bookmarkContentElement.appendChild(videoTitleElement);
bookmarkContentElement.appendChild(bookmarkTitleElement);
bookmarkElement.appendChild(controlsElement);
return bookmarkElement;
};
const createControlButton = (src, eventListener) => {
const buttonElement = document.createElement("img");
buttonElement.src = `assets/${src}.png`;
buttonElement.title = src;
buttonElement.style = "height:20px; width:20px; cursor:pointer;";
buttonElement.addEventListener("click", eventListener);
return buttonElement;
};
const renderBookmarks = (bookmarks) => {
const bookmarksElement = document.getElementById("bookmarks");
bookmarksElement.innerHTML = "";
if (bookmarks.length > 0) {
bookmarks.forEach((bookmark) => {
const bookmarkElement = createBookmarkElement(bookmark);
bookmarksElement.appendChild(bookmarkElement);
});
} else {
bookmarksElement.innerHTML =
"<p class='description'>There's no time tag to show</p>";
}
};
const onPlay = (e) => {
const bookmarkTime = e.target
.closest(".bookmark")
.getAttribute("timestamp");
const videoId = e.target.closest(".bookmark").getAttribute("videoId");
if (videoId) {
const videoURL = `https://www.youtube.com/watch?v=${videoId}&t=${Math.floor(
bookmarkTime
)}s`;
chrome.tabs.create({ url: videoURL });
} else {
alert("This bookmark is not a valid YouTube video bookmark.");
}
};
const onDelete = (e) => {
const bookmarkTime = e.target
.closest(".bookmark")
.getAttribute("timestamp");
const videoId = e.target.closest(".bookmark").getAttribute("videoId");
const bookmarkElementToDelete = e.target.closest(".bookmark");
bookmarkElementToDelete.remove();
chrome.storage.sync.get(["allBookmarks"], (data) => {
let allBookmarks = data["allBookmarks"]
? JSON.parse(data["allBookmarks"])
: [];
allBookmarks = allBookmarks.filter(
(b) => !(b.videoId === videoId && b.time == bookmarkTime)
);
chrome.storage.sync.set({ allBookmarks: JSON.stringify(allBookmarks) });
});
};
document.addEventListener("DOMContentLoaded", () => {
chrome.storage.sync.get(["allBookmarks"], (data) => {
const allBookmarks = data["allBookmarks"]
? JSON.parse(data["allBookmarks"])
: [];
renderBookmarks(allBookmarks);
});
});