Files
button/ui/static/ui/button.js
2020-04-24 21:05:19 -04:00

40 lines
1.1 KiB
JavaScript

function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
var button = document.getElementById("BUTTON");
var count = document.getElementById("COUNT");
button.addEventListener("click", event => {
button.disabled = true;
fetch('/button', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken
}
})
.then((response) => {
return response.json();
})
.then((data) => {
count.innerText = data.pressed;
}).finally(() => {
button.disabled = false;
});
});
button.focus();