goCaptcha/web/goCaptcha.js
web/goCaptcha.jsRaw
1
2var selection = [0, 0, 0, 0, 0, 0];
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}
26
27function showCaptcha(captcha) {
28 var html;
29 html = "";
30 html += "<h2>Select all " + captcha.question + "s</h2>";
31 for (k in captcha.imgs) {
32 html += "<img id='" + k + "' onclick='selectCaptchaImg(this)' src='" + goCaptchaURL + "/image/" + captcha.imgs[k] + "' style='width:150px;cursor:pointer;' />";
33 }
34 html += "<div onclick='validateCaptcha()' class='g_button c_blue300 g_floatRight'>Validate</div>";
35 document.getElementById("goCaptcha").innerHTML = html;
36}
37
38function selectCaptchaImg(elem) {
39 if (selection[elem.id] == 0) {
40 selection[elem.id] = 1;
41 document.getElementById(elem.id).className = "g_selected";
42 } else {
43 selection[elem.id] = 0;
44 document.getElementById(elem.id).className = "g_unselected";
45 }
46}
47
48function validateCaptcha() {
49 var answer = {
50 selection: selection,
51 captchaid: captcha.id
52 };
53 data = httpPost(goCaptchaURL + "/answer", JSON.stringify(answer));
54 resp = JSON.parse(data);
55 var html = "";
56 if (resp) {
57 html += "<h2>goCaptcha validated</h2>";
58 } else {
59 selection = [0, 0, 0, 0, 0, 0];
60 html += "<h2>goCaptcha failed</h2>";
61 html += "<div onclick='getCaptcha()' class='g_button c_red300 g_floatRight'>Reload Captcha</div>";
62 }
63 document.getElementById("goCaptcha").innerHTML = html;
64}
65
66if (document.getElementById("goCaptcha")) {
67 getCaptcha();
68}