main.js: fix captcha breaks if loaded before main.js #164 (#165)

Reviewed-on: https://git.leftypol.org/leftypol/leftypol/pulls/165
This commit is contained in:
Zankaria 2024-08-09 22:53:25 +00:00
parent 17f92344e9
commit eb6649b260

View file

@ -243,7 +243,13 @@ function getCookie(cookie_name) {
} }
{% endraw %} {% endraw %}
/* BEGIN CAPTCHA REGION */
{% if config.turnstile %} {% if config.turnstile %}
// Global captcha object. Assigned by `onCaptchaLoad()`.
var captcha_renderer = null;
// Wrapper function to be called from thread.html // Wrapper function to be called from thread.html
window.onCaptchaLoadTurnstile_post_reply = function() { window.onCaptchaLoadTurnstile_post_reply = function() {
onCaptchaLoadTurnstile('post-reply'); onCaptchaLoadTurnstile('post-reply');
@ -277,9 +283,25 @@ function onCaptchaLoadTurnstile(action) {
onCaptchaLoad(renderer); 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 %} {% endif %}
function onCaptchaLoad(renderer) { function onCaptchaLoad(renderer) {
captcha_renderer = renderer;
let widgetId = renderer.renderOn('#captcha-container'); let widgetId = renderer.renderOn('#captcha-container');
if (widgetId === null) { if (widgetId === null) {
console.error('Could not render captcha!'); console.error('Could not render captcha!');
@ -300,11 +322,16 @@ function initDynamicCaptcha() {
if (isDynamicCaptchaEnabled()) { if (isDynamicCaptchaEnabled()) {
let captcha_hook = document.getElementById('captcha'); let captcha_hook = document.getElementById('captcha');
captcha_hook.style = ""; captcha_hook.style = "";
initCaptchaImpl();
} }
} }
{% else %} {% else %}
// No-op for `init()`.
function initDynamicCaptcha() {} function initDynamicCaptcha() {}
{% endif %} {% endif %}
/* END CAPTCHA REGION */
{% raw %} {% raw %}
function highlightReply(id) { function highlightReply(id) {