From eb6649b260269d54ec82407fa9dae084f2e1bd46 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Fri, 9 Aug 2024 22:53:25 +0000 Subject: [PATCH] main.js: fix captcha breaks if loaded before main.js #164 (#165) Reviewed-on: https://git.leftypol.org/leftypol/leftypol/pulls/165 --- templates/main.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/templates/main.js b/templates/main.js index b73963ec..422759b4 100755 --- a/templates/main.js +++ b/templates/main.js @@ -243,7 +243,13 @@ function getCookie(cookie_name) { } {% endraw %} + +/* BEGIN CAPTCHA REGION */ + {% if config.turnstile %} +// Global captcha object. Assigned by `onCaptchaLoad()`. +var captcha_renderer = null; + // Wrapper function to be called from thread.html window.onCaptchaLoadTurnstile_post_reply = function() { onCaptchaLoadTurnstile('post-reply'); @@ -277,9 +283,25 @@ function onCaptchaLoadTurnstile(action) { onCaptchaLoad(renderer); } + +function initCaptchaImpl() { + /* + * Turnstile is set but the captcha_renderer is null? Turnstile loaded before main.js could set up the callbacks. + * Init now. + */ + if (turnstile && captcha_renderer === null) { + if (active_page === 'index' || active_page === 'catalog') { + onCaptchaLoadTurnstile('post-thread'); + } else if (active_page === 'thread') { + onCaptchaLoadTurnstile('post-reply'); + } + } +} {% endif %} function onCaptchaLoad(renderer) { + captcha_renderer = renderer; + let widgetId = renderer.renderOn('#captcha-container'); if (widgetId === null) { console.error('Could not render captcha!'); @@ -300,11 +322,16 @@ function initDynamicCaptcha() { if (isDynamicCaptchaEnabled()) { let captcha_hook = document.getElementById('captcha'); captcha_hook.style = ""; + initCaptchaImpl(); } } {% else %} +// No-op for `init()`. function initDynamicCaptcha() {} {% endif %} + +/* END CAPTCHA REGION */ + {% raw %} function highlightReply(id) {