You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.2 KiB

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