-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
93 lines (72 loc) · 3 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
import { getActiveTabURL } from "./utils.js";
const addNewBookmark = (bookmarks, bookmark) => {
const bookmarkTitleElement = document.createElement("div");
const controlsElement = document.createElement("div");
const newBookmarkElement = document.createElement("div");
bookmarkTitleElement.textContent = bookmark.desc;
bookmarkTitleElement.className = "bookmark-title";
controlsElement.className = "bookmark-controls";
setBookmarkAttributes("play", onPlay, controlsElement);
setBookmarkAttributes("delete", onDelete, controlsElement);
newBookmarkElement.id = "bookmark-" + bookmark.time;
newBookmarkElement.className = "bookmark";
newBookmarkElement.setAttribute("timestamp", bookmark.time);
newBookmarkElement.appendChild(bookmarkTitleElement);
newBookmarkElement.appendChild(controlsElement);
bookmarks.appendChild(newBookmarkElement);
};
const viewBookmarks = (currentBookmarks=[]) => {
const bookmarksElement = document.getElementById("bookmarks");
bookmarksElement.innerHTML = "";
if (currentBookmarks.length > 0) {
for (let i = 0; i < currentBookmarks.length; i++) {
const bookmark = currentBookmarks[i];
addNewBookmark(bookmarksElement, bookmark);
}
} else {
bookmarksElement.innerHTML = '<i class="row">No bookmarks to show</i>';
}
return;
};
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 onDelete = async e => {
const activeTab = await getActiveTabURL();
const bookmarkTime = e.target.parentNode.parentNode.getAttribute("timestamp");
const bookmarkElementToDelete = document.getElementById(
"bookmark-" + bookmarkTime
);
bookmarkElementToDelete.parentNode.removeChild(bookmarkElementToDelete);
chrome.tabs.sendMessage(activeTab.id, {
type: "DELETE",
value: bookmarkTime,
}, 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);
};
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>';
}
});