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 %} {% 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 %} {% endif %}

View file

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

View file

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

View file

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

View file

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

View file

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