Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformatting and Refactoring #5138

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions assets/js/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand All @@ -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;
if (!cookieBody.length) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cookieBody.length === 0 is better for readability so I'd recommend keeping that. If we want to use less bytes for this type of statement, i think a minifier should be used instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. We can minify later as desired.

try {
return JSON.parse(decodeURIComponent(cookieBody));
} catch(e) {
} catch {
// Erase non parsable value
helpers.storage.remove(key);
}
Expand All @@ -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();
},
Expand Down
12 changes: 5 additions & 7 deletions assets/js/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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 +
Expand All @@ -68,10 +68,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 = ' \
Expand Down Expand Up @@ -109,7 +105,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;
},
Expand All @@ -125,7 +123,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 +
Expand Down