4chan/js/tcaptcha.min.js
2025-04-17 14:46:47 -05:00

1 line
No EOL
11 KiB
JavaScript

var TCaptcha={node:null,frameNode:null,imgCntNode:null,bgNode:null,fgNode:null,msgNode:null,sliderNode:null,respNode:null,reloadNode:null,helpNode:null,challengeNode:null,ticketCaptchaNode:null,challenge:null,reloadTs:null,reloadTimeout:null,expireTimeout:null,frameTimeout:null,pcdBypassable:!1,errorCb:null,path:"/captcha",ticketKey:"4chan-tc-ticket",domain:"4chan.org",failCd:60,tabindex:null,hCaptchaSiteKey:"49d294fa-f15c-41fc-80ba-c2544c52ec2a",init:function(e,t,o,a){this.node&&this.destroy(),a&&(this.tabindex=a),this.node=e,e.style.position="relative",e.style.width="300px",this.frameNode=null,this.imgCntNode=this.buildImgCntNode(),this.bgNode=this.buildImgNode("bg"),this.fgNode=this.buildImgNode("fg"),this.sliderNode=this.buildSliderNode(),this.respNode=this.buildRespField(),this.reloadNode=this.buildReloadNode(t,o),this.helpNode=this.buildHelpNode(),this.msgNode=this.buildMsgNode(),this.challengeNode=this.buildChallengeNode(),e.appendChild(this.reloadNode),e.appendChild(this.respNode),e.appendChild(this.helpNode),this.imgCntNode.appendChild(this.bgNode),this.imgCntNode.appendChild(this.fgNode),e.appendChild(this.imgCntNode),e.appendChild(this.sliderNode),e.appendChild(this.msgNode),e.appendChild(this.challengeNode),window.addEventListener("message",this.onFrameMessage)},destroy:function(){let e=TCaptcha;e.node&&(window.removeEventListener("message",e.onFrameMessage),clearTimeout(e.frameTimeout),clearTimeout(e.reloadTimeout),clearTimeout(e.expireTimeout),e.node.textContent="",e.node=null,e.frameNode=null,e.imgCntNode=null,e.bgNode=null,e.fgNode=null,e.msgNode=null,e.sliderNode=null,e.respNode=null,e.reloadNode=null,e.helpNode=null,e.challengeNode=null,e.ticketCaptchaNode=null,e.challenge=null,e.pcdBypassable=!1,e.errorCb=null,e.reloadTs=null,e.onReloadCdDone=null)},setErrorCb:function(e){TCaptcha.errorCb=e},toggleImgCntNode:function(e){TCaptcha.imgCntNode.style.display=e?"block":"none"},getTicket:function(){return localStorage.getItem(TCaptcha.ticketKey)},setTicket:function(e){e?localStorage.setItem(TCaptcha.ticketKey,e):!1===e&&localStorage.removeItem(TCaptcha.ticketKey)},buildFrameNode:function(){let e=document.createElement("iframe");return e.id="t-frame",e.style.border="0",e.style.width="100%",e.style.height="80px",e.style.marginTop="2px",e.style.position="relative",e.style.display="block",e.onerror=TCaptcha.onFrameError,e},buildImgCntNode:function(){let e=document.createElement("div");return e.id="t-cnt",e.style.height="80px",e.style.marginTop="2px",e.style.position="relative",e},buildImgNode:function(e){let t=document.createElement("div");return t.id="t-"+e,t.style.width="100%",t.style.height="100%",t.style.position="absolute",t.style.backgroundRepeat="no-repeat",t.style.backgroundPosition="top left",t.style.pointerEvents="none",t},buildMsgNode:function(){let e=document.createElement("div");return e.id="t-msg",e.style.width="100%",e.style.height="calc(100% - 20px)",e.style.position="absolute",e.style.top="20px",e.style.textAlign="center",e.style.fontSize="12px",e.style.filter="inherit",e.style.display="none",e.style.alignContent="center",e},buildRespField:function(){let e=document.createElement("input");return e.id="t-resp",e.name="t-response",e.placeholder="Type the CAPTCHA here",e.setAttribute("autocomplete","off"),e.type="text",e.style.width="160px",e.style.boxSizing="border-box",e.style.textTransform="uppercase",e.style.fontSize="11px",e.style.height="18px",e.style.margin="0",e.style.padding="0 2px",e.style.fontFamily="monospace",e.style.verticalAlign="middle",this.tabindex&&e.setAttribute("tabindex",this.tabindex+2),e},buildSliderNode:function(){let e=document.createElement("input");return e.id="t-slider",e.setAttribute("autocomplete","off"),e.type="range",e.style.width="100%",e.style.boxSizing="border-box",e.style.visibility="hidden",e.style.margin="0",e.style.transition="box-shadow 15s linear",e.style.boxShadow="0 0 6px 4px #1d8dc4",e.style.position="relative",e.value=0,e.min=0,e.max=100,e.addEventListener("input",this.onSliderInput,!1),this.tabindex&&e.setAttribute("tabindex",this.tabindex+1),e},buildChallengeNode:function(){let e=document.createElement("input");return e.name="t-challenge",e.type="hidden",e},buildReloadNode:function(e,t){let o=document.createElement("button");return o.id="t-load",o.type="button",o.style.fontSize="11px",o.style.padding="0",o.style.width="90px",o.style.boxSizing="border-box",o.style.margin="0 6px 0 0",o.style.verticalAlign="middle",o.style.height="18px",o.textContent="Get Captcha",o.setAttribute("data-board",e),o.setAttribute("data-tid",t),o.addEventListener("click",this.onReloadClick,!1),this.tabindex&&o.setAttribute("tabindex",this.tabindex),o},buildHelpNode:function(){let e=document.createElement("button");return e.id="t-help",e.type="button",e.style.fontSize="11px",e.style.padding="0",e.style.width="20px",e.style.boxSizing="border-box",e.style.margin="0 0 0 6px",e.style.verticalAlign="middle",e.style.height="18px",e.textContent="?",e.setAttribute("data-tip","Help"),e.tabIndex=-1,e.addEventListener("click",this.onHelpClick,!1),e},onHelpClick:function(){alert("- Only type letters and numbers displayed in the image.\n- If needed, use the slider to align the image to make it readable.\n- Make sure to not block any cookies set by 4chan.")},onTicketCaptchaError:function(){TCaptcha.toggleMsgOverlay(!0,"Couldn't load the captcha.<br><br>Please check your browser's content blocker.")},onTicketCaptchaDone:function(e){TCaptcha.reloadNode.setAttribute("data-ticket-resp",e),TCaptcha.destroyTicketCaptcha(),TCaptcha.onReloadClick()},loadTicketCaptcha:function(){window.pcd_c_loaded=TCaptcha.buildTicketCaptcha,window.pcd_c_done=TCaptcha.onTicketCaptchaDone,TCaptcha.toggleMsgOverlay(!0,"Loading\u2026");let e=document.createElement("script");e.src="https://js.hcaptcha.com/1/api.js?onload=pcd_c_loaded&render=explicit&recaptchacompat=off",e.onerror=TCaptcha.onTicketCaptchaError,document.head.appendChild(e)},buildTicketCaptcha:function(){let e=TCaptcha;if(e.toggleMsgOverlay(!1),!window.hcaptcha)return void e.loadTicketCaptcha();let t=document.createElement("div");t.id="t-tc-cnt",e.imgCntNode.appendChild(t);let o=window.hcaptcha.render("t-tc-cnt",{sitekey:e.hCaptchaSiteKey,callback:"pcd_c_done"});t.setAttribute("data-wid",o),e.ticketCaptchaNode=t},destroyTicketCaptcha:function(){let e=TCaptcha;if(!window.hcaptcha||!e.ticketCaptchaNode)return;let t=e.ticketCaptchaNode.getAttribute("data-wid");window.hcaptcha.reset(t),e.imgCntNode.removeChild(e.ticketCaptchaNode),e.ticketCaptchaNode=null},onReloadClick:function(){let e=TCaptcha.reloadNode,t=e.getAttribute("data-board"),o=e.getAttribute("data-tid"),a=e.getAttribute("data-ticket-resp");e.removeAttribute("data-ticket-resp"),TCaptcha.toggleReloadBtn(!1,"Loading"),TCaptcha.load(t,o,a)},onFrameMessage:function(e){e.origin===`https://sys.${TCaptcha.domain}`&&e.data&&e.data.twister&&(TCaptcha.destroyFrame(),TCaptcha.buildFromJson(e.data.twister))},onFrameError:function(e){TCaptcha.unlockReloadBtn(),console.log(e),TCaptcha.errorCb&&TCaptcha.errorCb.call(null,"Couldn't load the captcha frame. Check your content blocker settings.")},load:function(e,t,o){let a=TCaptcha;clearTimeout(a.frameTimeout),clearTimeout(a.reloadTimeout),clearTimeout(a.expireTimeout);let l=["framed=1"];e&&l.push("board="+e),t>0&&l.push("thread_id="+t),o&&l.push("ticket_resp="+encodeURIComponent(o));let i=a.getTicket();i&&l.push("ticket="+i),l.length>0&&(l="?"+l.join("&"));let d="https://sys."+a.domain+a.path+l;a.frameNode=a.buildFrameNode(),a.toggleImgCntNode(!1),a.node.insertBefore(a.frameNode,a.imgCntNode),a.frameTimeout=setTimeout(a.onFrameTimeout,6e4,d),a.frameNode.src=d},onFrameTimeout:function(e){let t=TCaptcha;t.destroyFrame(),console.log("Captcha frame timeout"),t.errorCb&&t.errorCb.call(null,`Couldn't get the captcha.\nMake sure your browser doesn't block content on 4chan then click\n<a href="${e.replace("framed=1","opened=1")}" target="_blank">here</a>.`)},destroyFrame:function(){let e=TCaptcha;clearTimeout(e.frameTimeout),e.frameTimeout=null,e.frameNode&&(e.frameNode.remove(),e.frameNode=null),e.toggleImgCntNode(!0),e.unlockReloadBtn()},unlockReloadBtn:function(){TCaptcha.reloadTs=null,TCaptcha.toggleReloadBtn(!0,"Get Captcha")},toggleReloadBtn:function(e,t){let o=TCaptcha;o.reloadNode&&(o.reloadNode.disabled=!e,t!==undefined&&(o.reloadNode.textContent=t))},onCaptchaFailed:function(){let e=TCaptcha,t=1e3*e.failCd;e.reloadTs&&e.reloadTs<t&&e.setReloadCd(t,!0)},setReloadCd:function(e,t,o){let a=TCaptcha;a.node&&(clearTimeout(a.reloadTimeout),a.onReloadCdDone=o,a.pcdBypassable=-1===t,e?(a.toggleReloadBtn(!1),t?(a.reloadTs=Date.now()+e,a.onReloadCdTick()):a.reloadTimeout=setTimeout(a.stopReloadCd,e)):a.stopReloadCd())},stopReloadCd:function(){let e=TCaptcha;e.unlockReloadBtn(),e.onReloadCdDone&&e.onReloadCdDone.call(e)},onReloadCdTick:function(){let e=TCaptcha;if(!e.reloadNode||!e.reloadTs)return;let t=e.reloadTs-Date.now();e.pcdBypassable&&-1!==document.cookie.indexOf("_ev1=")&&(t=0),t>0?(e.reloadNode.textContent=Math.ceil(t/1e3),e.reloadTimeout=setTimeout(e.onReloadCdTick,Math.min(t,1e3))):e.stopReloadCd()},clearChallenge:function(){let e=TCaptcha;e.node&&(e.challengeNode.value="",e.respNode.value="",e.fgNode.style.backgroundImage="",e.bgNode.style.backgroundImage="",e.toggleSlider(!1),e.toggleMsgOverlay(!1))},toggleSlider:function(e){TCaptcha.sliderNode.style.visibility=e?"":"hidden",TCaptcha.sliderNode.style.boxShadow=e?"":"0 0 4px 2px #1d8dc4"},toggleMsgOverlay:function(e,t){t!==undefined&&(TCaptcha.msgNode.innerHTML=`<div>${t}</div>`),TCaptcha.msgNode.style.display=e?"grid":"none"},onSliderInput:function(){var e=-Math.floor(+this.value/100*this.twisterDelta);TCaptcha.bgNode.style.backgroundPositionX=e+"px"},onTicketPcdTick:function(){let e=TCaptcha,t=document.getElementById("t-pcd");if(!t)return;let o=+t.getAttribute("data-pcd");(o-=0|Date.now()/1e3)<=0?e.onTicketPcdEnd():(t.textContent=o,setTimeout(e.updateTicketPcd,1e3))},clearTicketOverlay:function(){TCaptcha.toggleMsgOverlay(!1)},buildFromJson:function(e){let t=TCaptcha;if(t.node){if(t.unlockReloadBtn(),t.toggleSlider(!1),t.toggleMsgOverlay(!1),t.setTicket(e.ticket),TCaptcha.errorCb&&TCaptcha.errorCb.call(null,""),e.cd&&t.setReloadCd(1e3*e.cd,!e.challenge),e.mpcd)return t.clearChallenge(),t.destroyTicketCaptcha(),void t.buildTicketCaptcha();if(e.pcd)t.buildTicket(e);else{if(e.error)return console.log(e.error),void(TCaptcha.errorCb&&TCaptcha.errorCb.call(null,e.error));t.imgCntNode.style.width=e.img_width+"px",t.imgCntNode.style.height=e.img_height+"px",t.challengeNode.value=e.challenge,t.expireTimeout=setTimeout(t.clearChallenge,1e3*e.ttl-3e3),e.bg_width?t.buildTwister(e):e.img?t.buildStatic(e):t.buildNoop(e)}}},buildTwister:function(e){let t=TCaptcha;t.fgNode.style.backgroundImage="url(data:image/png;base64,"+e.img+")",t.bgNode.style.backgroundImage="url(data:image/png;base64,"+e.bg+")",t.bgNode.style.backgroundPositionX="0px",t.toggleSlider(!0),t.sliderNode.value=0,t.sliderNode.twisterDelta=e.bg_width-e.img_width,t.sliderNode.focus()},buildStatic:function(e){let t=TCaptcha;t.fgNode.style.backgroundImage="url(data:image/png;base64,"+e.img+")",t.bgNode.style.backgroundImage=""},buildTicket:function(e){let t=TCaptcha;t.toggleMsgOverlay(!0,e.pcd_msg||"Please wait a while."),t.fgNode.style.backgroundImage="",t.bgNode.style.backgroundImage="",t.setReloadCd(1e3*e.pcd,!e.bpcd||-1,t.clearTicketOverlay)},buildNoop:function(){let e=TCaptcha;e.toggleMsgOverlay(!0,"Verification not required."),e.fgNode.style.backgroundImage="",e.bgNode.style.backgroundImage=""}};