invidious/assets/js/subscribe_widget.js

97 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-04-20 10:38:24 +02:00
'use strict';
var subscribe_data = JSON.parse(document.getElementById('subscribe_data').textContent);
2019-05-05 15:35:54 +02:00
var subscribe_button = document.getElementById('subscribe');
subscribe_button.parentNode['action'] = 'javascript:void(0)';
2019-05-02 03:03:39 +02:00
2019-05-05 15:35:54 +02:00
if (subscribe_button.getAttribute('data-type') === 'subscribe') {
subscribe_button.onclick = subscribe;
} else {
subscribe_button.onclick = unsubscribe;
}
2022-04-20 13:52:41 +02:00
function subscribe(retries) {
if (retries === undefined) retries = 5;
2019-06-15 17:08:06 +02:00
if (retries <= 0) {
console.warn('Failed to subscribe.');
2019-01-28 05:12:07 +01:00
return;
}
2019-05-02 03:03:39 +02:00
var url = '/subscription_ajax?action_create_subscription_to_channel=1&redirect=false' +
2019-05-08 16:15:27 +02:00
'&c=' + subscribe_data.ucid;
var xhr = new XMLHttpRequest();
2019-05-02 03:03:39 +02:00
xhr.responseType = 'json';
2019-06-16 19:34:00 +02:00
xhr.timeout = 10000;
2019-05-02 03:03:39 +02:00
xhr.open('POST', url, true);
2019-06-08 02:56:41 +02:00
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
2019-01-28 05:12:07 +01:00
var fallback = subscribe_button.innerHTML;
subscribe_button.onclick = unsubscribe;
2019-05-05 15:35:54 +02:00
subscribe_button.innerHTML = '<b>' + subscribe_data.unsubscribe_text + ' | ' + subscribe_data.sub_count_text + '</b>';
2019-05-05 15:35:54 +02:00
xhr.onreadystatechange = function () {
2022-04-20 11:13:16 +02:00
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
2019-01-28 05:12:07 +01:00
subscribe_button.onclick = subscribe;
subscribe_button.innerHTML = fallback;
}
}
2022-04-20 11:05:19 +02:00
};
2019-06-15 17:08:06 +02:00
xhr.onerror = function () {
console.warn('Subscribing failed... ' + retries + '/5');
2022-04-20 11:05:19 +02:00
setTimeout(function () { subscribe(retries - 1); }, 1000);
};
2019-06-15 17:08:06 +02:00
2019-05-05 15:35:54 +02:00
xhr.ontimeout = function () {
console.warn('Subscribing failed... ' + retries + '/5');
2019-06-15 17:08:06 +02:00
subscribe(retries - 1);
2022-04-20 11:05:19 +02:00
};
2019-06-15 17:08:06 +02:00
xhr.send('csrf_token=' + subscribe_data.csrf_token);
}
2022-04-20 13:52:41 +02:00
function unsubscribe(retries) {
if (retries === undefined)
retries = 5;
2019-06-15 17:08:06 +02:00
if (retries <= 0) {
console.warn('Failed to subscribe');
2019-01-28 05:12:07 +01:00
return;
}
2019-05-02 03:03:39 +02:00
var url = '/subscription_ajax?action_remove_subscriptions=1&redirect=false' +
2019-05-08 16:15:27 +02:00
'&c=' + subscribe_data.ucid;
var xhr = new XMLHttpRequest();
2019-05-02 03:03:39 +02:00
xhr.responseType = 'json';
2019-06-16 19:34:00 +02:00
xhr.timeout = 10000;
2019-05-02 03:03:39 +02:00
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
2019-01-28 05:12:07 +01:00
var fallback = subscribe_button.innerHTML;
subscribe_button.onclick = subscribe;
2019-05-05 15:35:54 +02:00
subscribe_button.innerHTML = '<b>' + subscribe_data.subscribe_text + ' | ' + subscribe_data.sub_count_text + '</b>';
2019-01-28 05:12:07 +01:00
2019-05-05 15:35:54 +02:00
xhr.onreadystatechange = function () {
2022-04-20 11:13:16 +02:00
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
2019-01-28 05:12:07 +01:00
subscribe_button.onclick = unsubscribe;
subscribe_button.innerHTML = fallback;
}
}
2022-04-20 11:05:19 +02:00
};
2019-01-28 05:12:07 +01:00
2019-06-15 17:08:06 +02:00
xhr.onerror = function () {
console.warn('Unsubscribing failed... ' + retries + '/5');
2022-04-20 11:05:19 +02:00
setTimeout(function () { unsubscribe(retries - 1); }, 1000);
};
2019-06-15 17:08:06 +02:00
2019-05-05 15:35:54 +02:00
xhr.ontimeout = function () {
console.warn('Unsubscribing failed... ' + retries + '/5');
2019-06-15 17:08:06 +02:00
unsubscribe(retries - 1);
2022-04-20 11:05:19 +02:00
};
2019-06-15 17:08:06 +02:00
xhr.send('csrf_token=' + subscribe_data.csrf_token);
}