service-captcha-gui/production/script.js

5 lines
4.8 KiB
JavaScript
Raw Normal View History

/*
* Copyright (C) 2023 Leonid Nikitin (kor-elf)
* https://git.kor-elf.net/kor-elf/service-captcha-gui/src/branch/main/LICENSE.md
*/
2023-12-02 02:37:12 +06:00
!function(e){let t=e.querySelectorAll("div.captcha-service-kor-elf");function n(e){e.style.display="none"}function c(t,n,c,a){let i=t.querySelector("div.window-captcha__content__body");if(i.querySelectorAll(".loading").length>0)return null;i.innerHTML='<div class="loading"></div>',fetch(n+"/api/v1/captcha",{headers:{"Content-Type":"application/json",Accept:"application/json","public-token":c}}).then(e=>e.json()).then(function(t){let r=t.image_body.width,l=t.image_body.height;i.innerHTML=`<form method="post"><div class="window-captcha__content__body__head"><img src="${t.image_head.base64}" width="100%"></div><p>Выберите значение в том порядке, на котором на картинке выше:</p><div class="window-captcha__content__body__coordinator"><img src="${t.image_body.base64}" width="100%" /></div><input type="hidden" name="captcha_key" class="captcha_key" value="${t.captcha_key}" /><button type="button" class="window-captcha__content__body__button">Я не робот!</button></form>`;let d=i.querySelector("div.window-captcha__content__body__coordinator"),s=d.querySelector("img");s.addEventListener("click",function(t){let n=e.createElement("div");n.style.left=t.offsetX/s.width*100+"%",n.style.top=t.offsetY/s.height*100+"%",n.classList.add("pointer");let c=t.offsetX*(r/s.width),o=t.offsetY*(l/s.height),a=d.querySelectorAll(".pointer").length+1;n.innerHTML=`<span class="pounter__number">${a}</span><input type="hidden" class="x" name="pointer[][x]" value="${Math.round(c)}" /><input type="hidden" class="y" name="pointer[][y]" value="${Math.round(o)}" />`,n.addEventListener("click",function(){n.remove(),d.querySelectorAll(".pointer").forEach(function(e,t){e.querySelector("span.pounter__number").textContent=t+1})}),d.appendChild(n)}),i.querySelector(".window-captcha__content__body__button").addEventListener("click",function(){(function t(n,c,a,i){let r=n.querySelector("button.window-captcha__content__body__button");if(r.querySelectorAll(".loading").length>0)return null;let l=e.createElement("span");l.classList.add("loading"),r.appendChild(l);let d={captcha_key:n.querySelector(".captcha_key").value,verification:[]};n.querySelectorAll(".pointer").forEach(function(e){d.verification.push({x:e.querySelector(".x").value,y:e.querySelector(".y").value})}),fetch(c+"/api/v1/captcha",{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json","public-token":a},body:JSON.stringify(d)}).then(e=>{if(429===e.status){o("Вы превысили количество попыток. Обновите каптчу.",r);return}return e.json()}).then(function(t){if(void 0!==t.errors){let n="";for(let c in t.errors)n+=t.errors[c]+"<br>";""===n&&(n=t.message),o(n,r);return}if(void 0===t.captcha_key){o("Произошла ошибка!",r);return}i.querySelector("button.button-open-window-captcha").remove();let a=e.createElement("div");a.innerHTML=`<span class="captcha-verified">Ура!!! Проверку прошли!</span><input type="hidden" name="captcha-verified" value="${t.captcha_key}">`,i.appendChild(a),i.querySelector(".window-captcha").remove()}).catch(e=>{o("Произошла ошибка!",r)}).finally(function(){l.remove()})})(i,n,c,a)})}).catch(e=>{i.innerHTML=`<div class="window-captcha__content__body__error">Произошла ошибка, сервис с каптчей не ответил. Попробуйте ещё раз!</div>`})}function o(t,n){let c=e.createElement("div");c.classList.add("error-message"),c.innerHTML=t,n.before(c),setTimeout(function(){c.remove()},3e3)}t.forEach(function(t){let o=t.attachShadow({mode:"closed"}),a=e.createElement("button"),i=e.createElement("link"),r=t.getAttribute("data-domain"),l=t.getAttribute("data-token"),d=t.getAttribute("data-static-path"),s=null;i.rel="stylesheet",i.type="text/css",i.href=d+"/style.css",o.appendChild(i),a.textContent="Я не робот!",a.type="button",a.classList.add("button-open-window-captcha"),o.appendChild(a),a.addEventListener("click",function(){null===s&&(s=function t(o,a,i){let r=e.createElement("div"