2017-06-09 10:33:56 +01:00
window . onload = function ( ) {
2018-04-16 02:01:58 +01:00
2017-06-10 00:21:46 +01:00
var api _url = "https://captcheck.netsyms.com/api.php" ;
2017-06-12 06:22:13 +01:00
2018-04-16 02:01:58 +01:00
function chooseAnswer ( idp , ans ) {
var box = document . getElementById ( "captcheck_" + idp + "_answer_" + ans ) ;
box . checked = true ;
return false ;
}
function switchMode ( idp ) {
var switch _label = document . getElementById ( "captcheck_" + idp + "_alt_question_button" ) ;
var img _q = document . getElementById ( "captcheck_" + idp + "_question_image" ) ;
var acc _q = document . getElementById ( "captcheck_" + idp + "_question_access" ) ;
var img _a = document . getElementById ( "captcheck_" + idp + "_answer_images" ) ;
var acc _a = document . getElementById ( "captcheck_" + idp + "_answer_access" ) ;
if ( switch _label . innerHTML == "> Text mode" ) {
switch _label . innerHTML = "> Image mode" ;
img _q . style . display = "none" ;
acc _q . style . display = "initial" ;
img _a . style . display = "none" ;
acc _a . style . display = "initial" ;
acc _a . innerHTML = "<input type='text' name='captcheck_selected_answer' aria-label='Type your answer here.' autocomplete='off' autofill='off'/>" ;
} else {
switch _label . innerHTML = "> Text mode" ;
img _q . style . display = "initial" ;
acc _q . style . display = "none" ;
img _a . style . display = "initial" ;
acc _a . style . display = "none" ;
acc _a . innerHTML = "" ;
}
}
2017-06-12 06:22:13 +01:00
2018-04-16 02:01:58 +01:00
var nonce = "" ;
2017-06-12 06:22:13 +01:00
/* Loop over all the CAPTCHA containers on the page, setting up a different CAPTCHA in each */
Array . prototype . forEach . call ( document . getElementsByClassName ( "captcheck_container" ) , function ( container ) {
2018-04-16 02:01:58 +01:00
if ( container . dataset . stylenonce ) {
nonce = container . dataset . stylenonce ;
}
2017-06-09 10:33:56 +01:00
var xhr = new XMLHttpRequest ( ) ;
2017-06-12 06:22:13 +01:00
xhr . open ( 'GET' , api _url + "?action=new" , true ) ;
2017-06-09 10:33:56 +01:00
xhr . onreadystatechange = function ( ) {
if ( this . readyState == 4 ) {
2017-06-12 06:22:13 +01:00
var status = this . status ;
var json = this . responseText ;
/* Create captcha div */
var captcha = document . createElement ( "div" ) ;
captcha . setAttribute ( "class" , "captcheck_box" ) ;
container . appendChild ( captcha ) ;
2017-06-09 10:33:56 +01:00
2017-06-12 06:22:13 +01:00
if ( status == 200 ) {
var data = JSON . parse ( json ) ;
// ID prefix to use for this instance
var idp = data . id _prefix ;
/* Create answer buttons */
var answers = "<div class='captcheck_answer_images' id='captcheck_" + idp + "_answer_images'>" ;
for ( var i = 0 , len = data . answers . length ; i < len ; i ++ ) {
var src = api _url + "?action=img&s=" + data . session + "&c=" + data . answers [ i ] ;
2017-11-27 23:11:25 +00:00
answers += "<a class='captcheck_answer_label' href='' data-prefix='" + idp + "' data-answer='" + data . answers [ i ] + "' tabindex='0' aria-role='button'><input id='captcheck_" + idp + "_answer_" + data . answers [ i ] + "' type='radio' name='captcheck_selected_answer' value='" + data . answers [ i ] + "' data-prefix='" + idp + "' data-answer='" + data . answers [ i ] + "' /><img src='" + src + "' data-prefix='" + idp + "' data-answer='" + data . answers [ i ] + "'/></a>" ;
2017-06-12 06:22:13 +01:00
}
answers += "</div>" ;
var answer _div = document . createElement ( "div" ) ;
answer _div . innerHTML = answers + "<div class='captcheck_answer_access' id='captcheck_" + idp + "_answer_access'></div>" ;
/* Create question */
var question _div = document . createElement ( "div" ) ;
question _div . setAttribute ( "class" , "captcheck_label_message" ) ;
question _div . setAttribute ( "id" , "captcheck_" + idp + "_label_message" )
2017-11-27 23:11:25 +00:00
question _div . innerHTML = "<span class='captcheck_question_image' id='captcheck_" + idp + "_question_image'>" + data . question _i + "</span><span class='captcheck_question_access' id='captcheck_" + idp + "_question_access'>" + data . question _a + "</span><a href='' class='captcheck_alt_question_button' data-prefix='" + idp + "' id='captcheck_" + idp + "_alt_question_button' aria-role='button' aria-label='Switch between image and text question' tabindex='0'>> Text mode</a>" ;
2017-06-09 10:33:56 +01:00
2017-06-12 06:22:13 +01:00
/* Add question and answers */
captcha . appendChild ( question _div ) ;
captcha . appendChild ( answer _div ) ;
2017-06-09 10:33:56 +01:00
2017-06-12 06:22:13 +01:00
/* Add hidden session ID element */
var skey _input = document . createElement ( "span" ) ;
skey _input . innerHTML = "<input type='hidden' name='captcheck_session_code' value='" + data . session + "' />" ;
captcha . appendChild ( skey _input ) ;
2018-04-16 02:01:58 +01:00
2017-11-27 23:11:25 +00:00
var answer _buttons = document . querySelectorAll ( ".captcheck_answer_label[data-prefix=\"" + idp + "\"]" ) ;
for ( var i = 0 ; i < answer _buttons . length ; i ++ ) {
answer _buttons [ i ] . addEventListener ( "click" , function ( ev ) {
chooseAnswer ( ev . target . getAttribute ( "data-prefix" ) , ev . target . getAttribute ( "data-answer" ) ) ;
ev . preventDefault ( ) ;
} ) ;
2018-04-16 02:01:58 +01:00
answer _buttons [ i ] . addEventListener ( 'keydown' , function ( ev ) {
2017-11-27 23:11:25 +00:00
if ( ev . key === "Enter" || ev . which === 13 || ev . keyCode === 13 || ev . key === ' ' || ev . which === 32 || ev . keyCode === 32 ) {
chooseAnswer ( ev . target . getAttribute ( "data-prefix" ) , ev . target . getAttribute ( "data-answer" ) ) ;
ev . preventDefault ( ) ;
}
} ) ;
}
document . querySelector ( ".captcheck_alt_question_button[data-prefix=\"" + idp + "\"]" ) . addEventListener ( "click" , function ( ev ) {
switchMode ( ev . target . getAttribute ( "data-prefix" ) ) ;
ev . preventDefault ( ) ;
} ) ;
2018-04-16 02:01:58 +01:00
document . querySelector ( ".captcheck_alt_question_button[data-prefix=\"" + idp + "\"]" ) . addEventListener ( 'keydown' , function ( ev ) {
2017-11-27 23:11:25 +00:00
if ( ev . key === "Enter" || ev . which === 13 || ev . keyCode === 13 || ev . key === ' ' || ev . which === 32 || ev . keyCode === 32 ) {
switchMode ( ev . target . getAttribute ( "data-prefix" ) ) ;
ev . preventDefault ( ) ;
}
} ) ;
2017-06-12 06:22:13 +01:00
} else {
/* Add error message */
captcha . innerHTML = "<span class='captcheck_error_message'>There was a problem loading the CAPTCHA.</span>" ;
}
}
} ;
xhr . send ( ) ;
2017-06-09 10:33:56 +01:00
} ) ;
2018-04-16 02:01:58 +01:00
/* Add custom styles */
var styles = document . createElement ( 'style' ) ;
if ( nonce != "" ) {
styles . setAttribute ( "nonce" , nonce ) ;
2017-06-12 06:22:13 +01:00
}
2018-04-16 02:01:58 +01:00
/* Remove newlines/comments from captcheck.css and put it here */
styles . innerHTML = ".captcheck_box{font-family:Ubuntu,Arial,sans-serif;color:black;border:1px solid #e0e0e0;border-radius:3px;display:inline-block;padding:3px;margin:5px 2px 5px 1px;background-color:#f5f5f5;text-decoration:none}.captcheck_label_message,.captcheck_label_message b{color:black;font-family:Ubuntu,Roboto,Arial,sans-serif}.captcheck_answer_label{border:0}.captcheck_answer_label>input{visibility:hidden;position:absolute}.captcheck_answer_label>input+img{cursor:pointer;border:2px solid transparent;border-radius:3px;min-width:32px;width:18%;max-width:64px}.captcheck_answer_label>input:checked+img{cursor:pointer;border:2px solid #424242;border-radius:3px}.captcheck_error_message{color:red}.captcheck_question_image{display:initial}.captcheck_question_access{display:none}.captcheck_alt_question_button{float:right;font-size:80%;cursor:pointer;color:inherit;text-decoration:inherit;border:0}.captcheck_answer_images{display:initial}.captcheck_answer_access{display:none}" ;
document . body . appendChild ( styles ) ;
2017-06-09 10:33:56 +01:00
}