diff --git a/assets/js/_helpers.js b/assets/js/_helpers.js index 8e18169e..3ce1746d 100644 --- a/assets/js/_helpers.js +++ b/assets/js/_helpers.js @@ -153,8 +153,8 @@ window.helpers = window.helpers || { return; } - if (!options.entity_name) options.entity_name = 'unknown'; - if (!options.retry_timeout) options.retry_timeout = 1000; + options.entity_name = options.entity_name || 'unknown'; + options.retry_timeout = options.retry_timeout || 1000; const retries_total = options.retries; let currentTry = 1; @@ -197,7 +197,7 @@ window.helpers = window.helpers || { storage: (function () { // access to localStorage throws exception in Tor Browser, so try is needed let localStorageIsUsable = false; - try{localStorageIsUsable = !!localStorage.setItem;}catch(e){} + try { localStorageIsUsable = !!localStorage.setItem; } catch {} if (localStorageIsUsable) { return { @@ -206,7 +206,7 @@ window.helpers = window.helpers || { if (!storageItem) return; try { return JSON.parse(decodeURIComponent(storageItem)); - } catch(e) { + } catch { // Erase non parsable value helpers.storage.remove(key); } @@ -224,14 +224,13 @@ window.helpers = window.helpers || { return { get: function (key) { const cookiePrefix = key + '='; - function findCallback(cookie) {return cookie.startsWith(cookiePrefix);} - const matchedCookie = document.cookie.split('; ').find(findCallback); + const matchedCookie = document.cookie.split('; ').find(cookie => cookie.startsWith(cookiePrefix)); if (matchedCookie) { const cookieBody = matchedCookie.replace(cookiePrefix, ''); if (cookieBody.length === 0) return; try { return JSON.parse(decodeURIComponent(cookieBody)); - } catch(e) { + } catch { // Erase non parsable value helpers.storage.remove(key); } @@ -240,9 +239,9 @@ window.helpers = window.helpers || { set: function (key, value) { const cookie_data = encodeURIComponent(JSON.stringify(value)); - // Set expiration in 2 year + // Set expiration for 2 years out const date = new Date(); - date.setFullYear(date.getFullYear()+2); + date.setFullYear(date.getFullYear() + 2); document.cookie = key + '=' + cookie_data + '; expires=' + date.toGMTString(); }, diff --git a/assets/js/comments.js b/assets/js/comments.js index 35ffa96e..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() { @@ -58,7 +49,7 @@ function get_youtube_comments() { var fallback = comments.innerHTML; comments.innerHTML = spinnerHTML; - var baseUrl = video_data.base_url || '/api/v1/comments/'+ video_data.id + var baseUrl = video_data.base_url || '/api/v1/comments/' + video_data.id var url = baseUrl + '?format=html' + '&hl=' + video_data.preferences.locale + @@ -68,10 +59,6 @@ function get_youtube_comments() { url += '&ucid=' + video_data.ucid } - 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: 'comments'}, { on200: function (response) { var commentInnerHtml = ' \ @@ -109,7 +96,9 @@ function get_youtube_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; }, onError: function (xhr) { comments.innerHTML = spinnerHTML; }, @@ -125,7 +114,7 @@ function get_youtube_replies(target, load_more, load_replies) { var body = target.parentNode.parentNode; var fallback = body.innerHTML; body.innerHTML = spinnerHTML; - var baseUrl = video_data.base_url || '/api/v1/comments/'+ video_data.id + var baseUrl = video_data.base_url || '/api/v1/comments/' + video_data.id var url = baseUrl + '?format=html' + '&hl=' + video_data.preferences.locale + 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 d869d40d..ff5f42c3 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); @@ -86,14 +89,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); }); @@ -121,10 +117,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 = ' \ @@ -156,7 +148,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; } }); } @@ -164,14 +158,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); @@ -182,13 +169,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');