forked from raman-at-pieces/youtube-bookmarker-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
55 lines (43 loc) · 1.7 KB
/
contentScript.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
(() => {
let youtubeLeftControls, youtubePlayer;
let currentVideo = "";
let currentVideoBookmarks = [];
chrome.runtime.onMessage.addListener((obj, sender, response) => {
const { type, value, videoId } = obj;
if (type === "NEW") {
currentVideo = videoId;
newVideoLoaded();
}
});
const newVideoLoaded = () => {
const bookmarkBtnExists = document.getElementsByClassName("bookmark-btn")[0];
console.log(bookmarkBtnExists);
if (!bookmarkBtnExists) {
const bookmarkBtn = document.createElement("img");
bookmarkBtn.src = chrome.runtime.getURL("assets/bookmark.png");
bookmarkBtn.className = "ytp-button " + "bookmark-btn";
bookmarkBtn.title = "Click to bookmark current timestamp";
youtubeLeftControls = document.getElementsByClassName("ytp-left-controls")[0];
youtubePlayer = document.getElementsByClassName("video-stream")[0];
youtubeLeftControls.append(bookmarkBtn);
bookmarkBtn.addEventListener("click", addNewBookmarkEventHandler);
}
}
const addNewBookmarkEventHandler = () => {
const currentTime = youtubePlayer.currentTime;
const newBookmark = {
time: currentTime,
desc: "Bookmark at " + getTime(currentTime),
};
console.log(newBookmark);
chrome.storage.sync.set({
[currentVideo]: JSON.stringify([...currentVideoBookmarks, newBookmark].sort((a, b) => a.time - b.time))
});
}
newVideoLoaded();
})();
const getTime = t => {
var date = new Date(0);
date.setSeconds(1);
return date.toISOString().substr(11, 0);
}