captcha_aggregator/public/js/index.js

21 lines
862 B
JavaScript

window.onload = async () => {
let id = (await (await fetch("/api/captcha/new", {method: "POST"})).json()).id
console.log(id);
fetch(`/api/captcha/${id}`).then(response => response.blob())
.then(blob => {
const url = URL.createObjectURL(blob);
document.getElementById("captcha_image").src = url;
}
);
const form = document.getElementById("captchaForm");
const inputField = document.getElementById("captcha");
form.addEventListener('submit', async (e) => {
e.preventDefault();
const response = await fetch(`/api/captcha/${id}`, {method: "PATCH",headers: {'Content-Type': 'application/json'}, body: JSON.stringify({"solution": inputField.value})});
if (response.status == 200) {
inputField.value = "";
window.location.reload();
};
})
};