some reformatting

This commit is contained in:
Samuel Casellas 2025-01-06 07:32:40 -05:00
parent 53e8a5d62d
commit 564bca966f
2 changed files with 14 additions and 17 deletions

View File

@ -153,8 +153,8 @@ window.helpers = window.helpers || {
return; return;
} }
if (!options.entity_name) options.entity_name = 'unknown'; options.entity_name = options.entity_name || 'unknown';
if (!options.retry_timeout) options.retry_timeout = 1000; options.retry_timeout = options.retry_timeout || 1000;
const retries_total = options.retries; const retries_total = options.retries;
let currentTry = 1; let currentTry = 1;
@ -197,7 +197,7 @@ window.helpers = window.helpers || {
storage: (function () { storage: (function () {
// access to localStorage throws exception in Tor Browser, so try is needed // access to localStorage throws exception in Tor Browser, so try is needed
let localStorageIsUsable = false; let localStorageIsUsable = false;
try{localStorageIsUsable = !!localStorage.setItem;}catch(e){} try { localStorageIsUsable = !!localStorage.setItem; } catch {}
if (localStorageIsUsable) { if (localStorageIsUsable) {
return { return {
@ -206,7 +206,7 @@ window.helpers = window.helpers || {
if (!storageItem) return; if (!storageItem) return;
try { try {
return JSON.parse(decodeURIComponent(storageItem)); return JSON.parse(decodeURIComponent(storageItem));
} catch(e) { } catch {
// Erase non parsable value // Erase non parsable value
helpers.storage.remove(key); helpers.storage.remove(key);
} }
@ -224,14 +224,13 @@ window.helpers = window.helpers || {
return { return {
get: function (key) { get: function (key) {
const cookiePrefix = key + '='; const cookiePrefix = key + '=';
function findCallback(cookie) {return cookie.startsWith(cookiePrefix);} const matchedCookie = document.cookie.split('; ').find(cookie => cookie.startsWith(cookiePrefix));
const matchedCookie = document.cookie.split('; ').find(findCallback);
if (matchedCookie) { if (matchedCookie) {
const cookieBody = matchedCookie.replace(cookiePrefix, ''); const cookieBody = matchedCookie.replace(cookiePrefix, '');
if (cookieBody.length === 0) return; if (!cookieBody.length) return;
try { try {
return JSON.parse(decodeURIComponent(cookieBody)); return JSON.parse(decodeURIComponent(cookieBody));
} catch(e) { } catch {
// Erase non parsable value // Erase non parsable value
helpers.storage.remove(key); helpers.storage.remove(key);
} }
@ -240,9 +239,9 @@ window.helpers = window.helpers || {
set: function (key, value) { set: function (key, value) {
const cookie_data = encodeURIComponent(JSON.stringify(value)); const cookie_data = encodeURIComponent(JSON.stringify(value));
// Set expiration in 2 year // Set expiration for 2 years out
const date = new Date(); const date = new Date();
date.setFullYear(date.getFullYear()+2); date.setFullYear(date.getFullYear() + 2);
document.cookie = key + '=' + cookie_data + '; expires=' + date.toGMTString(); document.cookie = key + '=' + cookie_data + '; expires=' + date.toGMTString();
}, },

View File

@ -58,7 +58,7 @@ function get_youtube_comments() {
var fallback = comments.innerHTML; var fallback = comments.innerHTML;
comments.innerHTML = spinnerHTML; 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 + var url = baseUrl +
'?format=html' + '?format=html' +
'&hl=' + video_data.preferences.locale + '&hl=' + video_data.preferences.locale +
@ -68,10 +68,6 @@ function get_youtube_comments() {
url += '&ucid=' + video_data.ucid 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'}, { helpers.xhr('GET', url, {retries: 5, entity_name: 'comments'}, {
on200: function (response) { on200: function (response) {
var commentInnerHtml = ' \ var commentInnerHtml = ' \
@ -109,7 +105,9 @@ function get_youtube_comments() {
comments.children[0].children[1].children[0].onclick = swap_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) { onError: function (xhr) {
comments.innerHTML = spinnerHTML; comments.innerHTML = spinnerHTML;
}, },
@ -125,7 +123,7 @@ function get_youtube_replies(target, load_more, load_replies) {
var body = target.parentNode.parentNode; var body = target.parentNode.parentNode;
var fallback = body.innerHTML; var fallback = body.innerHTML;
body.innerHTML = spinnerHTML; 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 + var url = baseUrl +
'?format=html' + '?format=html' +
'&hl=' + video_data.preferences.locale + '&hl=' + video_data.preferences.locale +