-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
294 lines (252 loc) · 8.32 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
var cached = [];
cached.push(location.href);
var bottomMenu;
var snackbar_timeout;
var selected;
var addbuttoncolor2 = "#c4c4c4"
// Get the videoplayer
function attachToVid() {
var vid = document.querySelector("video");
if (vid) {
// Play next when video ends
vid.onended = function(e) {
nextMessage();
}
vid.addEventListener("pause", function() {
chrome.runtime.sendMessage({
type: "playing",
state: false
});
});
vid.addEventListener("play", function() {
chrome.runtime.sendMessage({
type: "playing",
state: true
});
});
}
}
// Snackbar to notify about "playnexthere"
function injectSnackbar() {
var snackbar = document.createElement("div");
snackbar.setAttribute("id", "snackbar");
document.body.append(snackbar);
}
function showSnackbar(message) {
if (snackbar_timeout) {
clearTimeout(snackbar_timeout);
}
// Get the snackbar DIV
var x = document.getElementById("snackbar");
x.innerHTML = message;
// Add the "show" class to DIV
x.className = "show";
// After 3 seconds, remove the show class from DIV
snackbar_timeout = setTimeout(function() {
x.className = "";
snackbar_timeout = null;
}, 3000);
}
injectSnackbar();
function herebarSnackbarMessage() {
var message = "";
// sendmessage is async
chrome.runtime.sendMessage({
type: "check"
}, function(response) {
// Decide which screen the user is in and the context
if (response.response == "selected") {
message = "Queue paused";
} else if (document.location.href.match(/watch/)) {
message = "Queued after this video";
} else {
message = "Queued to this tab";
}
// Show snackbar with appropriate message
showSnackbar(message);
});
}
function injectPrimaryAddButton() {
var video_primary_info = document.querySelector("ytd-menu-renderer.ytd-video-primary-info-renderer");
var top_level_buttons = video_primary_info.querySelector("div#top-level-buttons");
top_level_buttons.querySelectorAll(".addbuttonprimary").forEach(function(button) {
top_level_buttons.removeChild(button);
});
var titlediv = document.querySelector("h1.title");
var title = titlediv.querySelector("yt-formatted-string.ytd-video-primary-info-renderer").innerHTML;
var button = document.createElement("img");
button.src = chrome.extension.getURL("images/plus.png");
button.className = "addbuttonprimary";
button.onclick = function() {
button.style.background = addbuttoncolor2;
button.style.borderColor = addbuttoncolor2;
addNext(title.trim(), document.location.href);
};
top_level_buttons.appendChild(button);
}
// Tell the background that the video is done
function nextMessage() {
chrome.runtime.sendMessage({
type: "next"
});
}
// Add next to local queue storage
function addNext(name, nexturl) {
var videoqueue;
chrome.storage.local.get({
'queue': []
}, function(result) {
videoqueue = result.queue;
videoqueue.push([name, nexturl]);
chrome.storage.local.set({
'queue': videoqueue
}, function() {});
});
}
// Injects the controls in the bottom
function injectBottomMenu() {
bottomMenu = document.createElement("div");
bottomMenu.id = "bottomMenu";
var nextbar = document.createElement("a");
nextbar.id = "next";
var img = document.createElement("img");
img.src = chrome.extension.getURL("images/nexticon.png");
nextbar.appendChild(img);
var herebar = document.createElement("a");
herebar.id = "here";
herebar.innerHTML = "Q";
// Ask background if this tab is selected
chrome.runtime.sendMessage({
type: "check"
}, function(response) {
if (response.response == "selected") {
herebar.style.color = 'white';
}
});
var pip = document.createElement("a");
pip.id = "pip";
var img = document.createElement("img");
img.src = chrome.extension.getURL("images/pip.png");
pip.appendChild(img);
nextbar.onclick = function() {
chrome.runtime.sendMessage({
type: "forcenext"
});
}
pip.onclick = function() {
var vid = document.querySelector("video");
if (vid) {
if (vid !== document.pictureInPictureElement) {
vid.requestPictureInPicture();
} else {
document.exitPictureInPicture();
}
}
}
herebar.onclick = function() {
herebarSnackbarMessage();
chrome.runtime.sendMessage({
type: "playnexthere"
});
}
bottomMenu.appendChild(nextbar);
bottomMenu.appendChild(herebar);
bottomMenu.appendChild(pip);
document.body.appendChild(bottomMenu);
}
injectBottomMenu();
// Add button to div called dismissible
function injectAddButton(dismissible) {
var thumbnailoverlay = dismissible.querySelector("ytd-thumbnail");
if (thumbnailoverlay == null || thumbnailoverlay.querySelector(".addbutton") != null) {
return;
}
var thumbnail = dismissible.querySelector("#thumbnail");
var title = dismissible.querySelector("#video-title");
var button = document.createElement("img");
button.src = chrome.extension.getURL("images/plus.png");
if (!document.location.href.match(/watch/)){
button.style.width="18px";
button.style.height="18px";
button.style.borderWidth="7px";
}
// var button = document.createElement("button");
button.className = "addbutton";
// button.appendChild(img);
button.onclick = function() {
button.style.background = addbuttoncolor2;
button.style.borderColor = addbuttoncolor2;
if(title.hasAttribute("title")){
addNext(title.getAttribute("title").trim(), "https://www.youtube.com" + thumbnail.getAttribute("href"));
} else {
addNext(title.innerHTML.trim(), "https://www.youtube.com" + thumbnail.getAttribute("href"));
}
};
thumbnailoverlay.appendChild(button);
}
// Select all current dismissibles
document.querySelectorAll("#dismissible").forEach(function(dismissible) {
injectAddButton(dismissible);
});
// When the "dismissible" div arrives, inject button
document.arrive("#dismissible", function() {
injectAddButton(this);
});
document.querySelectorAll(".html5-main-video").forEach(function() {
attachToVid();
});
document.arrive(".html5-main-video",function(){
attachToVid();
});
// Hide Bottom menu on fullscreen
document.addEventListener("fullscreenchange", (event) => {
if (document.fullscreenElement) {
bottomMenu.style.display = "none";
} else if (!document.fullscreenElement) {
bottomMenu.style.display = "block";
}
});
// Broadcast tabid to other scripts
chrome.runtime.sendMessage({
type: "newtab"
});
// Button which adds the current video to the queue
window.addEventListener("yt-page-data-updated", function() {
injectPrimaryAddButton();
});
// window.addEventListener("DOMContentLoaded", function() {
// injectPrimaryAddButton();
// });
document.arrive("#top-level-buttons",function(){
injectPrimaryAddButton();
document.unbindArrive("#top-level-buttons");
});
// Listen to directions from the background script
chrome.runtime.onMessage.addListener(
function(msg, sender, sendResponse) {
if (msg.type == "notselected") {
document.querySelector("a#here").style.color = "black";
} else if (msg.type == "selected") {
document.querySelector("a#here").style.color = "white";
}
if (msg.type == "play") {
document.querySelector("video").play();
chrome.runtime.sendMessage({
type: "playing",
state: true
});
} else if (msg.type == "pause") {
document.querySelector("video").pause();
chrome.runtime.sendMessage({
type: "playing",
state: false
});
}
});
window.addEventListener("yt-navigate-finish", function() {
injectPrimaryAddButton();
chrome.runtime.sendMessage({
type: "playing",
state: true
});
});