summaryrefslogtreecommitdiff
path: root/src/main/resources/META-INF
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2022-05-24 22:19:29 +0200
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2022-05-24 22:21:07 +0200
commit14e59440fd5f03d046e4e329eec73560450f1467 (patch)
treed7babd441d7ecb24c5911690cff4fcfb17be75ef /src/main/resources/META-INF
parent34453241b6b7fd5159ef26c8268cbb5cc014e22c (diff)
KB66 Add comment post form.
Change-Id: Iac22d115f0d1c59eb273a26d720c07f6d1b11077
Diffstat (limited to 'src/main/resources/META-INF')
-rw-r--r--src/main/resources/META-INF/resources/lib.js1
-rw-r--r--src/main/resources/META-INF/resources/posts/commentBox.js51
2 files changed, 52 insertions, 0 deletions
diff --git a/src/main/resources/META-INF/resources/lib.js b/src/main/resources/META-INF/resources/lib.js
index 9f54f5f..7a59751 100644
--- a/src/main/resources/META-INF/resources/lib.js
+++ b/src/main/resources/META-INF/resources/lib.js
@@ -2,6 +2,7 @@ import './bookmarks/MlkBookmarkSubmissionForm.js';
import './bookmarks/newBookmark.js';
import './lazychat/MlkLazychatSubmissionForm.js';
import './lazychat/newLazychatMessage.js';
+import './posts/commentBox.js';
import './posts/postList.js';
import './web_modules/elix/define/Button.js';
diff --git a/src/main/resources/META-INF/resources/posts/commentBox.js b/src/main/resources/META-INF/resources/posts/commentBox.js
new file mode 100644
index 0000000..d802a63
--- /dev/null
+++ b/src/main/resources/META-INF/resources/posts/commentBox.js
@@ -0,0 +1,51 @@
+document.addEventListener('DOMContentLoaded', () => {
+ const messageEncoder = new TextEncoder();
+
+ const acceptableHash = (hash) =>
+ hash[0] === 0 &&
+ hash[1] === 0;
+
+ const checkSalt = (message, salt, thenC, elseC) => {
+ const saltedMessage = `Hashcash-Salt: ${salt}\n\n${message}`;
+ const encodedSaltedMessage = messageEncoder.encode(saltedMessage);
+
+ crypto.subtle.digest('SHA-256', encodedSaltedMessage).then((hashBuf) => {
+ if (acceptableHash(new Uint8Array(hashBuf))) {
+ thenC();
+ } else {
+ elseC();
+ }
+ });
+ };
+
+ const commentForms = document.getElementsByClassName('comment-form');
+ for (const commentForm of commentForms) {
+ const hashcashSaltInput = commentForm.querySelector('input[name=hashcash-salt]');
+ const messageTextarea = commentForm.querySelector('textarea[name=message]');
+ const submitButton = commentForm.querySelector('input[type=submit]');
+
+ let salt = 0;
+ const tryHashcash = () => {
+ checkSalt(messageTextarea.value, salt, () => {
+ hashcashSaltInput.value = salt;
+ console.log(`hashcash ok ${salt}`);
+ submitButton.toggleAttribute('disabled');
+ commentForm.submit();
+ }, () => {
+ ++salt;
+ if (salt % 1000 === 0) {
+ console.log(`hashcash fail ${salt}, retrying`)
+ }
+ setTimeout(tryHashcash, 0);
+ });
+ };
+
+ commentForm.addEventListener(
+ 'submit',
+ (event) => {
+ event.preventDefault();
+ submitButton.setAttribute('disabled', 'disabled');
+ setTimeout(tryHashcash, 0);
+ });
+ }
+});