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 %}
/* 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) {