aboutsummaryrefslogtreecommitdiff
path: root/haunt/static/js
diff options
context:
space:
mode:
Diffstat (limited to 'haunt/static/js')
-rw-r--r--haunt/static/js/dynamic-comment-form.js94
-rw-r--r--haunt/static/js/gallery.js2
-rw-r--r--haunt/static/js/local.js110
-rw-r--r--haunt/static/js/proof-of-work.js109
-rw-r--r--haunt/static/js/rsvp.js2
-rw-r--r--haunt/static/js/section-folds.js2
-rw-r--r--haunt/static/js/webmention.js2
7 files changed, 207 insertions, 114 deletions
diff --git a/haunt/static/js/dynamic-comment-form.js b/haunt/static/js/dynamic-comment-form.js
new file mode 100644
index 0000000..4be2d52
--- /dev/null
+++ b/haunt/static/js/dynamic-comment-form.js
@@ -0,0 +1,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");
+ });
+ });
+});
diff --git a/haunt/static/js/gallery.js b/haunt/static/js/gallery.js
index d7ed13b..038340c 100644
--- a/haunt/static/js/gallery.js
+++ b/haunt/static/js/gallery.js
@@ -1,6 +1,6 @@
/*
* gallery.js -- Simple web-based gallery viewer.
- * Copyright © 2022 Jakob L. Kreuze <zerodaysfordays@sdf.org>
+ * 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
diff --git a/haunt/static/js/local.js b/haunt/static/js/local.js
deleted file mode 100644
index 51e0ea1..0000000
--- a/haunt/static/js/local.js
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * local.js -- Fetch and display local comments.
- * Copyright © 2022 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 getData(url, callback) {
- let xhr = new XMLHttpRequest();
- xhr.onload = function(data) {
- callback(data);
- }
- xhr.onerror = function(error) {
- throw new Error(`Request failed: ${error}`);
- }
-}
-
-function makeComment(comment) {
- const strip = (uri) => {
- const sep = "://";
- return uri.substring(uri.indexOf(sep) + sep.length);
- };
-
- const element = (type, attributes) => {
- let res = document.createElement(type);
- for (const attribute in attributes) {
- res.setAttribute(attribute, attributes[attribute]);
- }
- return res;
- };
-
- // Create the h-card section.
- let avatar = element("img", {
- "class": "u-photo",
- "src": `https://www.gravatar.com/avatar/${comment.iconHash}?s=200`
- });
-
- let authorName = element("a", {
- "class": "p-name u-url",
- "href": comment.authorUrl
- });
- authorName.textContent = comment.authorName;
-
- let authorURI = element("a", {
- "class": "author_url",
- "href": comment.author.url
- });
- authorURI.textContent = strip(comment.author.url);
-
- let hCardContainer = element("div", {
- "class": "p-author h-card author"
- });
- hCardContainer.appendChild(avatar);
- hCardContainer.appendChild(authorName);
- hCardContainer.appendChild(authorURI);
-
- // Create the content section.
- let contentContainer = element("div", {
- "class": "e-content p-name comment-content",
- });
-
- contentContainer.textContent = comment.content;
-
- // Create the metaline section.
- let pubTime = comment.published;
- let time = element("time", {
- "class": "dt-published",
- "datetime": pubTime,
- });
- time.textContent = (new Date(pubTime)).toString();
-
- let linkBack = element("a", {
- "class": "u-url",
- "href": comment.url
- });
- linkBack.appendChild(time);
-
- let metalineContainer = element("div", {
- "class": "metaline"
- });
- metalineContainer.appendChild(linkBack);
-
- // Put it all together.
- let wrapper = element("li", {
- "class": "p-comment h-cite comment",
- });
- wrapper.appendChild(hCardContainer);
- wrapper.appendChild(contentContainer);
- wrapper.appendChild(metalineContainer);
-
- return wrapper;
-}
-
-getData("http://localhost:8081/api/challenge", (b64data) => {
- console.log("Here2");
- document.getElementById("captcha").src = 'data:image/png;base64,' + b64data;
-});
-console.log("Here1");
diff --git a/haunt/static/js/proof-of-work.js b/haunt/static/js/proof-of-work.js
new file mode 100644
index 0000000..b4b5c57
--- /dev/null
+++ b/haunt/static/js/proof-of-work.js
@@ -0,0 +1,109 @@
+/*
+ * proof-of-work.js -- Alternative captcha based on SHA-256 proof-of-work.
+ * 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 makeid(length) {
+ let result = '';
+ let alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
+ for (let i = 0; i < length; i++) {
+ result += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
+ }
+ return result;
+}
+
+async function digestMessage(message) {
+ const msgUtf8 = new TextEncoder().encode(message);
+ const hashBuffer = await crypto.subtle.digest('SHA-256', msgUtf8);
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
+ const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
+ return hashHex;
+}
+
+async function findPrefix(hardness, nonce) {
+ while (true) {
+ let prefix = makeid(32);
+ let digestHex = await digestMessage(prefix + nonce);
+ if (digestHex.startsWith("0".repeat(hardness))) {
+ return prefix;
+ }
+ }
+}
+
+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();
+ });
+}
+
+function raceEndpoint() {
+ return new Promise(function (resolve, reject) {
+ makeRequest("GET", "/api/challenge/proof-of-work")
+ .then(function (data) {
+ let challengeData = JSON.parse(data);
+ findPrefix(challengeData.hardness, challengeData.nonce)
+ .then((prefix) => { resolve([prefix, challengeData["challenge-id"]]) } );
+ })
+ .catch(reject);
+ });
+}
+
+window.addEventListener("load", () => {
+ let trigger = document.getElementById("pow-trigger");
+ trigger.removeAttribute("hidden");
+ trigger.addEventListener("click", (e) => {
+ trigger.innerHTML = "Please wait...";
+ trigger.disabled = true;
+ raceEndpoint()
+ .then((result) => {
+ let resultField = document.getElementById("captcha-alt");
+ let challengeIdField = document.getElementById("captcha-alt-id");
+ resultField.value = result[0];
+ challengeIdField.value = result[1];
+ trigger.innerHTML = "Proof-of-Work completed successfully!";
+ trigger.disabled = true;
+
+ // Hide the primary captcha, too, to make it clear that it isn't necessary to complete.
+ let primaryChallenge = document.getElementById("captcha-challenge-primary");
+ primaryChallenge.hidden = true;
+ })
+ .catch((err) => {
+ console.log(err);
+ trigger.innerHTML = "Proof-of-Work failed!";
+ trigger.disabled = true;
+ });
+ e.preventDefault();
+ })
+});
diff --git a/haunt/static/js/rsvp.js b/haunt/static/js/rsvp.js
index f0a4353..06cdb66 100644
--- a/haunt/static/js/rsvp.js
+++ b/haunt/static/js/rsvp.js
@@ -1,6 +1,6 @@
/*
* rsvp.js -- Simple web-based RSVP tool.
- * Copyright © 2022 Jakob L. Kreuze <zerodaysfordays@sdf.org>
+ * 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
diff --git a/haunt/static/js/section-folds.js b/haunt/static/js/section-folds.js
index 559dd9a..9440264 100644
--- a/haunt/static/js/section-folds.js
+++ b/haunt/static/js/section-folds.js
@@ -1,6 +1,6 @@
/*
* section-folds.js -- Parse out sections to hide behind an "unfold" dialog
- * Copyright © 2022 Jakob L. Kreuze <zerodaysfordays@sdf.org>
+ * 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
diff --git a/haunt/static/js/webmention.js b/haunt/static/js/webmention.js
index c451031..d280d4d 100644
--- a/haunt/static/js/webmention.js
+++ b/haunt/static/js/webmention.js
@@ -1,6 +1,6 @@
/*
* webmention.js -- Fetch and display mentions from webmention.io.
- * Copyright © 2019 - 2020 Jakob L. Kreuze <zerodaysfordays@sdf.org>
+ * Copyright © 2019 - 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

© 2015 - 2026 Jakob L. Kreuze