From 584fac7caab440ea9cdab1fde3e89f47d0f2646c Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 00:48:12 +0200 Subject: [PATCH] main.js: rework dynamic captcha rendering to correctly reset when a user posts --- templates/main.js | 15 +++++++++++++++ templates/post_form.html | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/templates/main.js b/templates/main.js index 072bb6af..9369353c 100755 --- a/templates/main.js +++ b/templates/main.js @@ -255,6 +255,20 @@ function getCaptchaPubKey() { {% endif %} } +// Should be called by the captcha API when it's ready. Ugly I know... D: +window.onCaptchaLoadTurnstile = function() { + // Do not actually pass any parameters, as those should already be included in the HTML node. + let widgetId = turnstile.render('captcha-container', {}); + if (widgetId === undefined) { + console.error('Could not render Turnstile captcha!'); + return; + } + document.addEventListener('post', function(e) { + // User posted! Reset the captcha. + turnstile.reset(widgetId); + }); +} + function initDynamicCaptcha() { if (isDynamicCaptchaEnabled()) { let pub_key = getCaptchaPubKey(); @@ -317,6 +331,7 @@ function dopost(form) { saved[document.location] = form.elements['body'].value; sessionStorage.body = JSON.stringify(saved); + document.dispatchEvent(new Event('post')); return form.elements['body'].value != "" || (form.elements['file'] && form.elements['file'].value != "") || (form.elements.file_url && form.elements['file_url'].value != ""); } diff --git a/templates/post_form.html b/templates/post_form.html index b3f1d8ce..6c5a6205 100644 --- a/templates/post_form.html +++ b/templates/post_form.html @@ -117,7 +117,7 @@ {{ antibot.html() }} -
+
{{ antibot.html() }}