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.

68 lines
2.0 KiB

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