-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
111 lines (99 loc) · 3.62 KB
/
content.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
// Declare class names at the top
const YTP_CHROME_CONTROLS_CLASS = "ytp-chrome-controls";
const VIDEO_STREAM_CLASS = "video-stream";
const BOOKMARK_BTN_CLASS = "bookmark-btn";
let youtubeControls;
let youtubePlayer;
let currentVideo = "";
let currentVideoBookmarks = [];
const getTime = (t) => new Date(t * 1e3).toISOString().substr(11, 8);
const fetchBookmarks = async () =>
new Promise((resolve) =>
chrome.storage.sync.get([currentVideo], ({ [currentVideo]: current }) =>
resolve(current ? JSON.parse(current) : [])
)
);
const showMessage = (message, time, style) => {
let messageDiv = document.createElement("div");
messageDiv.textContent = message;
messageDiv.style = style;
document.body.appendChild(messageDiv);
setTimeout(() => (messageDiv.style.opacity = "0"), time * 0.75);
setTimeout(() => document.body.removeChild(messageDiv), time);
};
const createBookmarkBtn = () => {
const bookmarkBtn = document.createElement("img");
bookmarkBtn.src = chrome.runtime.getURL("assets/timetag.png");
bookmarkBtn.className = `${YTP_CHROME_CONTROLS_CLASS} ${BOOKMARK_BTN_CLASS}`;
bookmarkBtn.title = "Click to add a time tag";
bookmarkBtn.style = "height:30px; width:30px; margin:auto; cursor:pointer;";
return bookmarkBtn;
};
const setupYoutubeControlsAndPlayer = () => {
youtubeControls = document.getElementsByClassName(
YTP_CHROME_CONTROLS_CLASS
)[0];
youtubeControls.style.display = "flex";
youtubePlayer = document.getElementsByClassName(VIDEO_STREAM_CLASS)[0];
};
// const newVideoLoaded = async () => {
// const bookmarkBtnExists =
// document.getElementsByClassName(BOOKMARK_BTN_CLASS)[0];
// setupYoutubeControlsAndPlayer();
// currentVideoBookmarks = await fetchBookmarks();
// if (!bookmarkBtnExists && youtubeControls) {
// const bookmarkBtn = createBookmarkBtn();
// youtubeControls.appendChild(bookmarkBtn);
// bookmarkBtn.addEventListener("click", addNewBookmarkEventHandler);
// }
// };
const newVideoLoaded = async () => {
setupYoutubeControlsAndPlayer();
currentVideoBookmarks = await fetchBookmarks();
// Check if the bookmark button exists after setting up youtubeControls
const bookmarkBtnExists = youtubeControls.querySelector(
`.${BOOKMARK_BTN_CLASS}`
);
if (!bookmarkBtnExists && youtubeControls) {
const bookmarkBtn = createBookmarkBtn();
youtubeControls.appendChild(bookmarkBtn);
bookmarkBtn.addEventListener("click", addNewBookmarkEventHandler);
}
};
const addNewBookmarkEventHandler = async () => {
const currentTime = youtubePlayer.currentTime;
const videoTitle = document.title;
const newBookmark = {
time: currentTime,
desc: getTime(currentTime),
title: videoTitle,
videoId: currentVideo,
};
chrome.storage.sync.get(["allBookmarks"], ({ allBookmarks: all }) => {
let allBookmarks = all ? JSON.parse(all) : [];
allBookmarks.push(newBookmark);
chrome.storage.sync.set({ allBookmarks: JSON.stringify(allBookmarks) });
});
showMessage(
"Video time tag added!",
2000,
`font-size: 1.5rem; position: fixed; z-index: 1000; background-color: #4CAF50; border-radius: 0.5rem; color: white; padding: 15px; bottom: 30px; right: 30px; transition: opacity 1s; opacity: 1;`
);
};
chrome.runtime.onMessage.addListener((message, sender, response) => {
const { type, value, videoId } = message;
if (type === "NEW") {
currentVideo = videoId;
newVideoLoaded();
} else if (type === "PLAY") {
youtubePlayer.currentTime = value;
} else if (type === "DELETE") {
currentVideoBookmarks = currentVideoBookmarks.filter(
(bookmark) => bookmark.time != value
);
chrome.storage.sync.set({
[currentVideo]: JSON.stringify(currentVideoBookmarks),
});
response(currentVideoBookmarks);
}
});