/* * dynamic-comment-form.js -- Captcha challenge loader * Copyright © 2022 - 2023 Jakob L. Kreuze * * 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 * . */ 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"); primaryDisplay.removeAttribute("hidden"); const altDisplay = document.getElementById("captcha-alternative-wrapper"); let trigger = document.getElementById("captcha-challenge-trigger"); trigger.addEventListener("click", (event) => { let captchaId = document.getElementById("captcha-id"); let wrapper = document.getElementById("captcha-image-wrapper"); 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"]; const img = document.createElement('img'); img.src = challengeData["image"]; // img.src = `data:image/png;base64,${base64Data}`; wrapper.innerHTML = ''; wrapper.appendChild(img); trigger.hidden = true; // Unhide fieldset. altDisplay.removeAttribute("hidden"); }) .catch(console.err); } event.preventDefault(); }); }); window.addEventListener("load", () => { // Unhide additional comment actions if Javascript is enabled. let additionalActionsRows = document.querySelectorAll(".comment-additional-actions"); for (let row of additionalActionsRows) { row.removeAttribute("hidden"); } 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"); }); }); });