invidious/assets/js/watch.js

450 lines
15 KiB
JavaScript
Raw Normal View History

2019-05-06 10:48:33 -04:00
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;
});
}
2018-10-06 23:20:40 -04:00
function toggle_parent(target) {
2019-05-01 21:03:39 -04:00
body = target.parentNode.parentNode.children[1];
2019-05-06 10:48:33 -04:00
if (body.style.display === null || body.style.display === '') {
target.innerHTML = '[ + ]';
body.style.display = 'none';
2019-05-01 21:03:39 -04:00
} else {
2019-05-06 10:48:33 -04:00
target.innerHTML = '[ - ]';
body.style.display = '';
2019-05-01 21:03:39 -04:00
}
2018-10-06 23:20:40 -04:00
}
2019-05-15 14:30:30 -04:00
function toggle_comments(event) {
var target = event.target;
2019-05-01 21:03:39 -04:00
body = target.parentNode.parentNode.parentNode.children[1];
2019-05-06 10:48:33 -04:00
if (body.style.display === null || body.style.display === '') {
target.innerHTML = '[ + ]';
body.style.display = 'none';
2019-05-01 21:03:39 -04:00
} else {
2019-05-06 10:48:33 -04:00
target.innerHTML = '[ - ]';
body.style.display = '';
2019-05-01 21:03:39 -04:00
}
2018-10-06 23:20:40 -04:00
}
2019-05-15 14:30:30 -04:00
function swap_comments(event) {
var source = event.target.getAttribute('data-comments');
if (source === 'youtube') {
2019-05-01 21:03:39 -04:00
get_youtube_comments();
2019-05-15 14:30:30 -04:00
} else if (source === 'reddit') {
2019-05-01 21:03:39 -04:00
get_reddit_comments();
}
2018-10-06 23:20:40 -04:00
}
2019-05-15 14:30:30 -04:00
function hide_youtube_replies(event) {
var target = event.target;
sub_text = target.getAttribute('data-inner-text');
inner_text = target.getAttribute('data-sub-text');
2019-05-01 21:03:39 -04:00
body = target.parentNode.parentNode.children[1];
2019-05-15 14:30:30 -04:00
body.style.display = 'none';
2018-10-15 12:15:23 -04:00
2019-05-15 14:30:30 -04:00
target.innerHTML = sub_text;
target.onclick = show_youtube_replies;
target.setAttribute('data-inner-text', inner_text);
target.setAttribute('data-sub-text', sub_text);
2018-10-15 12:15:23 -04:00
}
2019-05-15 14:30:30 -04:00
function show_youtube_replies(event) {
var target = event.target;
sub_text = target.getAttribute('data-inner-text');
inner_text = target.getAttribute('data-sub-text');
2019-05-01 21:03:39 -04:00
body = target.parentNode.parentNode.children[1];
2019-05-15 14:30:30 -04:00
body.style.display = '';
2018-10-15 12:15:23 -04:00
2019-05-01 21:03:39 -04:00
target.innerHTML = sub_text;
2019-05-15 14:30:30 -04:00
target.onclick = hide_youtube_replies;
target.setAttribute('data-inner-text', inner_text);
target.setAttribute('data-sub-text', sub_text);
2019-05-06 10:48:33 -04:00
}
2019-05-06 12:23:14 -04:00
var continue_button = document.getElementById('continue');
2019-05-14 09:18:57 -04:00
if (continue_button) {
continue_button.onclick = continue_autoplay;
}
2019-05-06 12:23:14 -04:00
function continue_autoplay(event) {
if (event.target.checked) {
2019-05-06 10:48:33 -04:00
player.on('ended', function () {
2019-05-06 12:23:14 -04:00
var url = new URL('https://example.com/watch?v=' + video_data.next_video);
2019-05-06 10:48:33 -04:00
2019-05-06 12:23:14 -04:00
if (video_data.params.autoplay || video_data.params.continue_autoplay) {
2019-05-06 10:48:33 -04:00
url.searchParams.set('autoplay', '1');
}
2019-05-06 12:23:14 -04:00
if (video_data.params.listen !== video_data.preferences.listen) {
url.searchParams.set('listen', video_data.params.listen);
2019-05-06 10:48:33 -04:00
}
2019-05-06 12:23:14 -04:00
if (video_data.params.speed !== video_data.preferences.speed) {
url.searchParams.set('speed', video_data.params.speed);
2019-05-06 10:48:33 -04:00
}
2019-05-27 13:16:22 -04:00
if (video_data.params.local !== video_data.preferences.local) {
url.searchParams.set('local', video_data.params.local);
}
2019-05-06 10:48:33 -04:00
url.searchParams.set('continue', '1');
location.assign(url.pathname + url.search);
});
} else {
player.off('ended');
}
}
function number_with_separator(val) {
while (/(\d+)(\d{3})/.test(val.toString())) {
val = val.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
}
return val;
}
function get_playlist(plid, retries) {
if (retries == undefined) retries = 5;
2019-05-06 10:48:33 -04:00
playlist = document.getElementById('playlist');
2019-06-15 11:08:06 -04:00
if (retries <= 0) {
2019-05-06 10:48:33 -04:00
console.log('Failed to pull playlist');
playlist.innerHTML = '';
return;
}
playlist.innerHTML = ' \
<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3> \
<hr>'
if (plid.startsWith('RD')) {
var plid_url = '/api/v1/mixes/' + plid +
2019-05-06 12:23:14 -04:00
'?continuation=' + video_data.id +
'&format=html&hl=' + video_data.preferences.locale;
2019-05-06 10:48:33 -04:00
} else {
var plid_url = '/api/v1/playlists/' + plid +
2019-05-06 12:23:14 -04:00
'?continuation=' + video_data.id +
'&format=html&hl=' + video_data.preferences.locale;
2019-05-06 10:48:33 -04:00
}
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
2019-06-16 13:34:00 -04:00
xhr.timeout = 10000;
2019-05-06 10:48:33 -04:00
xhr.open('GET', plid_url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
playlist.innerHTML = xhr.response.playlistHtml;
if (xhr.response.nextVideo) {
player.on('ended', function () {
2019-05-14 09:18:57 -04:00
var url = new URL('https://example.com/watch?v=' + xhr.response.nextVideo);
2019-05-06 10:48:33 -04:00
2019-05-06 12:23:14 -04:00
if (video_data.params.autoplay || video_data.params.continue_autoplay) {
2019-05-06 10:48:33 -04:00
url.searchParams.set('autoplay', '1');
}
2019-05-06 12:23:14 -04:00
if (video_data.params.listen !== video_data.preferences.listen) {
url.searchParams.set('listen', video_data.params.listen);
2019-05-06 10:48:33 -04:00
}
2019-05-06 12:23:14 -04:00
if (video_data.params.speed !== video_data.preferences.speed) {
url.searchParams.set('speed', video_data.params.speed);
2019-05-06 10:48:33 -04:00
}
2019-05-27 13:16:22 -04:00
if (video_data.params.local !== video_data.preferences.local) {
url.searchParams.set('local', video_data.params.local);
}
2019-05-06 10:48:33 -04:00
url.searchParams.set('list', plid);
location.assign(url.pathname + url.search);
});
}
} else {
playlist.innerHTML = '';
document.getElementById('continue').style.display = '';
}
}
}
2019-06-15 11:08:06 -04:00
xhr.onerror = function () {
playlist = document.getElementById('playlist');
playlist.innerHTML =
'<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3><hr>';
console.log('Pulling playlist timed out... ' + retries + '/5');
setTimeout(function () { get_playlist(plid, retries - 1) }, 1000);
}
2019-05-06 10:48:33 -04:00
xhr.ontimeout = function () {
playlist = document.getElementById('playlist');
playlist.innerHTML =
'<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3><hr>';
2019-06-07 20:56:41 -04:00
2019-06-15 11:08:06 -04:00
console.log('Pulling playlist timed out... ' + retries + '/5');
get_playlist(plid, retries - 1);
2019-05-06 10:48:33 -04:00
}
2019-06-15 11:08:06 -04:00
xhr.send();
2019-05-06 10:48:33 -04:00
}
function get_reddit_comments(retries) {
if (retries == undefined) retries = 5;
2019-05-06 10:48:33 -04:00
comments = document.getElementById('comments');
2019-06-15 11:08:06 -04:00
if (retries <= 0) {
2019-05-06 10:48:33 -04:00
console.log('Failed to pull comments');
comments.innerHTML = '';
return;
}
var fallback = comments.innerHTML;
comments.innerHTML =
'<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>';
2019-05-06 12:23:14 -04:00
var url = '/api/v1/comments/' + video_data.id +
2019-05-06 10:48:33 -04:00
'?source=reddit&format=html' +
2019-05-06 12:23:14 -04:00
'&hl=' + video_data.preferences.locale;
2019-05-06 10:48:33 -04:00
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
2019-06-16 13:34:00 -04:00
xhr.timeout = 10000;
2019-05-06 10:48:33 -04:00
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
comments.innerHTML = ' \
<div> \
<h3> \
2019-05-15 14:30:30 -04:00
<a href="javascript:void(0)">[ - ]</a> \
2019-05-06 10:48:33 -04:00
{title} \
</h3> \
<p> \
<b> \
2019-05-15 14:30:30 -04:00
<a href="javascript:void(0)" data-comments="youtube"> \
2019-05-06 10:48:33 -04:00
{youtubeCommentsText} \
</a> \
</b> \
</p> \
<b> \
<a rel="noopener" target="_blank" href="https://reddit.com{permalink}">{redditPermalinkText}</a> \
</b> \
</div> \
<div>{contentHtml}</div> \
<hr>'.supplant({
title: xhr.response.title,
2019-05-06 12:23:14 -04:00
youtubeCommentsText: video_data.youtube_comments_text,
redditPermalinkText: video_data.reddit_permalink_text,
2019-05-06 10:48:33 -04:00
permalink: xhr.response.permalink,
contentHtml: xhr.response.contentHtml
});
2019-05-15 14:30:30 -04:00
comments.children[0].children[0].children[0].onclick = toggle_comments;
comments.children[0].children[1].children[0].onclick = swap_comments;
2019-05-06 10:48:33 -04:00
} else {
2019-05-29 15:24:30 -04:00
if (video_data.params.comments[1] === 'youtube') {
2019-06-15 11:08:06 -04:00
console.log('Pulling comments failed... ' + retries + '/5');
setTimeout(function () { get_youtube_comments(retries - 1) }, 1000);
2019-05-06 10:48:33 -04:00
} else {
comments.innerHTML = fallback;
}
}
}
}
2019-06-15 11:08:06 -04:00
xhr.onerror = function () {
console.log('Pulling comments failed... ' + retries + '/5');
setInterval(function () { get_reddit_comments(retries - 1) }, 1000);
}
2019-05-06 10:48:33 -04:00
xhr.ontimeout = function () {
2019-06-15 11:08:06 -04:00
console.log('Pulling comments failed... ' + retries + '/5');
get_reddit_comments(retries - 1);
2019-05-06 10:48:33 -04:00
}
2019-06-15 11:08:06 -04:00
xhr.send();
2019-05-06 10:48:33 -04:00
}
function get_youtube_comments(retries) {
if (retries == undefined) retries = 5;
2019-05-06 10:48:33 -04:00
comments = document.getElementById('comments');
2019-06-15 11:08:06 -04:00
if (retries <= 0) {
2019-05-06 10:48:33 -04:00
console.log('Failed to pull comments');
comments.innerHTML = '';
return;
}
var fallback = comments.innerHTML;
comments.innerHTML =
'<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>';
2019-05-06 12:23:14 -04:00
var url = '/api/v1/comments/' + video_data.id +
2019-05-06 10:48:33 -04:00
'?format=html' +
2019-05-06 12:23:14 -04:00
'&hl=' + video_data.preferences.locale +
'&thin_mode=' + video_data.preferences.thin_mode;
2019-05-06 10:48:33 -04:00
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
2019-06-16 13:34:00 -04:00
xhr.timeout = 10000;
2019-05-06 10:48:33 -04:00
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
2019-05-29 15:24:30 -04:00
comments.innerHTML = ' \
<div> \
<h3> \
<a href="javascript:void(0)">[ - ]</a> \
{commentsText} \
</h3> \
<b> \
<a href="javascript:void(0)" data-comments="reddit"> \
{redditComments} \
</a> \
</b> \
</div> \
<div>{contentHtml}</div> \
<hr>'.supplant({
contentHtml: xhr.response.contentHtml,
redditComments: video_data.reddit_comments_text,
commentsText: video_data.comments_text.supplant(
{ commentCount: number_with_separator(xhr.response.commentCount) }
)
});
2019-05-15 14:30:30 -04:00
2019-05-29 15:24:30 -04:00
comments.children[0].children[0].children[0].onclick = toggle_comments;
comments.children[0].children[1].children[0].onclick = swap_comments;
2019-05-06 10:48:33 -04:00
} else {
2019-05-29 15:24:30 -04:00
if (video_data.params.comments[1] === 'youtube') {
2019-06-15 11:08:06 -04:00
setTimeout(function () { get_youtube_comments(retries - 1) }, 1000);
2019-05-06 10:48:33 -04:00
} else {
comments.innerHTML = '';
}
}
}
}
2019-06-15 11:08:06 -04:00
xhr.onerror = function () {
comments.innerHTML =
'<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>';
console.log('Pulling comments failed... ' + retries + '/5');
setInterval(function () { get_youtube_comments(retries - 1) }, 1000);
}
2019-05-06 10:48:33 -04:00
xhr.ontimeout = function () {
comments.innerHTML =
'<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>';
2019-06-15 11:08:06 -04:00
console.log('Pulling comments failed... ' + retries + '/5');
get_youtube_comments(retries - 1);
2019-05-06 10:48:33 -04:00
}
2019-06-15 11:08:06 -04:00
xhr.send();
2019-05-06 10:48:33 -04:00
}
function get_youtube_replies(target, load_more) {
var continuation = target.getAttribute('data-continuation');
var body = target.parentNode.parentNode;
var fallback = body.innerHTML;
body.innerHTML =
'<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>';
2019-05-06 12:23:14 -04:00
var url = '/api/v1/comments/' + video_data.id +
2019-05-06 10:48:33 -04:00
'?format=html' +
2019-05-06 12:23:14 -04:00
'&hl=' + video_data.preferences.locale +
'&thin_mode=' + video_data.preferences.thin_mode +
2019-05-06 10:48:33 -04:00
'&continuation=' + continuation;
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
2019-06-16 13:34:00 -04:00
xhr.timeout = 10000;
2019-05-06 10:48:33 -04:00
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (load_more) {
body = body.parentNode.parentNode;
body.removeChild(body.lastElementChild);
body.innerHTML += xhr.response.contentHtml;
} else {
2019-05-15 14:30:30 -04:00
body.removeChild(body.lastElementChild);
var p = document.createElement('p');
var a = document.createElement('a');
p.appendChild(a);
a.href = 'javascript:void(0)';
a.onclick = hide_youtube_replies;
a.setAttribute('data-sub-text', video_data.hide_replies_text);
a.setAttribute('data-inner-text', video_data.show_replies_text);
a.innerText = video_data.hide_replies_text;
var div = document.createElement('div');
div.innerHTML = xhr.response.contentHtml;
body.appendChild(p);
body.appendChild(div);
2019-05-06 10:48:33 -04:00
}
} else {
body.innerHTML = fallback;
}
}
}
xhr.ontimeout = function () {
2019-06-15 11:08:06 -04:00
console.log('Pulling comments failed.');
2019-05-06 10:48:33 -04:00
body.innerHTML = fallback;
}
2019-06-15 11:08:06 -04:00
xhr.send();
2019-05-06 10:48:33 -04:00
}
2019-05-06 12:23:14 -04:00
if (video_data.play_next) {
2019-05-06 10:48:33 -04:00
player.on('ended', function () {
2019-05-06 12:23:14 -04:00
var url = new URL('https://example.com/watch?v=' + video_data.next_video);
2019-05-06 10:48:33 -04:00
2019-05-06 12:23:14 -04:00
if (video_data.params.autoplay || video_data.params.continue_autoplay) {
2019-05-06 10:48:33 -04:00
url.searchParams.set('autoplay', '1');
}
2019-05-06 12:23:14 -04:00
if (video_data.params.listen !== video_data.preferences.listen) {
url.searchParams.set('listen', video_data.params.listen);
2019-05-06 10:48:33 -04:00
}
2019-05-06 12:23:14 -04:00
if (video_data.params.speed !== video_data.preferences.speed) {
url.searchParams.set('speed', video_data.params.speed);
2019-05-06 10:48:33 -04:00
}
2019-05-27 13:16:22 -04:00
if (video_data.params.local !== video_data.preferences.local) {
url.searchParams.set('local', video_data.params.local);
}
2019-05-06 10:48:33 -04:00
url.searchParams.set('continue', '1');
location.assign(url.pathname + url.search);
});
}
2019-05-06 12:23:14 -04:00
if (video_data.plid) {
get_playlist(video_data.plid);
2019-05-06 10:48:33 -04:00
}
2019-05-29 15:24:30 -04:00
if (video_data.params.comments[0] === 'youtube') {
2019-05-06 10:48:33 -04:00
get_youtube_comments();
2019-05-29 15:24:30 -04:00
} else if (video_data.params.comments[0] === 'reddit') {
2019-05-06 10:48:33 -04:00
get_reddit_comments();
2019-05-29 15:24:30 -04:00
} else if (video_data.params.comments[1] === 'youtube') {
2019-05-06 10:48:33 -04:00
get_youtube_comments();
2019-05-29 15:24:30 -04:00
} else if (video_data.params.comments[1] === 'reddit') {
2019-05-06 10:48:33 -04:00
get_reddit_comments();
} else {
comments = document.getElementById('comments');
comments.innerHTML = '';
2018-10-15 12:15:23 -04:00
}