From c365cd274e348904e170969b7227ab1894f0fc36 Mon Sep 17 00:00:00 2001 From: Samuel Casellas Date: Sat, 11 Jan 2025 14:11:47 -0500 Subject: [PATCH] more refactoring --- assets/js/comments.js | 25 +++++++------------ assets/js/community.js | 25 +++++++------------ assets/js/embed.js | 28 ++++++++++------------ assets/js/watch.js | 54 ++++++++++++++++-------------------------- 4 files changed, 49 insertions(+), 83 deletions(-) diff --git a/assets/js/comments.js b/assets/js/comments.js index 6c0f7c1d..dda83c0b 100644 --- a/assets/js/comments.js +++ b/assets/js/comments.js @@ -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() { diff --git a/assets/js/community.js b/assets/js/community.js index 32fe4ebc..2e2b7001 100644 --- a/assets/js/community.js +++ b/assets/js/community.js @@ -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) { diff --git a/assets/js/embed.js b/assets/js/embed.js index b11b5e5a..7eb6b643 100644 --- a/assets/js/embed.js +++ b/assets/js/embed.js @@ -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(',')); diff --git a/assets/js/watch.js b/assets/js/watch.js index 26ad138f..cd60e552 100644 --- a/assets/js/watch.js +++ b/assets/js/watch.js @@ -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');