2023-11-23 00:10:39 +06:00
|
|
|
|
(function (document) {
|
|
|
|
|
const captchaBlocks = document.querySelectorAll('div.captcha-service-kor-elf');
|
|
|
|
|
captchaBlocks.forEach(function (element) {
|
|
|
|
|
const shadow = element.attachShadow({ mode: 'closed' });
|
|
|
|
|
const button = document.createElement("button");
|
|
|
|
|
const style = document.createElement('link');
|
|
|
|
|
const domain = element.getAttribute('data-domain');
|
|
|
|
|
const token = element.getAttribute('data-token');
|
2023-12-02 01:44:21 +06:00
|
|
|
|
const staticPath = element.getAttribute('data-static-path');
|
2023-12-02 21:27:06 +06:00
|
|
|
|
const captchaVerifiedName = element.getAttribute('data-captcha-verified-name');
|
2023-11-23 00:10:39 +06:00
|
|
|
|
|
|
|
|
|
let windowCaptcha = null;
|
|
|
|
|
|
|
|
|
|
style.rel = 'stylesheet';
|
|
|
|
|
style.type = 'text/css';
|
2023-12-02 01:44:21 +06:00
|
|
|
|
style.href = staticPath + '/style.css';
|
2023-11-23 00:10:39 +06:00
|
|
|
|
shadow.appendChild(style);
|
|
|
|
|
|
|
|
|
|
button.textContent = 'Я не робот!';
|
|
|
|
|
button.type = 'button';
|
|
|
|
|
button.classList.add('button-open-window-captcha');
|
|
|
|
|
shadow.appendChild(button);
|
|
|
|
|
button.addEventListener('click', function () {
|
|
|
|
|
if (windowCaptcha === null) {
|
2023-12-02 21:27:06 +06:00
|
|
|
|
windowCaptcha = createWindowCaptcha(shadow, domain, token, captchaVerifiedName);
|
2023-11-23 00:10:39 +06:00
|
|
|
|
}
|
|
|
|
|
windowCaptcha.style.display = 'block';
|
2023-12-02 21:27:06 +06:00
|
|
|
|
actionGetCaptcha(windowCaptcha, domain, token, shadow, captchaVerifiedName);
|
2023-11-23 00:10:39 +06:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-12-02 21:27:06 +06:00
|
|
|
|
function createWindowCaptcha (shadow, domain, token, captchaVerifiedName) {
|
2023-11-23 00:10:39 +06:00
|
|
|
|
const windowCaptcha = document.createElement('div');
|
|
|
|
|
windowCaptcha.classList.add('window-captcha');
|
|
|
|
|
windowCaptcha.innerHTML = `<div class="window-captcha__content">
|
|
|
|
|
<div class="window-captcha__content__header">Я не робот! <button type="button" class="window-captcha__reload"></button><button type="button" class="window-captcha__close">X</button></div>
|
|
|
|
|
<div class="window-captcha__content__body"></div>
|
|
|
|
|
</div>`;
|
|
|
|
|
shadow.appendChild(windowCaptcha);
|
|
|
|
|
|
|
|
|
|
windowCaptcha.addEventListener('click', function (event) {
|
|
|
|
|
if (event.target !== windowCaptcha) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
actionClose(windowCaptcha);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
windowCaptcha.querySelector('button.window-captcha__close').addEventListener('click', function () {
|
2023-12-02 01:44:21 +06:00
|
|
|
|
actionClose(windowCaptcha);
|
2023-11-23 00:10:39 +06:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
windowCaptcha.querySelector('button.window-captcha__reload').addEventListener('click', function () {
|
2023-12-02 21:27:06 +06:00
|
|
|
|
actionGetCaptcha(windowCaptcha, domain, token, shadow, captchaVerifiedName);
|
2023-11-23 00:10:39 +06:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return windowCaptcha;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function actionClose(windowCaptcha)
|
|
|
|
|
{
|
|
|
|
|
windowCaptcha.style.display = 'none';
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 21:27:06 +06:00
|
|
|
|
function actionGetCaptcha(windowCaptcha, domain, token, shadow, captchaVerifiedName)
|
2023-11-23 00:10:39 +06:00
|
|
|
|
{
|
|
|
|
|
let bodyBlock = windowCaptcha.querySelector('div.window-captcha__content__body');
|
|
|
|
|
if (bodyBlock.querySelectorAll('.loading').length > 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
bodyBlock.innerHTML = '<div class="loading"></div>';
|
|
|
|
|
fetch(domain + '/api/v1/captcha', {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
'public-token': token
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-12-02 01:44:21 +06:00
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(function (data) {
|
|
|
|
|
let originalWidth = data.image_body.width;
|
|
|
|
|
let originalHeight = data.image_body.height;
|
|
|
|
|
bodyBlock.innerHTML = `
|
2023-11-23 00:10:39 +06:00
|
|
|
|
<form method="post">
|
|
|
|
|
<div class="window-captcha__content__body__head"><img src="${ data.image_head.base64 }" width="100%"></div>
|
|
|
|
|
<p>Выберите значение в том порядке, на котором на картинке выше:</p>
|
|
|
|
|
<div class="window-captcha__content__body__coordinator"><img src="${ data.image_body.base64 }" width="100%" /></div>
|
|
|
|
|
<input type="hidden" name="captcha_key" class="captcha_key" value="${ data.captcha_key }" />
|
|
|
|
|
<button type="button" class="window-captcha__content__body__button">Я не робот!</button>
|
|
|
|
|
</form>
|
|
|
|
|
`;
|
2023-12-02 01:44:21 +06:00
|
|
|
|
let blockCoordinator = bodyBlock.querySelector('div.window-captcha__content__body__coordinator');
|
|
|
|
|
let blockCoordinatorImg = blockCoordinator.querySelector('img');
|
|
|
|
|
blockCoordinatorImg.addEventListener('click', function (event) {
|
|
|
|
|
let pointer = document.createElement('div');
|
|
|
|
|
pointer.style.left = ( event.offsetX / blockCoordinatorImg.width * 100 ) + '%';
|
|
|
|
|
pointer.style.top = ( event.offsetY / blockCoordinatorImg.height * 100 ) + '%';
|
|
|
|
|
pointer.classList.add('pointer');
|
|
|
|
|
|
|
|
|
|
let coordinatorX = event.offsetX * (originalWidth / blockCoordinatorImg.width)
|
|
|
|
|
let coordinatorY = event.offsetY * (originalHeight / blockCoordinatorImg.height)
|
|
|
|
|
|
|
|
|
|
let pointerNumber = blockCoordinator.querySelectorAll('.pointer').length + 1
|
|
|
|
|
pointer.innerHTML = `
|
2023-11-23 00:10:39 +06:00
|
|
|
|
<span class="pounter__number">${ pointerNumber }</span>
|
|
|
|
|
<input type="hidden" class="x" name="pointer[][x]" value="${ Math.round(coordinatorX) }" />
|
|
|
|
|
<input type="hidden" class="y" name="pointer[][y]" value="${ Math.round(coordinatorY) }" />
|
|
|
|
|
`;
|
|
|
|
|
|
2023-12-02 01:44:21 +06:00
|
|
|
|
pointer.addEventListener('click', function () {
|
|
|
|
|
pointer.remove();
|
|
|
|
|
blockCoordinator.querySelectorAll('.pointer').forEach(function (elementPointer, pointIndex) {
|
|
|
|
|
elementPointer.querySelector('span.pounter__number').textContent = pointIndex + 1;
|
|
|
|
|
});
|
2023-11-23 00:10:39 +06:00
|
|
|
|
});
|
|
|
|
|
|
2023-12-02 01:44:21 +06:00
|
|
|
|
blockCoordinator.appendChild(pointer);
|
|
|
|
|
});
|
2023-11-23 00:10:39 +06:00
|
|
|
|
|
2023-12-02 01:44:21 +06:00
|
|
|
|
bodyBlock.querySelector('.window-captcha__content__body__button').addEventListener('click', function () {
|
2023-12-02 21:27:06 +06:00
|
|
|
|
checkingCaptcha(bodyBlock, domain, token, shadow, captchaVerifiedName);
|
2023-12-02 01:44:21 +06:00
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
bodyBlock.innerHTML = `<div class="window-captcha__content__body__error">Произошла ошибка, сервис с каптчей не ответил. Попробуйте ещё раз!</div>`;
|
2023-11-23 00:10:39 +06:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 21:27:06 +06:00
|
|
|
|
function checkingCaptcha (bodyBlock, domain, token, shadow, captchaVerifiedName) {
|
2023-11-23 00:10:39 +06:00
|
|
|
|
let button = bodyBlock.querySelector('button.window-captcha__content__body__button');
|
|
|
|
|
if (button.querySelectorAll('.loading').length > 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
let loadingSpan = document.createElement('span');
|
|
|
|
|
loadingSpan.classList.add('loading');
|
|
|
|
|
button.appendChild(loadingSpan);
|
|
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
|
captcha_key: bodyBlock.querySelector('.captcha_key').value,
|
|
|
|
|
verification: []
|
|
|
|
|
};
|
|
|
|
|
bodyBlock.querySelectorAll('.pointer').forEach(function (elementPointer) {
|
|
|
|
|
data["verification"].push({
|
|
|
|
|
x: elementPointer.querySelector('.x').value,
|
|
|
|
|
y: elementPointer.querySelector('.y').value
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fetch(domain + '/api/v1/captcha', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
'public-token': token
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
})
|
2023-12-02 01:44:21 +06:00
|
|
|
|
.then(response => {
|
|
|
|
|
if (response.status === 429) {
|
|
|
|
|
setError('Вы превысили количество попыток. Обновите каптчу.', button);
|
|
|
|
|
return
|
2023-11-23 00:10:39 +06:00
|
|
|
|
}
|
2023-12-02 01:44:21 +06:00
|
|
|
|
return response.json();
|
|
|
|
|
})
|
|
|
|
|
.then(function (data) {
|
|
|
|
|
if (typeof data.errors !== 'undefined') {
|
|
|
|
|
let errorMessage = '';
|
|
|
|
|
for (let key in data.errors) {
|
|
|
|
|
errorMessage += data.errors[key] + '<br>';
|
|
|
|
|
}
|
|
|
|
|
if (errorMessage === '') {
|
|
|
|
|
errorMessage = data.message;
|
|
|
|
|
}
|
|
|
|
|
setError(errorMessage, button);
|
|
|
|
|
return
|
2023-11-23 01:10:04 +06:00
|
|
|
|
}
|
2023-12-02 01:44:21 +06:00
|
|
|
|
if (typeof data.captcha_key === 'undefined') {
|
|
|
|
|
setError('Произошла ошибка!', button);
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
shadow.querySelector('button.button-open-window-captcha').remove();
|
|
|
|
|
let captchaVerified = document.createElement('div');
|
2023-12-02 21:27:06 +06:00
|
|
|
|
captchaVerified.innerHTML = `<span class="captcha-verified">Ура!!! Проверку прошли!</span><input type="hidden" name="${ captchaVerifiedName }" value="${ data.captcha_key }">`;
|
2023-12-02 01:44:21 +06:00
|
|
|
|
shadow.appendChild(captchaVerified);
|
|
|
|
|
shadow.querySelector('.window-captcha').remove();
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
setError('Произошла ошибка!', button)
|
|
|
|
|
}).finally(function () {
|
2023-11-23 00:10:39 +06:00
|
|
|
|
loadingSpan.remove();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setError(message, button) {
|
|
|
|
|
let errorBlock = document.createElement('div');
|
|
|
|
|
errorBlock.classList.add("error-message");
|
|
|
|
|
errorBlock.innerHTML = message;
|
|
|
|
|
button.before(errorBlock);
|
|
|
|
|
|
|
|
|
|
setTimeout( function () {
|
|
|
|
|
errorBlock.remove();
|
|
|
|
|
}, 3000);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 01:44:21 +06:00
|
|
|
|
})(document);
|