main.js: refactor captcha parameters to be managed in JS

This commit is contained in:
Zankaria 2024-08-04 01:16:51 +02:00
parent 584fac7caa
commit 1b3b8cb9b6
6 changed files with 29 additions and 17 deletions

View file

@ -1,3 +1,3 @@
{% if config.turnstile %}
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=onCaptchaLoadTurnstile" async defer></script>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=onCaptchaLoadTurnstile_{{ form_action_type }}" async defer></script>
{% endif %}

View file

@ -13,7 +13,7 @@
active_page = "ukko";
{% endif %}
</script>
{% include 'captcha_script.html' %}
{{ include('captcha_script.html', { form_action_type: 'post_thread' }) }}
{% include 'header.html' %}
{% set page_num %}{% for page in pages %}{% if page.selected %}{% if page.num != 1 %}{{ page.num }}{% endif %}{% endif %}{% endfor %}{% endset %}
@ -59,7 +59,7 @@
{{ config.ad.top }}
{% if not no_post_form %}
{{ include('post_form.html', {form_action_type: 'post-thread'}) }}
{% include 'post_form.html' %}
{% else %}
{% include 'boardlist.html' %}
{% endif %}

View file

@ -255,10 +255,28 @@ function getCaptchaPubKey() {
{% endif %}
}
// Wrapper function to be called from thread.html
window.onCaptchaLoadTurnstile_post_reply = function() {
onCaptchaLoadTurnstile('post-reply');
}
// Wrapper function to be called from index.html and catalog.html
window.onCaptchaLoadTurnstile_post_thread = function() {
onCaptchaLoadTurnstile('post-thread');
}
// Should be called by the captcha API when it's ready. Ugly I know... D:
window.onCaptchaLoadTurnstile = function() {
// Do not actually pass any parameters, as those should already be included in the HTML node.
let widgetId = turnstile.render('captcha-container', {});
function onCaptchaLoadTurnstile(action) {
let pub_key = getCaptchaPubKey();
if (!pub_key) {
console.error("Missing public captcha key!");
return;
}
let widgetId = turnstile.render('captcha-container', {
sitekey: pub_key,
action: action,
});
if (widgetId === undefined) {
console.error('Could not render Turnstile captcha!');
return;
@ -271,12 +289,6 @@ window.onCaptchaLoadTurnstile = function() {
function initDynamicCaptcha() {
if (isDynamicCaptchaEnabled()) {
let pub_key = getCaptchaPubKey();
if (!pub_key) {
console.error("Missing public captcha key!");
return;
}
let captcha_hook = document.getElementById('captcha');
captcha_hook.style = "";
}

View file

@ -117,7 +117,7 @@
{{ antibot.html() }}
</th>
<td>
<div class="captcha-container" data-sitekey="{{ config.turnstile_public }}" data-action="{{ form_action_type }}"></div>
<div id="captcha-container"></div>
{{ antibot.html() }}
</td>
</tr>

View file

@ -9,7 +9,7 @@
, board_name = "{{ board.uri }}"
, is_overboard = "{{ is_overboard }}";
</script>
{% include 'captcha_script.html' %}
{{ include('captcha_script.html', { form_action_type: 'post_thread' }) }}
<title>{{ settings.title }} ( /{{ board.title|e }}/ )</title>
{% include 'header.html' %}
</head>
@ -29,7 +29,7 @@
<center>[ {% trans 'Create new thread' %} ]</center>
</summary>
<div style="margin: 1em 0;">
{{ include('post_form.html', {form_action_type: 'post-thread'}) }}
{% include 'post_form.html' %}
</div>
</details>
{% endif %}

View file

@ -8,7 +8,7 @@
, board_name = "{{ board.uri }}"
, thread_id = "{{ thread.id }}";
</script>
{% include 'captcha_script.html' %}
{{ include('captcha_script.html', { form_action_type: 'post_reply' }) }}
{% include 'header.html' %}
@ -54,7 +54,7 @@
{{ config.ad.top }}
{{ include('post_form.html', {form_action_type: 'post-reply'}) }}
{% include 'post_form.html' %}
{% if config.global_message %}<hr /><div class="blotter">{{ config.global_message }}</div>{% endif %}
<hr />