107 lines
3.5 KiB
JavaScript
107 lines
3.5 KiB
JavaScript
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"
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
const blobToBase64 = (blob) => {
|
|
return new Promise((resolve, _) => {
|
|
const reader = new FileReader();
|
|
reader.onloadend = () => resolve(reader.result);
|
|
reader.readAsDataURL(blob);
|
|
});
|
|
}
|
|
|
|
const show_stats = async () => {
|
|
const response = await fetch("/api/user/stats", {
|
|
method: "GET",
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
const stats = (await response.json()).stats;
|
|
const statsText = document.getElementById("topSolversText");
|
|
statsText.innerHTML += '<br/>'
|
|
stats.top_five.forEach(stat => {
|
|
statsText.innerHTML += `<b>${stat.username}: ${stat.count}</b><br/>`;
|
|
});
|
|
statsText.innerHTML += `You have solved ${stats.my_count} captcha(s)`
|
|
}
|
|
|
|
const validate_solution = (settings, solution) => {
|
|
if (solution.length != settings.captcha_length) return false;
|
|
for (let i = 0; i < solution.length; i++) {
|
|
let char = solution[i];
|
|
if (!char.match(settings.captcha_regex)) {
|
|
console.log("Illegal symbol: " + char);
|
|
alert(`Illegal symbol ${char} at position ${i + 1}`);
|
|
return false
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
window.onload = async () => {
|
|
const inputField = document.getElementById("captcha");
|
|
inputField.focus();
|
|
// intentionally do not wait for it
|
|
show_stats();
|
|
if (!document.cookie.includes('JWT')) {
|
|
document.location.href = "/login";
|
|
}
|
|
const settings = await (await fetch("/api/settings")).json();
|
|
const captcha_source_url = settings.captcha_source_url;
|
|
const response = await fetch(captcha_source_url);
|
|
captcha = await response.blob();
|
|
|
|
const url = URL.createObjectURL(captcha);
|
|
document.getElementById("captcha_image").src = url;
|
|
const form = document.getElementById("captchaForm");
|
|
|
|
form.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
if (!validate_solution(settings, inputField.value)) {
|
|
alert("You must specify valid solution!")
|
|
return;
|
|
}
|
|
// if (!await check_solution(inputField.value)) {
|
|
// alert("Капча решена неверно")
|
|
// returnl
|
|
// }
|
|
|
|
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)
|
|
}
|
|
});
|
|
}; |