summaryrefslogtreecommitdiffstats
path: root/assets/js/community.js
diff options
context:
space:
mode:
authormeow <woem>2022-05-06 04:46:59 +0300
committermeow <woem>2022-05-06 04:46:59 +0300
commit7dd699370fae20c69119a4117468b1d999a2752a (patch)
treea02ff5e2a25bb8ce7a89e5ac43a55a463adbf791 /assets/js/community.js
parentef8c7184de2da3dd143dfc54dcb8f20d0ab67dc8 (diff)
downloadinvidious-7dd699370fae20c69119a4117468b1d999a2752a.tar.gz
invidious-7dd699370fae20c69119a4117468b1d999a2752a.tar.bz2
invidious-7dd699370fae20c69119a4117468b1d999a2752a.zip
js code rewrite. Created _helpers.js with XHR and storage wrapper
Diffstat (limited to 'assets/js/community.js')
-rw-r--r--assets/js/community.js80
1 files changed, 29 insertions, 51 deletions
diff --git a/assets/js/community.js b/assets/js/community.js
index 44066a58..33e2e3ed 100644
--- a/assets/js/community.js
+++ b/assets/js/community.js
@@ -1,13 +1,6 @@
'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;
@@ -38,13 +31,6 @@ function show_youtube_replies(event) {
target.setAttribute('data-sub-text', sub_text);
}
-function number_with_separator(val) {
- while (/(\d+)(\d{3})/.test(val.toString())) {
- val = val.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
- }
- return val;
-}
-
function get_youtube_replies(target, load_more) {
var continuation = target.getAttribute('data-continuation');
@@ -52,53 +38,45 @@ function get_youtube_replies(target, load_more) {
var fallback = body.innerHTML;
body.innerHTML =
'<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>';
-
+
var url = '/api/v1/channels/comments/' + community_data.ucid +
'?format=html' +
'&hl=' + community_data.preferences.locale +
'&thin_mode=' + community_data.preferences.thin_mode +
'&continuation=' + continuation;
- var xhr = new XMLHttpRequest();
- xhr.responseType = 'json';
- xhr.timeout = 10000;
- xhr.open('GET', url, true);
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- if (xhr.status === 200) {
- if (load_more) {
- body = body.parentNode.parentNode;
- body.removeChild(body.lastElementChild);
- body.innerHTML += xhr.response.contentHtml;
- } else {
- body.removeChild(body.lastElementChild);
+ helpers.xhr('GET', url, {}, {
+ on200: function (response) {
+ if (load_more) {
+ body = body.parentNode.parentNode;
+ body.removeChild(body.lastElementChild);
+ body.innerHTML += response.contentHtml;
+ } else {
+ body.removeChild(body.lastElementChild);
- var p = document.createElement('p');
- var a = document.createElement('a');
- p.appendChild(a);
+ var p = document.createElement('p');
+ var a = document.createElement('a');
+ p.appendChild(a);
- a.href = 'javascript:void(0)';
- a.onclick = hide_youtube_replies;
- a.setAttribute('data-sub-text', community_data.hide_replies_text);
- a.setAttribute('data-inner-text', community_data.show_replies_text);
- a.innerText = community_data.hide_replies_text;
+ a.href = 'javascript:void(0)';
+ a.onclick = hide_youtube_replies;
+ a.setAttribute('data-sub-text', community_data.hide_replies_text);
+ a.setAttribute('data-inner-text', community_data.show_replies_text);
+ a.innerText = community_data.hide_replies_text;
- var div = document.createElement('div');
- div.innerHTML = xhr.response.contentHtml;
+ var div = document.createElement('div');
+ div.innerHTML = response.contentHtml;
- body.appendChild(p);
- body.appendChild(div);
- }
- } else {
- body.innerHTML = fallback;
+ body.appendChild(p);
+ body.appendChild(div);
}
+ },
+ onNon200: function (xhr) {
+ body.innerHTML = fallback;
+ },
+ onTimeout: function (xhr) {
+ console.warn('Pulling comments failed');
+ body.innerHTML = fallback;
}
- };
-
- xhr.ontimeout = function () {
- console.warn('Pulling comments failed.');
- body.innerHTML = fallback;
- };
-
- xhr.send();
+ });
}