main.js: rework dynamic captcha rendering to correctly reset when a user posts

This commit is contained in:
Zankaria 2024-08-04 00:48:12 +02:00
parent 95d5ec6cc6
commit 584fac7caa
2 changed files with 16 additions and 1 deletions

View file

@ -255,6 +255,20 @@ function getCaptchaPubKey() {
{% endif %} {% 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() { function initDynamicCaptcha() {
if (isDynamicCaptchaEnabled()) { if (isDynamicCaptchaEnabled()) {
let pub_key = getCaptchaPubKey(); let pub_key = getCaptchaPubKey();
@ -317,6 +331,7 @@ function dopost(form) {
saved[document.location] = form.elements['body'].value; saved[document.location] = form.elements['body'].value;
sessionStorage.body = JSON.stringify(saved); 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 != ""); return form.elements['body'].value != "" || (form.elements['file'] && form.elements['file'].value != "") || (form.elements.file_url && form.elements['file_url'].value != "");
} }

View file

@ -117,7 +117,7 @@
{{ antibot.html() }} {{ antibot.html() }}
</th> </th>
<td> <td>
<div class="cf-turnstile" data-sitekey="{{ config.turnstile_public }}" data-action="{{ form_action_type }}"></div> <div class="captcha-container" data-sitekey="{{ config.turnstile_public }}" data-action="{{ form_action_type }}"></div>
{{ antibot.html() }} {{ antibot.html() }}
</td> </td>
</tr> </tr>