You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.6 KiB
JavaScript
79 lines
2.6 KiB
JavaScript
6 years ago
|
var subscribe_button = document.getElementById('subscribe');
|
||
|
subscribe_button.parentNode['action'] = 'javascript:void(0)';
|
||
6 years ago
|
|
||
6 years ago
|
if (subscribe_button.getAttribute('data-type') === 'subscribe') {
|
||
|
subscribe_button.onclick = subscribe;
|
||
|
} else {
|
||
|
subscribe_button.onclick = unsubscribe;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
function subscribe(timeouts = 0) {
|
||
|
if (timeouts > 10) {
|
||
6 years ago
|
console.log('Failed to subscribe.');
|
||
6 years ago
|
return;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
var url = '/subscription_ajax?action_create_subscription_to_channel=1&redirect=false' +
|
||
6 years ago
|
'&c=' + subscribe_data.ucid +
|
||
|
'&referer=' + location.pathname + location.search;
|
||
6 years ago
|
var xhr = new XMLHttpRequest();
|
||
6 years ago
|
xhr.responseType = 'json';
|
||
6 years ago
|
xhr.timeout = 20000;
|
||
6 years ago
|
xhr.open('POST', url, true);
|
||
6 years ago
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||
6 years ago
|
xhr.send('csrf_token=' + subscribe_data.csrf_token);
|
||
6 years ago
|
|
||
6 years ago
|
var fallback = subscribe_button.innerHTML;
|
||
|
subscribe_button.onclick = unsubscribe;
|
||
6 years ago
|
subscribe_button.innerHTML = '<b>' + subscribe_data.unsubscribe_text + ' | ' + subscribe_data.sub_count_text + '</b>';
|
||
6 years ago
|
|
||
6 years ago
|
xhr.onreadystatechange = function () {
|
||
6 years ago
|
if (xhr.readyState == 4) {
|
||
6 years ago
|
if (xhr.status != 200) {
|
||
|
subscribe_button.onclick = subscribe;
|
||
|
subscribe_button.innerHTML = fallback;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
xhr.ontimeout = function () {
|
||
6 years ago
|
console.log('Subscribing timed out.');
|
||
6 years ago
|
subscribe(timeouts + 1);
|
||
|
};
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
function unsubscribe(timeouts = 0) {
|
||
|
if (timeouts > 10) {
|
||
6 years ago
|
console.log('Failed to subscribe');
|
||
6 years ago
|
return;
|
||
|
}
|
||
|
|
||
6 years ago
|
var url = '/subscription_ajax?action_remove_subscriptions=1&redirect=false' +
|
||
6 years ago
|
'&c=' + subscribe_data.ucid +
|
||
|
'&referer=' + location.pathname + location.search;
|
||
6 years ago
|
var xhr = new XMLHttpRequest();
|
||
6 years ago
|
xhr.responseType = 'json';
|
||
6 years ago
|
xhr.timeout = 20000;
|
||
6 years ago
|
xhr.open('POST', url, true);
|
||
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
||
6 years ago
|
xhr.send('csrf_token=' + subscribe_data.csrf_token);
|
||
6 years ago
|
|
||
6 years ago
|
var fallback = subscribe_button.innerHTML;
|
||
|
subscribe_button.onclick = subscribe;
|
||
6 years ago
|
subscribe_button.innerHTML = '<b>' + subscribe_data.subscribe_text + ' | ' + subscribe_data.sub_count_text + '</b>';
|
||
6 years ago
|
|
||
6 years ago
|
xhr.onreadystatechange = function () {
|
||
6 years ago
|
if (xhr.readyState == 4) {
|
||
6 years ago
|
if (xhr.status != 200) {
|
||
|
subscribe_button.onclick = unsubscribe;
|
||
|
subscribe_button.innerHTML = fallback;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
xhr.ontimeout = function () {
|
||
6 years ago
|
console.log('Unsubscribing timed out.');
|
||
6 years ago
|
unsubscribe(timeouts + 1);
|
||
|
};
|
||
6 years ago
|
}
|