more refactoring

This commit is contained in:
Samuel Casellas 2025-01-11 14:11:47 -05:00
parent 88e80d50ad
commit c365cd274e
4 changed files with 49 additions and 83 deletions

View file

@ -22,34 +22,25 @@ function toggle_comments(event) {
}
}
function hide_youtube_replies(event) {
var target = event.target;
function toggle_youtube_replies(target, next_click_action) {
var sub_text = target.getAttribute('data-inner-text');
var inner_text = target.getAttribute('data-sub-text');
var body = target.parentNode.parentNode.children[1];
body.style.display = 'none';
body.style.display = next_click_action === show_youtube_replies ? 'none' : '';
target.textContent = sub_text;
target.onclick = show_youtube_replies;
target.onclick = next_click_action;
target.setAttribute('data-inner-text', inner_text);
target.setAttribute('data-sub-text', sub_text);
}
function hide_youtube_replies(event) {
toggle_youtube_replies(event.target, show_youtube_replies);
}
function show_youtube_replies(event) {
var target = event.target;
var sub_text = target.getAttribute('data-inner-text');
var inner_text = target.getAttribute('data-sub-text');
var body = target.parentNode.parentNode.children[1];
body.style.display = '';
target.textContent = sub_text;
target.onclick = hide_youtube_replies;
target.setAttribute('data-inner-text', inner_text);
target.setAttribute('data-sub-text', sub_text);
toggle_youtube_replies(event.target, hide_youtube_replies);
}
function get_youtube_comments() {

View file

@ -1,34 +1,25 @@
'use strict';
var community_data = JSON.parse(document.getElementById('community_data').textContent);
function hide_youtube_replies(event) {
var target = event.target;
function toggle_youtube_replies(target, next_click_action) {
var sub_text = target.getAttribute('data-inner-text');
var inner_text = target.getAttribute('data-sub-text');
var body = target.parentNode.parentNode.children[1];
body.style.display = 'none';
body.style.display = next_click_action === show_youtube_replies ? 'none' : '';
target.innerHTML = sub_text;
target.onclick = show_youtube_replies;
target.onclick = next_click_action;
target.setAttribute('data-inner-text', inner_text);
target.setAttribute('data-sub-text', sub_text);
}
function hide_youtube_replies(event) {
toggle_youtube_replies(event.target, show_youtube_replies);
}
function show_youtube_replies(event) {
var target = event.target;
var sub_text = target.getAttribute('data-inner-text');
var inner_text = target.getAttribute('data-sub-text');
var body = target.parentNode.parentNode.children[1];
body.style.display = '';
target.innerHTML = sub_text;
target.onclick = hide_youtube_replies;
target.setAttribute('data-inner-text', inner_text);
target.setAttribute('data-sub-text', sub_text);
toggle_youtube_replies(event.target, hide_youtube_replies);
}
function get_youtube_replies(target, load_more) {

View file

@ -1,6 +1,16 @@
'use strict';
var video_data = JSON.parse(document.getElementById('video_data').textContent);
function set_search_params() {
if (video_data.params.autoplay || video_data.params.continue_autoplay)
url.searchParams.set('autoplay', '1');
['listen', 'speed', 'local'].forEach(p => {
if (video_data.params[p] !== video_data.preferences[p])
url.searchParams.set(p, video_data.params[p]);
});
}
function get_playlist(plid) {
var plid_url;
if (plid.startsWith('RD')) {
@ -25,14 +35,7 @@ function get_playlist(plid) {
url.searchParams.set('list', plid);
if (!plid.startsWith('RD'))
url.searchParams.set('index', response.index);
if (video_data.params.autoplay || video_data.params.continue_autoplay)
url.searchParams.set('autoplay', '1');
if (video_data.params.listen !== video_data.preferences.listen)
url.searchParams.set('listen', video_data.params.listen);
if (video_data.params.speed !== video_data.preferences.speed)
url.searchParams.set('speed', video_data.params.speed);
if (video_data.params.local !== video_data.preferences.local)
url.searchParams.set('local', video_data.params.local);
set_search_params();
location.assign(url.pathname + url.search);
});
@ -47,14 +50,7 @@ addEventListener('load', function (e) {
player.on('ended', function () {
var url = new URL('https://example.com/embed/' + video_data.video_series.shift());
if (video_data.params.autoplay || video_data.params.continue_autoplay)
url.searchParams.set('autoplay', '1');
if (video_data.params.listen !== video_data.preferences.listen)
url.searchParams.set('listen', video_data.params.listen);
if (video_data.params.speed !== video_data.preferences.speed)
url.searchParams.set('speed', video_data.params.speed);
if (video_data.params.local !== video_data.preferences.local)
url.searchParams.set('local', video_data.params.local);
set_search_params();
if (video_data.video_series.length !== 0)
url.searchParams.set('playlist', video_data.video_series.join(','));

View file

@ -26,17 +26,20 @@ if (continue_button) {
continue_button.onclick = continue_autoplay;
}
function set_search_params() {
if (video_data.params.autoplay || video_data.params.continue_autoplay)
url.searchParams.set('autoplay', '1');
['listen', 'speed', 'local'].forEach(p => {
if (video_data.params[p] !== video_data.preferences[p])
url.searchParams.set(p, video_data.params[p]);
});
}
function next_video() {
var url = new URL('https://example.com/watch?v=' + video_data.next_video);
if (video_data.params.autoplay || video_data.params.continue_autoplay)
url.searchParams.set('autoplay', '1');
if (video_data.params.listen !== video_data.preferences.listen)
url.searchParams.set('listen', video_data.params.listen);
if (video_data.params.speed !== video_data.preferences.speed)
url.searchParams.set('speed', video_data.params.speed);
if (video_data.params.local !== video_data.preferences.local)
url.searchParams.set('local', video_data.params.local);
set_search_params();
url.searchParams.set('continue', '1');
location.assign(url.pathname + url.search);
@ -82,14 +85,7 @@ function get_playlist(plid) {
url.searchParams.set('list', plid);
if (!plid.startsWith('RD'))
url.searchParams.set('index', response.index);
if (video_data.params.autoplay || video_data.params.continue_autoplay)
url.searchParams.set('autoplay', '1');
if (video_data.params.listen !== video_data.preferences.listen)
url.searchParams.set('listen', video_data.params.listen);
if (video_data.params.speed !== video_data.preferences.speed)
url.searchParams.set('speed', video_data.params.speed);
if (video_data.params.local !== video_data.preferences.local)
url.searchParams.set('local', video_data.params.local);
set_search_params();
location.assign(url.pathname + url.search);
});
@ -117,10 +113,6 @@ function get_reddit_comments() {
'?source=reddit&format=html' +
'&hl=' + video_data.preferences.locale;
var onNon200 = function (xhr) { comments.innerHTML = fallback; };
if (video_data.params.comments[1] === 'youtube')
onNon200 = function (xhr) {};
helpers.xhr('GET', url, {retries: 5, entity_name: ''}, {
on200: function (response) {
comments.innerHTML = ' \
@ -152,7 +144,9 @@ function get_reddit_comments() {
comments.children[0].children[0].children[0].onclick = toggle_comments;
comments.children[0].children[1].children[0].onclick = swap_comments;
},
onNon200: onNon200, // declared above
onNon200: video_data.params.comments[1] === 'youtube'
? function (xhr) {}
: function (xhr) { comments.innerHTML = fallback; }
});
}
@ -160,14 +154,7 @@ if (video_data.play_next) {
player.on('ended', function () {
var url = new URL('https://example.com/watch?v=' + video_data.next_video);
if (video_data.params.autoplay || video_data.params.continue_autoplay)
url.searchParams.set('autoplay', '1');
if (video_data.params.listen !== video_data.preferences.listen)
url.searchParams.set('listen', video_data.params.listen);
if (video_data.params.speed !== video_data.preferences.speed)
url.searchParams.set('speed', video_data.params.speed);
if (video_data.params.local !== video_data.preferences.local)
url.searchParams.set('local', video_data.params.local);
set_search_params();
url.searchParams.set('continue', '1');
location.assign(url.pathname + url.search);
@ -178,13 +165,14 @@ addEventListener('load', function (e) {
if (video_data.plid)
get_playlist(video_data.plid);
if (video_data.params.comments[0] === 'youtube') {
const c = video_data.params.comments;
if (c[0] === 'youtube') {
get_youtube_comments();
} else if (video_data.params.comments[0] === 'reddit') {
} else if (c[0] === 'reddit') {
get_reddit_comments();
} else if (video_data.params.comments[1] === 'youtube') {
} else if (c[1] === 'youtube') {
get_youtube_comments();
} else if (video_data.params.comments[1] === 'reddit') {
} else if (c[1] === 'reddit') {
get_reddit_comments();
} else {
var comments = document.getElementById('comments');