From 7b98295dcf1d35bb9a845e248368af194e691c66 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sat, 3 Aug 2024 23:45:53 +0200 Subject: [PATCH 01/14] main.js: fix captcha selection check --- templates/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/main.js b/templates/main.js index 1639175d..5449f129 100755 --- a/templates/main.js +++ b/templates/main.js @@ -248,7 +248,7 @@ function is_dynamic_captcha_enabled() { function get_captcha_pub_key() { {% if config.recaptcha %} return "{{ config.recaptcha_public }}"; -{% elseif config.turnstile_public %} +{% elseif config.turnstile %} return "{{ config.turnstile_public }}"; {% else %} return null; From be69385fef373a91c3a12825deecbededd94f666 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sat, 3 Aug 2024 23:56:25 +0200 Subject: [PATCH 02/14] main.js: use camelCase for captcha javascript --- templates/main.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/templates/main.js b/templates/main.js index 5449f129..072bb6af 100755 --- a/templates/main.js +++ b/templates/main.js @@ -230,7 +230,7 @@ function init_stylechooser() { document.getElementById('bottom-hud').before(newElement); } -function get_cookie(cookie_name) { +function getCookie(cookie_name) { var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)'); if (results) return (unescape(results[2])); @@ -240,12 +240,12 @@ function get_cookie(cookie_name) { {% endraw %} {% if config.dynamic_captcha %} -function is_dynamic_captcha_enabled() { - let cookie = get_cookie('captcha-required'); +function isDynamicCaptchaEnabled() { + let cookie = getCookie('captcha-required'); return cookie === '1'; } -function get_captcha_pub_key() { +function getCaptchaPubKey() { {% if config.recaptcha %} return "{{ config.recaptcha_public }}"; {% elseif config.turnstile %} @@ -255,9 +255,9 @@ function get_captcha_pub_key() { {% endif %} } -function init_dynamic_captcha() { - if (is_dynamic_captcha_enabled()) { - let pub_key = get_captcha_pub_key(); +function initDynamicCaptcha() { + if (isDynamicCaptchaEnabled()) { + let pub_key = getCaptchaPubKey(); if (!pub_key) { console.error("Missing public captcha key!"); return; @@ -268,7 +268,7 @@ function init_dynamic_captcha() { } } {% else %} -function init_dynamic_captcha() {} +function initDynamicCaptcha() {} {% endif %} {% raw %} @@ -389,9 +389,9 @@ function rememberStuff() { if (sessionStorage.body) { var saved = JSON.parse(sessionStorage.body); - if (get_cookie('{% endraw %}{{ config.cookies.js }}{% raw %}')) { + if (getCookie('{% endraw %}{{ config.cookies.js }}{% raw %}')) { // Remove successful posts - var successful = JSON.parse(get_cookie('{% endraw %}{{ config.cookies.js }}{% raw %}')); + var successful = JSON.parse(getCookie('{% endraw %}{{ config.cookies.js }}{% raw %}')); for (var url in successful) { saved[url] = null; } @@ -424,7 +424,7 @@ var script_settings = function(script_name) { function init() { init_stylechooser(); - init_dynamic_captcha(); + initDynamicCaptcha(); {% endraw %} {% if config.allow_delete %} From 95d5ec6cc6e5c15f7aa8fc4454c70ad5884088b0 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 00:47:12 +0200 Subject: [PATCH 03/14] captcha_script.html: refactor captcha script inclusion in a separate template --- templates/captcha_script.html | 3 +++ templates/index.html | 4 +--- templates/themes/catalog/catalog.html | 4 +--- templates/thread.html | 4 +--- 4 files changed, 6 insertions(+), 9 deletions(-) create mode 100644 templates/captcha_script.html diff --git a/templates/captcha_script.html b/templates/captcha_script.html new file mode 100644 index 00000000..2573682b --- /dev/null +++ b/templates/captcha_script.html @@ -0,0 +1,3 @@ +{% if config.turnstile %} + +{% endif %} diff --git a/templates/index.html b/templates/index.html index f36ff88b..2fda9773 100644 --- a/templates/index.html +++ b/templates/index.html @@ -13,9 +13,7 @@ active_page = "ukko"; {% endif %} - {% if config.turnstile %} - - {% endif %} + {% include 'captcha_script.html' %} {% include 'header.html' %} {% set page_num %}{% for page in pages %}{% if page.selected %}{% if page.num != 1 %}{{ page.num }}{% endif %}{% endif %}{% endfor %}{% endset %} diff --git a/templates/themes/catalog/catalog.html b/templates/themes/catalog/catalog.html index 78015377..44204e48 100644 --- a/templates/themes/catalog/catalog.html +++ b/templates/themes/catalog/catalog.html @@ -9,9 +9,7 @@ , board_name = "{{ board.uri }}" , is_overboard = "{{ is_overboard }}"; - {% if config.turnstile %} - - {% endif %} + {% include 'captcha_script.html' %} {{ settings.title }} ( /{{ board.title|e }}/ ) {% include 'header.html' %} diff --git a/templates/thread.html b/templates/thread.html index 5c015ddd..6888f56f 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -8,9 +8,7 @@ , board_name = "{{ board.uri }}" , thread_id = "{{ thread.id }}"; - {% if config.turnstile %} - - {% endif %} + {% include 'captcha_script.html' %} {% include 'header.html' %} From 584fac7caab440ea9cdab1fde3e89f47d0f2646c Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 00:48:12 +0200 Subject: [PATCH 04/14] main.js: rework dynamic captcha rendering to correctly reset when a user posts --- templates/main.js | 15 +++++++++++++++ templates/post_form.html | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/templates/main.js b/templates/main.js index 072bb6af..9369353c 100755 --- a/templates/main.js +++ b/templates/main.js @@ -255,6 +255,20 @@ function getCaptchaPubKey() { {% endif %} } +// 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', {}); + if (widgetId === undefined) { + console.error('Could not render Turnstile captcha!'); + return; + } + document.addEventListener('post', function(e) { + // User posted! Reset the captcha. + turnstile.reset(widgetId); + }); +} + function initDynamicCaptcha() { if (isDynamicCaptchaEnabled()) { let pub_key = getCaptchaPubKey(); @@ -317,6 +331,7 @@ function dopost(form) { saved[document.location] = form.elements['body'].value; sessionStorage.body = JSON.stringify(saved); + document.dispatchEvent(new Event('post')); return form.elements['body'].value != "" || (form.elements['file'] && form.elements['file'].value != "") || (form.elements.file_url && form.elements['file_url'].value != ""); } diff --git a/templates/post_form.html b/templates/post_form.html index b3f1d8ce..6c5a6205 100644 --- a/templates/post_form.html +++ b/templates/post_form.html @@ -117,7 +117,7 @@ {{ antibot.html() }} -
+
{{ antibot.html() }} From 1b3b8cb9b698c78a61cd36dc95221c9911cdaabb Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 01:16:51 +0200 Subject: [PATCH 05/14] main.js: refactor captcha parameters to be managed in JS --- templates/captcha_script.html | 2 +- templates/index.html | 4 ++-- templates/main.js | 30 +++++++++++++++++++-------- templates/post_form.html | 2 +- templates/themes/catalog/catalog.html | 4 ++-- templates/thread.html | 4 ++-- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/templates/captcha_script.html b/templates/captcha_script.html index 2573682b..a511d51e 100644 --- a/templates/captcha_script.html +++ b/templates/captcha_script.html @@ -1,3 +1,3 @@ {% if config.turnstile %} - + {% endif %} diff --git a/templates/index.html b/templates/index.html index 2fda9773..f5b64c96 100644 --- a/templates/index.html +++ b/templates/index.html @@ -13,7 +13,7 @@ active_page = "ukko"; {% endif %} - {% 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 %} diff --git a/templates/main.js b/templates/main.js index 9369353c..938320dd 100755 --- a/templates/main.js +++ b/templates/main.js @@ -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 = ""; } diff --git a/templates/post_form.html b/templates/post_form.html index 6c5a6205..220902e6 100644 --- a/templates/post_form.html +++ b/templates/post_form.html @@ -117,7 +117,7 @@ {{ antibot.html() }} -
+
{{ antibot.html() }} diff --git a/templates/themes/catalog/catalog.html b/templates/themes/catalog/catalog.html index 44204e48..a9bee317 100644 --- a/templates/themes/catalog/catalog.html +++ b/templates/themes/catalog/catalog.html @@ -9,7 +9,7 @@ , board_name = "{{ board.uri }}" , is_overboard = "{{ is_overboard }}"; - {% include 'captcha_script.html' %} + {{ include('captcha_script.html', { form_action_type: 'post_thread' }) }} {{ settings.title }} ( /{{ board.title|e }}/ ) {% include 'header.html' %} @@ -29,7 +29,7 @@
[ {% trans 'Create new thread' %} ]
- {{ include('post_form.html', {form_action_type: 'post-thread'}) }} + {% include 'post_form.html' %}
{% endif %} diff --git a/templates/thread.html b/templates/thread.html index 6888f56f..9d034a2b 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -8,7 +8,7 @@ , board_name = "{{ board.uri }}" , thread_id = "{{ thread.id }}"; - {% 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 %}
{{ config.global_message }}
{% endif %}
From 1eaa0f7631e29534d73b3ac1a07b6c530b4ad8d8 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 01:29:34 +0200 Subject: [PATCH 06/14] main.js: abstract captcha renderer --- templates/main.js | 65 ++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/templates/main.js b/templates/main.js index 938320dd..9bebf755 100755 --- a/templates/main.js +++ b/templates/main.js @@ -239,22 +239,7 @@ function getCookie(cookie_name) { } {% endraw %} -{% if config.dynamic_captcha %} -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 %} -} - +{% if config.turnstile %} // Wrapper function to be called from thread.html window.onCaptchaLoadTurnstile_post_reply = function() { onCaptchaLoadTurnstile('post-reply'); @@ -267,26 +252,48 @@ window.onCaptchaLoadTurnstile_post_thread = function() { // Should be called by the captcha API when it's ready. Ugly I know... D: function onCaptchaLoadTurnstile(action) { - let pub_key = getCaptchaPubKey(); - if (!pub_key) { - console.error("Missing public captcha key!"); - return; - } + let renderer = { + renderOn: function(container) { + let widgetId = turnstile.render(container, { + sitekey: "{{ config.turnstile_public }}", + action: action, + }); + if (widgetId === undefined) { + console.error('Could not render Turnstile captcha!'); + return undefined; + } + document.addEventListener('post', function(e) { + // User posted! Reset the captcha. + turnstile.reset(widgetId); + }); + return widgetId; + }, + remove: function(widgetId) { + turnstile.remove(widgetId); + } + }; - let widgetId = turnstile.render('captcha-container', { - sitekey: pub_key, - action: action, - }); - if (widgetId === undefined) { - console.error('Could not render Turnstile captcha!'); - return; + 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. - turnstile.reset(widgetId); + renderer.reset(widgetId); }); } +{% if config.dynamic_captcha %} +function isDynamicCaptchaEnabled() { + let cookie = getCookie('captcha-required'); + return cookie === '1'; +} + function initDynamicCaptcha() { if (isDynamicCaptchaEnabled()) { let captcha_hook = document.getElementById('captcha'); From 30d0b3bfe4d5f8abc15fbe54106e66a18ecdc3ae Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 15:07:03 +0200 Subject: [PATCH 07/14] main.js: extract captcha reset --- templates/main.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/templates/main.js b/templates/main.js index 9bebf755..72ca66c8 100755 --- a/templates/main.js +++ b/templates/main.js @@ -259,17 +259,15 @@ function onCaptchaLoadTurnstile(action) { action: action, }); if (widgetId === undefined) { - console.error('Could not render Turnstile captcha!'); - return undefined; + return null; } - document.addEventListener('post', function(e) { - // User posted! Reset the captcha. - turnstile.reset(widgetId); - }); return widgetId; }, remove: function(widgetId) { turnstile.remove(widgetId); + }, + reset: function(widgetId) { + turnstile.reset(widgetId); } }; @@ -288,7 +286,6 @@ function onCaptchaLoad(renderer) { }); } -{% if config.dynamic_captcha %} function isDynamicCaptchaEnabled() { let cookie = getCookie('captcha-required'); return cookie === '1'; From b77685f8f419a65aac99d554455c91aaf3e03a3b Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 15:07:56 +0200 Subject: [PATCH 08/14] main.js: format getCookie --- templates/main.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/templates/main.js b/templates/main.js index 72ca66c8..6e6f6b1a 100755 --- a/templates/main.js +++ b/templates/main.js @@ -231,11 +231,12 @@ function init_stylechooser() { } function getCookie(cookie_name) { - var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)'); - if (results) - return (unescape(results[2])); - else + let results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)'); + if (results) { + return unescape(results[2]); + } else { return null; + } } {% endraw %} From d8ea4c49adce475afb908b988a65e958c1da6d6b Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 15:13:05 +0200 Subject: [PATCH 09/14] main.js: rename dopost to camelCase --- templates/main.js | 2 +- templates/post_form.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/main.js b/templates/main.js index 6e6f6b1a..88765739 100755 --- a/templates/main.js +++ b/templates/main.js @@ -334,7 +334,7 @@ function generatePassword() { return pass; } -function dopost(form) { +function doPost(form) { if (form.elements['name']) { localStorage.name = form.elements['name'].value.replace(/( |^)## .+$/, ''); } diff --git a/templates/post_form.html b/templates/post_form.html index 220902e6..6434b7e8 100644 --- a/templates/post_form.html +++ b/templates/post_form.html @@ -1,5 +1,5 @@
Date: Sun, 4 Aug 2024 15:32:12 +0200 Subject: [PATCH 10/14] main.js: format --- templates/main.js | 207 ++++++++++++++++++++++++---------------------- 1 file changed, 109 insertions(+), 98 deletions(-) diff --git a/templates/main.js b/templates/main.js index 88765739..bfe89bc4 100755 --- a/templates/main.js +++ b/templates/main.js @@ -18,92 +18,92 @@ function _(s) { * > alert(fmt(_("{0} users"), [3])); * 3 uzytkownikow */ -function fmt(s,a) { +function fmt(s, a) { return s.replace(/\{([0-9]+)\}/g, function(x) { return a[x[1]]; }); } -function until($timestamp) { - var $difference = $timestamp - Date.now()/1000|0, $num; - switch(true){ - case ($difference < 60): - return "" + $difference + ' ' + _('second(s)'); - case ($difference < 3600): //60*60 = 3600 - return "" + ($num = Math.round($difference/(60))) + ' ' + _('minute(s)'); - case ($difference < 86400): //60*60*24 = 86400 - return "" + ($num = Math.round($difference/(3600))) + ' ' + _('hour(s)'); - case ($difference < 604800): //60*60*24*7 = 604800 - return "" + ($num = Math.round($difference/(86400))) + ' ' + _('day(s)'); - case ($difference < 31536000): //60*60*24*365 = 31536000 - return "" + ($num = Math.round($difference/(604800))) + ' ' + _('week(s)'); - default: - return "" + ($num = Math.round($difference/(31536000))) + ' ' + _('year(s)'); - } +function until(timestamp) { + let difference = timestamp - Date.now() / 1000 | 0; + switch (true) { + case (difference < 60): + return "" + difference + ' ' + _('second(s)'); + case (difference < 3600): // 60 * 60 = 3600 + return "" + Math.round(difference / 60) + ' ' + _('minute(s)'); + case (difference < 86400): // 60 * 60 * 24 = 86400 + return "" + Math.round(difference / 3600) + ' ' + _('hour(s)'); + case (difference < 604800): // 60 * 60 * 24 * 7 = 604800 + return "" + Math.round(difference / 86400) + ' ' + _('day(s)'); + case (difference < 31536000): // 60 * 60 * 24 * 365 = 31536000 + return "" + Math.round(difference / 604800) + ' ' + _('week(s)'); + default: + return "" + Math.round(difference / 31536000) + ' ' + _('year(s)'); + } } -function ago($timestamp) { - var $difference = (Date.now()/1000|0) - $timestamp, $num; - switch(true){ - case ($difference < 60) : - return "" + $difference + ' ' + _('second(s)'); - case ($difference < 3600): //60*60 = 3600 - return "" + ($num = Math.round($difference/(60))) + ' ' + _('minute(s)'); - case ($difference < 86400): //60*60*24 = 86400 - return "" + ($num = Math.round($difference/(3600))) + ' ' + _('hour(s)'); - case ($difference < 604800): //60*60*24*7 = 604800 - return "" + ($num = Math.round($difference/(86400))) + ' ' + _('day(s)'); - case ($difference < 31536000): //60*60*24*365 = 31536000 - return "" + ($num = Math.round($difference/(604800))) + ' ' + _('week(s)'); - default: - return "" + ($num = Math.round($difference/(31536000))) + ' ' + _('year(s)'); - } +function ago(timestamp) { + let difference = (Date.now() / 1000 | 0) - timestamp; + switch (true) { + case (difference < 60): + return "" + difference + ' ' + _('second(s)'); + case (difference < 3600): /// 60 * 60 = 3600 + return "" + Math.round(difference/(60)) + ' ' + _('minute(s)'); + case (difference < 86400): // 60 * 60 * 24 = 86400 + return "" + Math.round(difference/(3600)) + ' ' + _('hour(s)'); + case (difference < 604800): // 60 * 60 * 24 * 7 = 604800 + return "" + Math.round(difference/(86400)) + ' ' + _('day(s)'); + case (difference < 31536000): // 60 * 60 * 24 * 365 = 31536000 + return "" + Math.round(difference/(604800)) + ' ' + _('week(s)'); + default: + return "" + Math.round(difference/(31536000)) + ' ' + _('year(s)'); + } } var datelocale = - { days: [_('Sunday'), _('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday')] - , shortDays: [_("Sun"), _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat")] - , months: [_('January'), _('February'), _('March'), _('April'), _('May'), _('June'), _('July'), _('August'), _('September'), _('October'), _('November'), _('December')] - , shortMonths: [_('Jan'), _('Feb'), _('Mar'), _('Apr'), _('May'), _('Jun'), _('Jul'), _('Aug'), _('Sep'), _('Oct'), _('Nov'), _('Dec')] - , AM: _('AM') - , PM: _('PM') - , am: _('am') - , pm: _('pm') - }; + { days: [_('Sunday'), _('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday')] + , shortDays: [_("Sun"), _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat")] + , months: [_('January'), _('February'), _('March'), _('April'), _('May'), _('June'), _('July'), _('August'), _('September'), _('October'), _('November'), _('December')] + , shortMonths: [_('Jan'), _('Feb'), _('Mar'), _('Apr'), _('May'), _('Jun'), _('Jul'), _('Aug'), _('Sep'), _('Oct'), _('Nov'), _('Dec')] + , AM: _('AM') + , PM: _('PM') + , am: _('am') + , pm: _('pm') + }; function alert(a, do_confirm, confirm_ok_action, confirm_cancel_action) { - var handler, div, bg, closebtn, okbtn; - var close = function() { - handler.fadeOut(400, function() { handler.remove(); }); - return false; - }; + var handler, div, bg, closebtn, okbtn; + let close = function() { + handler.fadeOut(400, function() { handler.remove(); }); + return false; + }; - handler = $("
").hide().appendTo('body'); + handler = $("
").hide().appendTo('body'); - bg = $("
").appendTo(handler); + bg = $("
").appendTo(handler); - div = $("
").appendTo(handler); - closebtn = $("") - .appendTo(div); + div = $("
").appendTo(handler); + closebtn = $("
") + .appendTo(div); - $("
").html(a).appendTo(div); + $("
").html(a).appendTo(div); - okbtn = $("").appendTo(div); + okbtn = $("").appendTo(div); - if (do_confirm) { - confirm_ok_action = (typeof confirm_ok_action !== "function") ? function(){} : confirm_ok_action; - confirm_cancel_action = (typeof confirm_cancel_action !== "function") ? function(){} : confirm_cancel_action; - okbtn.click(confirm_ok_action); - $("").click(confirm_cancel_action).click(close).appendTo(div); - bg.click(confirm_cancel_action); - okbtn.click(confirm_cancel_action); - closebtn.click(confirm_cancel_action); - } + if (do_confirm) { + confirm_ok_action = (typeof confirm_ok_action !== "function") ? function(){} : confirm_ok_action; + confirm_cancel_action = (typeof confirm_cancel_action !== "function") ? function(){} : confirm_cancel_action; + okbtn.click(confirm_ok_action); + $("").click(confirm_cancel_action).click(close).appendTo(div); + bg.click(confirm_cancel_action); + okbtn.click(confirm_cancel_action); + closebtn.click(confirm_cancel_action); + } - bg.click(close); - okbtn.click(close); - closebtn.click(close); + bg.click(close); + okbtn.click(close); + closebtn.click(close); - handler.fadeIn(400); + handler.fadeIn(400); } var saved = {}; @@ -148,7 +148,7 @@ function changeStyle(styleName, link) { } document.getElementById('stylesheet').href = styles[styleName]; - selectedstyle = styleName; + selectedstyle = styleName; // Code stylesheet if (!document.getElementById('code_stylesheet')) { @@ -173,8 +173,9 @@ function changeStyle(styleName, link) { link.className = 'selected'; } - if (typeof $ != 'undefined') + if (typeof $ != 'undefined') { $(window).trigger('stylesheet', styleName); + } } @@ -309,26 +310,28 @@ function highlightReply(id) { return true; } - var divs = document.getElementsByTagName('div'); + let divs = document.getElementsByTagName('div'); for (var i = 0; i < divs.length; i++) { - if (divs[i].className.indexOf('post') != -1) + if (divs[i].className.indexOf('post') != -1) { divs[i].className = divs[i].className.replace(/highlighted/, ''); + } } if (id) { - var post = document.getElementById('reply_'+id); - if (post) + let post = document.getElementById('reply_' + id); + if (post) { post.className += ' highlighted'; - window.location.hash = id; + } + window.location.hash = id; } return true; } function generatePassword() { - var pass = ''; - var chars = '{% endraw %}{{ config.genpassword_chars }}{% raw %}'; - for (var i = 0; i < 8; i++) { - var rnd = Math.floor(Math.random() * chars.length); + let pass = ''; + let chars = '{% endraw %}{{ config.genpassword_chars }}{% raw %}'; + for (let i = 0; i < 8; i++) { + let rnd = Math.floor(Math.random() * chars.length); pass += chars.substring(rnd, rnd + 1); } return pass; @@ -356,26 +359,29 @@ function reloadCaptcha() { // Reload captcha images (date reduces chance of caching) // If no securimage captcha is enabled, no elements will be found captchaImgs = document.querySelectorAll('[id=captcha-img]'); - for (var i = 0; i < captchaImgs.length; ++i) - captchaImgs[i].src = "/captcha.php?"+Date.now(); + for (let i = 0; i < captchaImgs.length; ++i) { + captchaImgs[i].src = "/captcha.php?" + Date.now(); + } captchas = document.querySelectorAll('[id=captcha]'); - for (var i = 0; i < captchas.length; ++i) + for (let i = 0; i < captchas.length; ++i) { captchas[i].value = ""; + } } function citeReply(id, with_link) { - var textarea = document.getElementById('body'); - - if (!textarea) return false; + let textarea = document.getElementById('body'); + if (!textarea) { + return false; + } if (document.selection) { // IE textarea.focus(); - var sel = document.selection.createRange(); + let sel = document.selection.createRange(); sel.text = '>>' + id + '\n'; } else if (textarea.selectionStart || textarea.selectionStart == '0') { - var start = textarea.selectionStart; - var end = textarea.selectionEnd; + let start = textarea.selectionStart; + let end = textarea.selectionEnd; textarea.value = textarea.value.substring(0, start) + '>>' + id + '\n' + textarea.value.substring(end, textarea.value.length); textarea.selectionStart += ('>>' + id).length + 1; @@ -385,9 +391,9 @@ function citeReply(id, with_link) { textarea.value += '>>' + id + '\n'; } if (typeof $ != 'undefined') { - var select = document.getSelection().toString(); + let select = document.getSelection().toString(); if (select) { - var body = $('#reply_' + id + ', #op_' + id).find('div.body'); // TODO: support for OPs + let body = $('#reply_' + id + ', #op_' + id).find('div.body'); // TODO: support for OPs // commenting out this condition, not sure of the purpose and gets in the way of citing // var index = body.text().indexOf(select.replace('\n', '')); // for some reason this only works like this // if (index > -1) { @@ -406,25 +412,29 @@ function citeReply(id, with_link) { function rememberStuff() { if (document.forms.post) { if (document.forms.post.password) { - if (!localStorage.password) + if (!localStorage.password) { localStorage.password = generatePassword(); + } document.forms.post.password.value = localStorage.password; } - if (localStorage.name && document.forms.post.elements['name']) + if (localStorage.name && document.forms.post.elements['name']) { document.forms.post.elements['name'].value = localStorage.name; - if (localStorage.email && document.forms.post.elements['email']) + } + if (localStorage.email && document.forms.post.elements['email']) { document.forms.post.elements['email'].value = localStorage.email; + } - if (window.location.hash.indexOf('q') == 1) + if (window.location.hash.indexOf('q') == 1) { citeReply(window.location.hash.substring(2), true); + } if (sessionStorage.body) { - var saved = JSON.parse(sessionStorage.body); + let saved = JSON.parse(sessionStorage.body); if (getCookie('{% endraw %}{{ config.cookies.js }}{% raw %}')) { // Remove successful posts - var successful = JSON.parse(getCookie('{% endraw %}{{ config.cookies.js }}{% raw %}')); - for (var url in successful) { + let successful = JSON.parse(getCookie('{% endraw %}{{ config.cookies.js }}{% raw %}')); + for (let url in successful) { saved[url] = null; } sessionStorage.body = JSON.stringify(saved); @@ -448,8 +458,9 @@ var script_settings = function(script_name) { this.get = function(var_name, default_val) { if (typeof tb_settings == 'undefined' || typeof tb_settings[this.script_name] == 'undefined' || - typeof tb_settings[this.script_name][var_name] == 'undefined') + typeof tb_settings[this.script_name][var_name] == 'undefined') { return default_val; + } return tb_settings[this.script_name][var_name]; } }; @@ -480,7 +491,7 @@ function onready(fnc) { } function ready() { - for (var i = 0; i < onready_callbacks.length; i++) { + for (let i = 0; i < onready_callbacks.length; i++) { onready_callbacks[i](); } } From d7896b9c02eda4816f8fc3924e100f4fb11d0f2f Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 4 Aug 2024 15:37:36 +0200 Subject: [PATCH 11/14] main.js: rename init_stylechooser to camelCase --- templates/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/main.js b/templates/main.js index bfe89bc4..8bff3c03 100755 --- a/templates/main.js +++ b/templates/main.js @@ -211,7 +211,7 @@ function changeStyle(styleName, link) { {% endif %} {% raw %} -function init_stylechooser() { +function initStyleChooser() { var newElement = document.createElement('div'); newElement.className = 'styles'; @@ -466,7 +466,7 @@ var script_settings = function(script_name) { }; function init() { - init_stylechooser(); + initStyleChooser(); initDynamicCaptcha(); {% endraw %} From 8223dcfbadf3cf37eebbc096766d9af47ead6220 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 5 Aug 2024 18:32:11 +0200 Subject: [PATCH 12/14] main.js: rename onready to camelCase --- templates/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/main.js b/templates/main.js index 8bff3c03..cf993ed3 100755 --- a/templates/main.js +++ b/templates/main.js @@ -486,7 +486,7 @@ var RecaptchaOptions = { }; onready_callbacks = []; -function onready(fnc) { +function onReady(fnc) { onready_callbacks.push(fnc); } @@ -501,7 +501,7 @@ function ready() { var post_date = "{{ config.post_date }}"; var max_images = {{ config.max_images }}; -onready(init); +onReady(init); {% if config.google_analytics %}{% raw %} From b83be67b558c82e5727038ded7da1b33c6688575 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 6 Aug 2024 21:55:49 +0200 Subject: [PATCH 13/14] main.js: fix template --- templates/main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/main.js b/templates/main.js index cf993ed3..62e5297b 100755 --- a/templates/main.js +++ b/templates/main.js @@ -288,6 +288,7 @@ function onCaptchaLoad(renderer) { }); } +{% if config.dynamic_captcha %} function isDynamicCaptchaEnabled() { let cookie = getCookie('captcha-required'); return cookie === '1'; From 057addafa8f0b63d14e329dd98df6db69198fa56 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Wed, 7 Aug 2024 00:10:29 +0200 Subject: [PATCH 14/14] main.js: fix reset wiping the captcha token from the form before it was sent --- templates/main.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/main.js b/templates/main.js index 62e5297b..bd0a44c6 100755 --- a/templates/main.js +++ b/templates/main.js @@ -282,7 +282,7 @@ function onCaptchaLoad(renderer) { if (widgetId === null) { console.error('Could not render captcha!'); } - document.addEventListener('post', function(e) { + document.addEventListener('afterdopost', function(e) { // User posted! Reset the captcha. renderer.reset(widgetId); }); @@ -352,7 +352,8 @@ function doPost(form) { saved[document.location] = form.elements['body'].value; sessionStorage.body = JSON.stringify(saved); - document.dispatchEvent(new Event('post')); + // Needs to be delayed by at least 1 frame, otherwise it may reset the form (read captcha) fields before they're sent. + setTimeout(() => document.dispatchEvent(new Event('afterdopost'))); return form.elements['body'].value != "" || (form.elements['file'] && form.elements['file'].value != "") || (form.elements.file_url && form.elements['file_url'].value != ""); }