This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
537 lines (423 loc) · 16.6 KB
/
index.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
'use strict';
/* ===== Configuration ======== */
var API_KEY = null; // API KEY HERE!
var SEARCH_DELAY = 500; // in milliseconds
var SLIDER_INTERVAL = 500; // in ms
var SCROLL_TRIGGER = 300; // in pixels, before hitting the bottom
/* ============= Global Variables ============= */
var state = "watching";
var player = null;
var videoDuration = null;
var lastKeystroke = null;
var prev_state = null;
var searchResults = {};
var queue = [];
var nextPageToken = null;
var sliderInUse = false;
var isSearching = false;
var autolinker = new Autolinker({twitter: false, hashtag: false});
window.onload = function() {
var videoID = window.location.hash.slice(1, 1 + 11)
if (videoID.length == 11)
loadVideo(videoID);
else
changeVideo('WFxPkhLNrcc');
setInterval(updateSlider, SLIDER_INTERVAL);
/* BUGFIX
*
* I have no idea why but YouTube's iframe API doesn't always work as documented in the docs in case of
* `onStateChange` event. So we poll the state of the player every 10 ms.
*
* Source:
* http://stackoverflow.com/a/17078152/4466589
* from https://stackoverflow.com/questions/17078094/youtube-iframe-player-api-onstatechange-not-firing
*/
setInterval(function() {
var state = player.getPlayerState();
if (state != prev_state)
video_onStateChange();
prev_state = player.getPlayerState();
}, 50);
}
function loadVideo(videoId) {
if (player) {
player.destroy();
}
var page_content = document.getElementsByClassName("page-content")[0];
page_content.clientWidth;
player = new YT.Player('player', {
videoId: videoId,
width: page_content.clientWidth,
height: page_content.clientWidth * 9 / 16,
events: {
'onReady': video_onReady,
},
playerVars: { // For details: https://developers.google.com/youtube/player_parameters?playerVersion=HTML5
autohide: 1,
showinfo: 0,
disablekb: 1,
iv_load_policy: 3,
rel: 0
}
});
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState != 4 || xmlHttp.status != 200) // check if it's successfull and completely loaded
return;
var response = JSON.parse(xmlHttp.responseText);
var video_title = document.getElementById("video-title");
var video_desc = document.getElementById("video-desc");
var view_count = document.getElementById("view-count");
video_title.textContent = response.items[0].snippet.title;
video_desc.innerHTML = autolinker.link(response.items[0].snippet.description.split("\n").join("<br>"));
view_count.textContent = parseInt(response.items[0].statistics.viewCount).toLocaleString();
var likes = parseInt(response.items[0].statistics.likeCount);
var dislikes = parseInt(response.items[0].statistics.dislikeCount);
document.getElementById("like-dislike").MaterialProgress.setProgress(likes * 100 / (likes + dislikes));
var tabTitle = document.getElementsByTagName("title")[0];
tabTitle.textContent = response.items[0].snippet.title + " - EssentialYouTube";
var slider = document.getElementById("slider");
slider.setAttribute("max", parseDuration(response.items[0].contentDetails.duration));
slider.value = 0;
};
xmlHttp.open("GET", "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics,contentDetails&id=" + videoId + "&key=" + API_KEY, true);
xmlHttp.send(null);
}
function nextVideo() {
if (queue.length) {
changeVideo(queue.shift().id.videoId);
refreshQueue();
}
else {
player.stopVideo();
updateSlider();
}
}
function updateSlider() {
if (sliderInUse)
return;
var slider = document.getElementById("slider");
slider.MaterialSlider.change(player.getCurrentTime());
}
function enterSearch() {
if (state === "searching")
return;
var player_container = document.getElementById("player-container");
var controllers = document.getElementById("controllers");
var video_info = document.getElementById("info");
var search_results = document.getElementById("search-results");
player_container.classList.add("hidden");
video_info.classList.add("hidden");
controllers.classList.remove("hidden");
search_results.classList.remove("hidden");
state = "searching";
}
function exitSearch() {
if (state === "watching")
return;
var player_container = document.getElementById("player-container");
var controllers = document.getElementById("controllers");
var video_info = document.getElementById("info");
var search_results = document.getElementById("search-results");
search_results.classList.add("hidden");
controllers.classList.add("hidden");
player_container.classList.remove("hidden");
video_info.classList.remove("hidden");
state = "watching";
}
function search(nodelay, nextPage) {
var date = new Date();
if (!nodelay && date.getTime() - lastKeystroke < SEARCH_DELAY)
return;
if (isSearching)
return;
isSearching = true;
var search_i = document.getElementById("search");
var xmlHttp = new XMLHttpRequest();
var url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + encodeURIComponent(search_i.value) + '&type=video&maxResults=10&key=' + API_KEY;
if (nextPage && nextPageToken)
url += '&pageToken=' + nextPageToken;
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState != 4 || xmlHttp.status != 200) // check if it's successfull and completely loaded
return;
var response = JSON.parse(xmlHttp.responseText);
nextPageToken = response.nextPageToken;
for (var i=0; i < response.items.length; ++i) {
var item = response.items[i];
searchResults[item.id.videoId] = item;
}
refreshSearchResults();
isSearching = false;
};
xmlHttp.open('GET', url, true);
xmlHttp.send(null);
if (!nextPage)
searchResults = {};
}
function refreshSearchResults() {
var searchResultTemplate = "" +
'<li class="mdl-list__item">' +
'<div class="mdl-card mdl-shadow--2dp">' +
'<div class="mdl-card__title">' +
'<h3 class="mdl-card__title-text">' +
'<a onclick="javascript:video_title_onClick(\'{videoID}\')">{title}</a>' +
'</h3>' +
'</div>' +
'<div class="mdl-card__menu">' +
'<button class="mdl-button mdl-button--icon mdl-js-button" onclick="add_onClick(\'{videoID}\')" title="Add to queue">' +
'<i class="material-icons">playlist_add</i>' +
'</button>' +
'</div>' +
'<div class="entry-details">' +
'<div>' +
'<a onclick="javascript:thumbnail_onClick(\'{videoID}\')">' +
'<img src="{thumbnailURL}">' +
'</a>' +
'</div>' +
'<div class="mdl-card__supporting-text">' +
'{description}' +
'</div>' +
'</div>' +
'</div>' +
'</li>';
var search_results = document.getElementById("search-results");
search_results.innerHTML = '';
for (var itemID in searchResults) {
var item = searchResults[itemID];
search_results.innerHTML += searchResultTemplate.supplant({videoID: item.id.videoId,
title: item.snippet.title,
thumbnailURL: item.snippet.thumbnails.high.url,
description: autolinker.link(item.snippet.description)
});
}
}
function refreshQueue() {
var FIRST_ENTRY_TEMPLATE = '<li class="mdl-list__item">' +
'<div class="mdl-card mdl-shadow--2dp">' +
'<div class="mdl-card__title">' +
'<h3 class="mdl-card__title-text">'+
'<a onclick="javascript:queue_title_onClick()">Next</a>' +
'</h3>' +
'</div>' +
'<div class="mdl-card__menu">' +
'<button class="mdl-button mdl-button--icon mdl-js-button" onclick="downward_onClick(\'{pos}\')" disabled>' +
'<i class="material-icons">arrow_upward</i>' +
'</button>' +
'<button class="mdl-button mdl-button--icon mdl-js-button" onclick="downward_onClick(\'{pos}\')" title="Move downwards in queue">' +
'<i class="material-icons">arrow_downward</i>' +
'</button>' +
'<button class="mdl-button mdl-button--icon mdl-js-button" onclick="remove_onClick(\'{pos}\')" title="Remove from queue">' +
'<i class="material-icons">remove</i>' +
'</button>' +
'</div>' +
'<div class="entry-details">' +
'<div>' +
'<a onclick="javascript:queue_thumbnail_onClick()"><img src="{thumbnailURL}"></a>' +
'</div>' +
'<div class="mdl-card__supporting-text">' +
'{title}' +
'</div>' +
'</div>' +
'</div>' +
'</li>';
var CONSEQ_ENTRY_TEMPLATE = '<li class="mdl-list__item">' +
'<div class="mdl-card mdl-shadow--2dp">' +
'<div class="mdl-card__supporting-text">' +
'{title}' +
'</div>' +
'<div class="mdl-card__menu">' +
'<button class="mdl-button mdl-button--icon mdl-js-button" onclick="upward_onClick(\'{pos}\')" title="Move upwards in queue">' +
'<i class="material-icons">arrow_upward</i>' +
'</button>' +
'<button class="mdl-button mdl-button--icon mdl-js-button" onclick="downward_onClick(\'{pos}\')" title="Move downwards in queue" {isDisabled}>' +
'<i class="material-icons">arrow_downward</i>' +
'</button>' +
'<button class="mdl-button mdl-button--icon mdl-js-button" onclick="remove_onClick(\'{pos}\')" title="Remove from queue">' +
'<i class="material-icons">remove</i>' +
'</button>' +
'</div>' +
'</div>' +
'</li>';
var queue_list = document.getElementById("queue");
// delete all the entries
while (queue_list.lastChild) {
queue_list.removeChild(queue_list.lastChild);
}
// create the first entry
if (queue.length) {
queue_list.innerHTML = FIRST_ENTRY_TEMPLATE.supplant({title: queue[0].snippet.title,
thumbnailURL: queue[0].snippet.thumbnails.high.url,
pos: 0});
}
// create rest of the entries
for (var i=1; i < queue.length; ++i) {
queue_list.innerHTML += CONSEQ_ENTRY_TEMPLATE.supplant({title: queue[i].snippet.title,
pos: i,
isDisabled: i == queue.length - 1 ? "disabled" : ""});
}
}
function changeVideo(videoID) {
window.location.hash = videoID;
}
/* ==================================================== CALLBACKS =================================================== */
window.onhashchange = function() {
var videoID = window.location.hash.slice(1, 1 + 11);
if (videoID.length == 11) {
loadVideo(videoID);
}
}
function header_title_onClick() {
document.getElementsByTagName("main")[0].scrollTop = 0;
}
function video_title_onClick(videoID) {
exitSearch();
changeVideo(videoID);
}
function add_onClick(id) {
queue.push(searchResults[id]);
refreshQueue();
}
function queue_thumbnail_onClick() {
nextVideo();
}
function queue_title_onClick() {
nextVideo();
}
function thumbnail_onClick(videoID) {
exitSearch();
changeVideo(videoID);
}
function close_onClick() {
exitSearch();
}
function main_onScroll(main) {
if (state === "searching" && main.scrollTop + main.clientHeight + SCROLL_TRIGGER >= main.scrollHeight)
search(true, true);
}
function search_onInput(input_e) {
if (input_e.value.length === 0) {
exitSearch();
}
else {
enterSearch();
var date = new Date();
lastKeystroke = date.getTime();
setTimeout(search, SEARCH_DELAY);
}
}
function search_onKeydown(event) {
if (event.keyCode == 13) { // if enter is pressed
enterSearch();
search(true);
}
}
function search_onClick(search_i) {
if (search_i.value.length) {
enterSearch();
if (Object.keys(searchResults).length)
refreshSearchResults();
else
search(true);
}
}
function slider_onChange(slider) {
player.seekTo(slider.value, true);
}
function slider_onMousedown() {
sliderInUse = true;
}
function slider_onMouseup() {
sliderInUse = false;
}
function remove_onClick(pos) {
queue.splice(pos, 1);
refreshQueue();
}
function upward_onClick(pos) {
pos = parseInt(pos);
queue.splice(pos - 1, 0, queue.splice(pos, 1)[0]);
refreshQueue();
}
function downward_onClick(pos) {
pos = parseInt(pos);
queue.splice(pos + 1, 0, queue.splice(pos, 1)[0]);
refreshQueue();
}
function playPause_onClick() {
if (player.getPlayerState() === YT.PlayerState.PLAYING) {
player.pauseVideo();
}
else {
player.playVideo();
}
}
function volume_onClick(pp_btn) {
if (player.isMuted()) {
pp_btn.innerHTML = '<i class="material-icons">volume_up</i>';
player.unMute();
}
else {
player.mute();
pp_btn.innerHTML = '<i class="material-icons">volume_off</i>';
}
}
function video_onStateChange() {
var pp_btn = document.getElementById("play-pause");
if (player.getPlayerState() === YT.PlayerState.ENDED) {
if (document.getElementById("loopSwitch").checked) {
player.seekTo(0, true);
player.playVideo();
}
else {
pp_btn.innerHTML = '<i class="material-icons">play_arrow</i>';
nextVideo();
}
}
else if (player.getPlayerState() === YT.PlayerState.PLAYING) {
pp_btn.innerHTML = '<i class="material-icons">pause</i>';
}
else {
pp_btn.innerHTML = '<i class="material-icons">play_arrow</i>';
}
}
function video_onReady(event) {
event.target.playVideo();
}
function next_onClick() {
nextVideo();
}
/* ==================================================== UTILITIES =================================================== */
// http://javascript.crockford.com/remedial.html
if (!String.prototype.supplant) {
String.prototype.supplant = function (o) {
return this.replace(
/\{([^{}]*)\}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
}
// http://stackoverflow.com/a/25209563
function parseDuration(duration) {
var matches = duration.match(/[0-9]+[HMS]/g);
var seconds = 0;
matches.forEach(function (part) {
var unit = part.charAt(part.length-1);
var amount = parseInt(part.slice(0,-1));
switch (unit) {
case 'H':
seconds += amount * 60 * 60;
break;
case 'M':
seconds += amount * 60;
break;
case 'S':
seconds += amount;
break;
default:
}
});
return seconds;
}