goCaptcha/web/goCaptcha.js
web/goCaptcha.jsRaw
1
2var selection = [];
3var captchaid = "";
4
5function httpGet(url) {
6 var xmlHttp = new XMLHttpRequest();
7 xmlHttp.open("GET", url, false); // false for synchronous request
8 xmlHttp.send(null);
9 return xmlHttp.responseText;
10}
11
12function httpPost(url, data) {
13 var xmlHttp = new XMLHttpRequest();
14 xmlHttp.open("POST", url, false);
15 xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*");
16 xmlHttp.send(data);
17 return xmlHttp.responseText;
18}
19
20function getCaptcha() {
21 data = httpGet(goCaptchaURL + "/captcha")
22 captcha = JSON.parse(data);
23 captchaid = captcha.id;
24 showCaptcha(captcha);
25 selection=[];
26 for(k in captcha.imgs){
27 selection.push(0);
28 }
29}
30
31function showCaptcha(captcha) {
32 var html;
33 html = "";
34 html += "<h2>Select all " + captcha.question + "</h2>";
35 for (k in captcha.imgs) {
36 html += "<img id='" + k + "' onclick='selectCaptchaImg(this)' src='" + goCaptchaURL + "/image/" + captcha.imgs[k] + "' style='width:120px;cursor:pointer;' class='g_unselected' />";
37 }
38 html += "<div onclick='validateCaptcha()' class='g_button c_blue300 g_floatRight'>Validate</div>";
39 document.getElementById("goCaptcha").innerHTML = html;
40}
41
42function selectCaptchaImg(elem) {
43 if (selection[elem.id] == 0) {
44 selection[elem.id] = 1;
45 document.getElementById(elem.id).className = "g_selected";
46 } else {
47 selection[elem.id] = 0;
48 document.getElementById(elem.id).className = "g_unselected";
49 }
50}
51
52function validateCaptcha() {
53 var answer = {
54 selection: selection,
55 captchaid: captcha.id
56 };
57 data = httpPost(goCaptchaURL + "/answer", JSON.stringify(answer));
58 resp = JSON.parse(data);
59 var html = "";
60 if (resp) {
61 html += "<h2>goCaptcha validated</h2>";
62 html += "<div onclick='getCaptcha()' class='g_button c_green300 g_floatRight'>Reload Captcha</div>";
63 } else {
64 html += "<h2>goCaptcha failed</h2>";
65 html += "<div onclick='getCaptcha()' class='g_button c_red300 g_floatRight'>Reload Captcha</div>";
66 }
67 document.getElementById("goCaptcha").innerHTML = html;
68}
69
70if (document.getElementById("goCaptcha")) {
71 getCaptcha();
72}