relay requesting (and maybe verifying) to the user

This commit is contained in:
2025-04-30 18:03:56 +03:00
parent 991a4f29a6
commit 92fd491194
22 changed files with 968 additions and 222 deletions

View File

@@ -1,25 +1,73 @@
window.onload = async () => {
let id = (await (await fetch("/api/captcha", {method: "POST"})).json()).id
if (id == undefined) {
alert("Service is unavailable now. Please, try again later.");
return;
const check_solution = async (solution) => {
const body = {
"TotalSum": "78278",
"FnNumber": "9960440301173139",
"ReceiptOperationType": "1",
"DocNumber": "35704",
"DocFiscalSign": "4149689833",
"Captcha": solution,
"DocDateTime": "2022-09-21T20:28:00.000Z"
}
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 result = await fetch("https://check.ofd.ru/Document/FetchReceiptFromFns", {
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
// "Origin": "https://check.ofd.ru"
},
body: JSON.stringify(body),
});
return result.status != 400;
}
const get_cookie = (name) => {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.startsWith(name + '=')) {
const value = cookie.substring(name.length + 1);
console.log(value);
}
}
}
const blobToBase64 = (blob) => {
return new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
}
window.onload = async () => {
console.log("koka: " + get_cookie("JWT"));
console.log("all: " + document.cookie);
if (!document.cookie.includes('JWT')) {
document.location.href = "/login";
}
const response = await fetch("https://check.ofd.ru/api/captcha/common/img");
captcha = await response.blob();
const url = URL.createObjectURL(captcha);
document.getElementById("captcha_image").src = url;
console.log(captcha.type)
const form = document.getElementById("captchaForm");
const inputField = document.getElementById("captcha");
form.addEventListener('submit', async (e) => {
e.preventDefault();
// if (!await check_solution(inputField.value)) {
// alert("Капча решена неверно")
// returnl
// }
const response = await fetch(`/api/captcha/${id}`, {method: "PATCH",headers: {'Content-Type': 'application/json'}, body: JSON.stringify({"solution": inputField.value})});
const response = await fetch(`/api/captcha/submit`, {method: "POST", headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ "image": await blobToBase64(captcha), "solution": inputField.value})});
if (response.status == 200) {
inputField.value = "";
window.location.reload();
};
})
} else {
response_json = await response.json()
alert(response_json.message)
}
});
};