Skip to content

Commit

Permalink
Merge pull request #3045 from AHOHNMYC/js-lint
Browse files Browse the repository at this point in the history
Js lint
  • Loading branch information
SamantazFox authored Apr 27, 2022
2 parents a0f566f + dbb1e3f commit 3bbd709
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 245 deletions.
27 changes: 14 additions & 13 deletions assets/js/community.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
var community_data = JSON.parse(document.getElementById('community_data').innerHTML);
'use strict';
var community_data = JSON.parse(document.getElementById('community_data').textContent);

String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g, function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
}
};

function hide_youtube_replies(event) {
var target = event.target;

sub_text = target.getAttribute('data-inner-text');
inner_text = target.getAttribute('data-sub-text');
var sub_text = target.getAttribute('data-inner-text');
var inner_text = target.getAttribute('data-sub-text');

body = target.parentNode.parentNode.children[1];
var body = target.parentNode.parentNode.children[1];
body.style.display = 'none';

target.innerHTML = sub_text;
Expand All @@ -25,10 +26,10 @@ function hide_youtube_replies(event) {
function show_youtube_replies(event) {
var target = event.target;

sub_text = target.getAttribute('data-inner-text');
inner_text = target.getAttribute('data-sub-text');
var sub_text = target.getAttribute('data-inner-text');
var inner_text = target.getAttribute('data-sub-text');

body = target.parentNode.parentNode.children[1];
var body = target.parentNode.parentNode.children[1];
body.style.display = '';

target.innerHTML = sub_text;
Expand Down Expand Up @@ -63,8 +64,8 @@ function get_youtube_replies(target, load_more) {
xhr.open('GET', url, true);

xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (load_more) {
body = body.parentNode.parentNode;
body.removeChild(body.lastElementChild);
Expand Down Expand Up @@ -92,12 +93,12 @@ function get_youtube_replies(target, load_more) {
body.innerHTML = fallback;
}
}
}
};

xhr.ontimeout = function () {
console.log('Pulling comments failed.');
console.warn('Pulling comments failed.');
body.innerHTML = fallback;
}
};

xhr.send();
}
26 changes: 14 additions & 12 deletions assets/js/embed.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
var video_data = JSON.parse(document.getElementById('video_data').innerHTML);
'use strict';
var video_data = JSON.parse(document.getElementById('video_data').textContent);

function get_playlist(plid, retries) {
if (retries == undefined) retries = 5;
if (retries === undefined) retries = 5;

if (retries <= 0) {
console.log('Failed to pull playlist');
console.warn('Failed to pull playlist');
return;
}

var plid_url;
if (plid.startsWith('RD')) {
var plid_url = '/api/v1/mixes/' + plid +
plid_url = '/api/v1/mixes/' + plid +
'?continuation=' + video_data.id +
'&format=html&hl=' + video_data.preferences.locale;
} else {
var plid_url = '/api/v1/playlists/' + plid +
plid_url = '/api/v1/playlists/' + plid +
'?index=' + video_data.index +
'&continuation' + video_data.id +
'&format=html&hl=' + video_data.preferences.locale;
Expand Down Expand Up @@ -57,17 +59,17 @@ function get_playlist(plid, retries) {
}
}
}
}
};

xhr.onerror = function () {
console.log('Pulling playlist failed... ' + retries + '/5');
setTimeout(function () { get_playlist(plid, retries - 1) }, 1000);
}
console.warn('Pulling playlist failed... ' + retries + '/5');
setTimeout(function () { get_playlist(plid, retries - 1); }, 1000);
};

xhr.ontimeout = function () {
console.log('Pulling playlist failed... ' + retries + '/5');
console.warn('Pulling playlist failed... ' + retries + '/5');
get_playlist(plid, retries - 1);
}
};

xhr.send();
}
Expand Down Expand Up @@ -96,7 +98,7 @@ window.addEventListener('load', function (e) {
}

if (video_data.video_series.length !== 0) {
url.searchParams.set('playlist', video_data.video_series.join(','))
url.searchParams.set('playlist', video_data.video_series.join(','));
}

location.assign(url.pathname + url.search);
Expand Down
22 changes: 11 additions & 11 deletions assets/js/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
});

n2a(document.querySelectorAll('[data-onrange="update_volume_value"]')).forEach(function (e) {
var cb = function () { update_volume_value(e); }
var cb = function () { update_volume_value(e); };
e.oninput = cb;
e.onchange = cb;
});
Expand All @@ -102,13 +102,13 @@
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status != 200) {
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
count.innerText = parseInt(count.innerText) + 1;
row.style.display = '';
}
}
}
};

var csrf_token = target.parentNode.querySelector('input[name="csrf_token"]').value;
xhr.send('csrf_token=' + csrf_token);
Expand All @@ -131,35 +131,35 @@
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status != 200) {
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
count.innerText = parseInt(count.innerText) + 1;
row.style.display = '';
}
}
}
};

var csrf_token = target.parentNode.querySelector('input[name="csrf_token"]').value;
xhr.send('csrf_token=' + csrf_token);
}

// Handle keypresses
window.addEventListener('keydown', (event) => {
window.addEventListener('keydown', function (event) {
// Ignore modifier keys
if (event.ctrlKey || event.metaKey) return;

// Ignore shortcuts if any text input is focused
let focused_tag = document.activeElement.tagName.toLowerCase();
const allowed = /^(button|checkbox|file|radio|submit)$/;

if (focused_tag === "textarea") return;
if (focused_tag === "input") {
if (focused_tag === 'textarea') return;
if (focused_tag === 'input') {
let focused_type = document.activeElement.type.toLowerCase();
if (!focused_type.match(allowed)) return;
}

// Focus search bar on '/'
if (event.key == "/") {
if (event.key === '/') {
document.getElementById('searchbox').focus();
event.preventDefault();
}
Expand Down
31 changes: 16 additions & 15 deletions assets/js/notifications.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var notification_data = JSON.parse(document.getElementById('notification_data').innerHTML);
'use strict';
var notification_data = JSON.parse(document.getElementById('notification_data').textContent);

var notifications, delivered;

function get_subscriptions(callback, retries) {
if (retries == undefined) retries = 5;
if (retries === undefined) retries = 5;

if (retries <= 0) {
return;
Expand All @@ -17,21 +18,21 @@ function get_subscriptions(callback, retries) {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
subscriptions = xhr.response;
var subscriptions = xhr.response;
callback(subscriptions);
}
}
}
};

xhr.onerror = function () {
console.log('Pulling subscriptions failed... ' + retries + '/5');
setTimeout(function () { get_subscriptions(callback, retries - 1) }, 1000);
}
console.warn('Pulling subscriptions failed... ' + retries + '/5');
setTimeout(function () { get_subscriptions(callback, retries - 1); }, 1000);
};

xhr.ontimeout = function () {
console.log('Pulling subscriptions failed... ' + retries + '/5');
console.warn('Pulling subscriptions failed... ' + retries + '/5');
get_subscriptions(callback, retries - 1);
}
};

xhr.send();
}
Expand All @@ -40,7 +41,7 @@ function create_notification_stream(subscriptions) {
notifications = new SSE(
'/api/v1/auth/notifications?fields=videoId,title,author,authorId,publishedText,published,authorThumbnails,liveNow', {
withCredentials: true,
payload: 'topics=' + subscriptions.map(function (subscription) { return subscription.authorId }).join(','),
payload: 'topics=' + subscriptions.map(function (subscription) { return subscription.authorId; }).join(','),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
delivered = [];
Expand All @@ -53,7 +54,7 @@ function create_notification_stream(subscriptions) {
}

var notification = JSON.parse(event.data);
console.log('Got notification:', notification);
console.info('Got notification:', notification);

if (start_time < notification.published && !delivered.includes(notification.videoId)) {
if (Notification.permission === 'granted') {
Expand All @@ -67,7 +68,7 @@ function create_notification_stream(subscriptions) {

system_notification.onclick = function (event) {
window.open('/watch?v=' + event.currentTarget.tag, '_blank');
}
};
}

delivered.push(notification.videoId);
Expand All @@ -82,16 +83,16 @@ function create_notification_stream(subscriptions) {
'<i class="icon ion-ios-notifications-outline"></i>';
}
}
}
};

notifications.addEventListener('error', handle_notification_error);
notifications.stream();
}

function handle_notification_error(event) {
console.log('Something went wrong with notifications, trying to reconnect...');
console.warn('Something went wrong with notifications, trying to reconnect...');
notifications = { close: function () { } };
setTimeout(function () { get_subscriptions(create_notification_stream) }, 1000);
setTimeout(function () { get_subscriptions(create_notification_stream); }, 1000);
}

window.addEventListener('load', function (e) {
Expand Down
Loading

0 comments on commit 3bbd709

Please sign in to comment.