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