-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
135 lines (112 loc) · 3.98 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
let youtubeLeftControls, youtubePlayer; // for accessing the Controls & the Player
let currentVideo = "";
let currentVideoBookmarks = [];
const fetchBookmarks = () => {
return new Promise((resolve) => {
if (chrome.runtime && !chrome.runtime.lastError) {
chrome.storage.sync.get([currentVideo], (obj) => {
resolve(obj[currentVideo] ? JSON.parse(obj[currentVideo]) : []);
});
} else {
console.error("Extension context invalidated.");
resolve([]);
}
});
};
const addNewBookmarkEventHandler = async () => {
const currentTime = youtubePlayer.currentTime;
const newBookmark = {
time: currentTime,
desc: "Bookmark at " + getTime(currentTime),
};
currentVideoBookmarks = await fetchBookmarks();
chrome.storage.sync.set({
[currentVideo]: JSON.stringify([...currentVideoBookmarks, newBookmark].sort((a, b) => a.time - b.time))
});
// Send a message to the background script to show the badge
chrome.runtime.sendMessage({type: "SHOW_BADGE"});
};
const checkForPlayer = async () => {
youtubeLeftControls = document.getElementsByClassName("ytp-left-controls")[0];
youtubePlayer = document.getElementsByClassName("video-stream")[0];
if (youtubeLeftControls && youtubePlayer) {
currentVideoBookmarks = await fetchBookmarks();
const bookmarkBtnExists = document.getElementsByClassName("bookmark-btn")[0];
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.appendChild(bookmarkBtn);
bookmarkBtn.addEventListener("click", addNewBookmarkEventHandler);
return true; // Bookmark button added
} else {
return false; // Bookmark button already exists
}
}
return false; // Bookmark button not added
};
const addStyles = () => {
const style = document.createElement("style");
style.innerHTML = `
.bookmark-btn {
width: 46px !important;
height: 46px !important;
min-width: 46px !important;
min-height: 46px !important;
max-width: 46px !important;
max-height: 46px !important;
object-fit: contain;
margin-left: 8px;
margin-right: 8px;
z-index: 9999;
padding: 0 !important;
display: inline-block !important;
justify-content: center;
position: relative;
}
.bookmark-btn:hover {
cursor: pointer;
background-color: rgba(0, 0, 0, .05);
border: 1px solid rgba(0, 0, 0, .05);
}
.ytp-chrome-controls {
margin-right: 0 !important;
}
`;
document.head.appendChild(style);
};
const init = async () => {
addStyles();
return checkForPlayer();
};
chrome.runtime.onMessage.addListener(async (obj, sender, response) => {
const { type, value, videoId } = obj;
if (type === "NEW") {
currentVideo = videoId;
response(await init());
} else if (type === "PLAY") {
youtubePlayer.currentTime = value;
} else if (type === "DELETE") {
const bookmarkTime = parseFloat(value);
currentVideoBookmarks = currentVideoBookmarks.filter((b) => {
return b.time !== bookmarkTime;
});
chrome.storage.sync.set({ [currentVideo]: JSON.stringify(currentVideoBookmarks) });
response(currentVideoBookmarks);
}
});
const getTime = t => {
var date = new Date(0);
date.setSeconds(t);
return date.toISOString().substr(11, 8);
};
document.addEventListener('keydown', function(event) {
if (event.shiftKey && event.key === 'A') {
const tagName = event.target.tagName.toLowerCase();
if (tagName !== 'input' && tagName !== 'textarea' && !event.target.isContentEditable) {
event.preventDefault();
addNewBookmarkEventHandler();
}
}
});