main.js: abstract captcha renderer

This commit is contained in:
Zankaria 2024-08-04 01:29:34 +02:00
parent 1b3b8cb9b6
commit 1eaa0f7631

View file

@ -239,22 +239,7 @@ function getCookie(cookie_name) {
} }
{% endraw %} {% endraw %}
{% if config.dynamic_captcha %} {% if config.turnstile %}
function isDynamicCaptchaEnabled() {
let cookie = getCookie('captcha-required');
return cookie === '1';
}
function getCaptchaPubKey() {
{% if config.recaptcha %}
return "{{ config.recaptcha_public }}";
{% elseif config.turnstile %}
return "{{ config.turnstile_public }}";
{% else %}
return null;
{% endif %}
}
// 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');
@ -267,24 +252,46 @@ window.onCaptchaLoadTurnstile_post_thread = function() {
// Should be called by the captcha API when it's ready. Ugly I know... D: // Should be called by the captcha API when it's ready. Ugly I know... D:
function onCaptchaLoadTurnstile(action) { function onCaptchaLoadTurnstile(action) {
let pub_key = getCaptchaPubKey(); let renderer = {
if (!pub_key) { renderOn: function(container) {
console.error("Missing public captcha key!"); let widgetId = turnstile.render(container, {
return; sitekey: "{{ config.turnstile_public }}",
}
let widgetId = turnstile.render('captcha-container', {
sitekey: pub_key,
action: action, action: action,
}); });
if (widgetId === undefined) { if (widgetId === undefined) {
console.error('Could not render Turnstile captcha!'); console.error('Could not render Turnstile captcha!');
return; return undefined;
} }
document.addEventListener('post', function(e) { document.addEventListener('post', function(e) {
// User posted! Reset the captcha. // User posted! Reset the captcha.
turnstile.reset(widgetId); turnstile.reset(widgetId);
}); });
return widgetId;
},
remove: function(widgetId) {
turnstile.remove(widgetId);
}
};
onCaptchaLoad(renderer);
}
{% endif %}
function onCaptchaLoad(renderer) {
let widgetId = renderer.renderOn('#captcha-container');
if (widgetId === null) {
console.error('Could not render captcha!');
}
document.addEventListener('post', function(e) {
// User posted! Reset the captcha.
renderer.reset(widgetId);
});
}
{% if config.dynamic_captcha %}
function isDynamicCaptchaEnabled() {
let cookie = getCookie('captcha-required');
return cookie === '1';
} }
function initDynamicCaptcha() { function initDynamicCaptcha() {