summaryrefslogtreecommitdiff
path: root/static/js/dynamic-comment-form.js
blob: 4be2d5204af005510c512ce4269fd70d860dcd04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * dynamic-comment-form.js -- Captcha challenge loader
 * Copyright © 2022 - 2023 Jakob L. Kreuze <zerodaysfordays@sdf.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

function makeRequest (method, url) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.onload = function () {
      if (xhr.status >= 200 && xhr.status < 300) {
        resolve(xhr.response);
      } else {
        reject({
          status: xhr.status,
          statusText: xhr.statusText
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: xhr.status,
        statusText: xhr.statusText
      });
    };
    xhr.send();
  });
}

window.addEventListener("load", () => {
  let primaryDisplay = document.getElementById("comment-form-primary");
  primaryDisplay.removeAttribute("hidden");

  let altDisplay = document.getElementById("comment-form-alt");
  altDisplay.remove();

  let trigger = document.getElementById("captcha-challenge-trigger");
  let triggerBlock = document.getElementById("captcha-trigger-block");
  trigger.addEventListener("click", (event) => {
    let captchaField = document.getElementById("comment-captcha");

    let captchaId = document.getElementById("captcha-id");
    let captchaImage = document.getElementById("captcha-image");

    trigger.innerHTML = "Please wait...";
    trigger.disabled = true;

    if (captchaId.value === "") {
      makeRequest("GET", "/api/challenge/captcha")
        .then(function (data) {
          // Update form with parsed values.
          let challengeData = JSON.parse(data);
          captchaId.value = challengeData["challenge-id"];
          captchaImage.src = challengeData["image"];

          // Unhide fieldset.
          captchaField.removeAttribute("hidden");

          // Hide initial trigger.
          triggerBlock.remove();
        })
        .catch(console.err);
    }

    event.preventDefault();
  });
});


window.addEventListener("load", () => {
  let header = document.getElementById("comment-form-header");
  let replyTo = document.getElementById("reply-to");
  let buttons = document.querySelectorAll(".comment-reply-button");
  buttons.forEach((button) => {
    button.addEventListener("click", (event) => {
      header.innerHTML = "Comment form (reply mode)";
      replyTo.value = button.getAttribute("data-reply-to-id");
    });
  });
});