Add hcaptcha support (#166)

Co-authored-by: RealAngeleno <angeleno@screamer.wiki>
Reviewed-on: https://git.leftypol.org/leftypol/leftypol/pulls/166
Co-authored-by: Zankaria <zankaria.auxa@skiff.com>
Co-committed-by: Zankaria <zankaria.auxa@skiff.com>
This commit is contained in:
Zankaria 2024-08-10 19:58:20 +00:00 committed by Zankaria
parent 25089f5cbb
commit d8c5c600a8
5 changed files with 107 additions and 19 deletions

View file

@ -245,11 +245,39 @@ function getCookie(cookie_name) {
{% endraw %}
/* BEGIN CAPTCHA REGION */
{% if config.turnstile %}
{% if config.hcaptcha or config.turnstile %} // If any captcha
// Global captcha object. Assigned by `onCaptchaLoad()`.
var captcha_renderer = null;
{% if config.hcaptcha %} // If hcaptcha
function onCaptchaLoadHcaptcha() {
if (captcha_renderer === null) {
let renderer = {
renderOn: (container) => hcaptcha.render(container, {
sitekey: "{{ config.hcaptcha_public }}",
}),
remove: (widgetId) => { /* Not supported */ },
reset: (widgetId) => hcaptcha.reset(widgetId)
};
onCaptchaLoad(renderer);
}
}
function initCaptchaImpl() {
/*
* hcaptcha is set but the captcha_renderer is null? hcaptcha loaded before main.js could set up the callbacks.
* Init now.
*/
if (hcaptcha && captcha_renderer === null) {
if (active_page === 'index' || active_page === 'catalog' || active_page === 'thread') {
onCaptchaLoadHcaptcha();
}
}
}
{% endif %} // End if hcaptcha
{% if config.turnstile %} // If turnstile
// Wrapper function to be called from thread.html
window.onCaptchaLoadTurnstile_post_reply = function() {
onCaptchaLoadTurnstile('post-reply');
@ -265,7 +293,7 @@ function onCaptchaLoadTurnstile(action) {
if (captcha_renderer === null) {
let renderer = {
renderOn: function(container) {
let widgetId = turnstile.render(container, {
let widgetId = turnstile.render('#' + container, {
sitekey: "{{ config.turnstile_public }}",
action: action,
});
@ -274,12 +302,8 @@ function onCaptchaLoadTurnstile(action) {
}
return widgetId;
},
remove: function(widgetId) {
turnstile.remove(widgetId);
},
reset: function(widgetId) {
turnstile.reset(widgetId);
}
remove: (widgetId) => turnstile.remove(widgetId),
reset: (widgetId) => turnstile.reset(widgetId)
};
onCaptchaLoad(renderer);
@ -299,12 +323,12 @@ function initCaptchaImpl() {
}
}
}
{% endif %}
{% endif %} // End if turnstile
function onCaptchaLoad(renderer) {
captcha_renderer = renderer;
let widgetId = renderer.renderOn('#captcha-container');
let widgetId = renderer.renderOn('captcha-container');
if (widgetId === null) {
console.error('Could not render captcha!');
}
@ -314,23 +338,24 @@ function onCaptchaLoad(renderer) {
});
}
{% if config.dynamic_captcha %}
{% if config.dynamic_captcha %} // If dynamic captcha
function isDynamicCaptchaEnabled() {
let cookie = getCookie('captcha-required');
return cookie === '1';
}
function initDynamicCaptcha() {
function initCaptcha() {
if (isDynamicCaptchaEnabled()) {
let captcha_hook = document.getElementById('captcha');
captcha_hook.style = "";
initCaptchaImpl();
}
}
{% else %}
{% endif %} // End if dynamic captcha
{% else %} // Else if any captcha
// No-op for `init()`.
function initDynamicCaptcha() {}
{% endif %}
function initCaptcha() {}
{% endif %} // End if any captcha
/* END CAPTCHA REGION */
@ -500,7 +525,7 @@ var script_settings = function(script_name) {
function init() {
initStyleChooser();
initDynamicCaptcha();
initCaptcha();
{% endraw %}
{% if config.allow_delete %}