From 6740d4d5290480363389f09298da13b073de2222 Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Mon, 16 Nov 2020 07:43:39 +0000 Subject: [PATCH] Auto remove captcha field if its no longer needed close #284 --- gulp/res/js/captcha.js | 10 ++++++++++ gulp/res/js/forms.js | 6 +++++- models/forms/makepost.js | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/gulp/res/js/captcha.js b/gulp/res/js/captcha.js index 3b563c85..5c6edfcb 100644 --- a/gulp/res/js/captcha.js +++ b/gulp/res/js/captcha.js @@ -80,6 +80,16 @@ class CaptchaController { xhr.send(null); } + removeCaptcha() { + const postForm = document.getElementById('postform'); + const captchaField = postForm.querySelector('.captcha'); + if (captchaField) { + //delete the whole row + const captchaRow = captchaField.closest('.row'); + captchaRow.remove(); + } + } + addMissingCaptcha() { const postSubmitButton = document.getElementById('submitpost'); const captchaFormSectionHtml = captchaformsection({ captchaGridSize }); diff --git a/gulp/res/js/forms.js b/gulp/res/js/forms.js index a7274805..fbfadbaa 100644 --- a/gulp/res/js/forms.js +++ b/gulp/res/js/forms.js @@ -74,7 +74,6 @@ class formHandler { this.enctype = this.form.getAttribute('enctype'); this.messageBox = form.querySelector('#message'); this.captchaField = form.querySelector('.captchafield') || form.querySelector('.g-recaptcha') || form.querySelector('.h-captcha'); - this.submit = form.querySelector('input[type="submit"]'); if (this.submit) { this.originalSubmitText = this.submit.value; @@ -174,6 +173,11 @@ class formHandler { } else if(captchaResponse && hcaptcha) { hcaptcha.reset(); } + if (xhr.getResponseHeader('x-captcha-enabled') === 'false') { + //remove captcha if it got disabled after you opened the page + captchaController.removeCaptcha(); + this.captchaField = null; + } this.submit.disabled = false; this.submit.value = this.originalSubmitText; let json; diff --git a/models/forms/makepost.js b/models/forms/makepost.js index b0672292..6b763bc6 100644 --- a/models/forms/makepost.js +++ b/models/forms/makepost.js @@ -553,6 +553,9 @@ module.exports = async (req, res, next) => { 'board': res.locals.board }; + //let frontend script know if captcha is still enabled + res.set('x-captcha-enabled', captchaMode > 0); + if (req.headers['x-using-live'] != null && data.thread) { //defer build and post will come live res.json({