forked from leftypol/leftypol
main.js: require captcha before sending a post
This commit is contained in:
parent
869e2602ef
commit
3be6111fa4
1 changed files with 14 additions and 2 deletions
|
@ -246,6 +246,8 @@ function getCookie(cookie_name) {
|
|||
{% if config.hcaptcha or config.turnstile %} // If any captcha
|
||||
// Global captcha object. Assigned by `onCaptchaLoad()`.
|
||||
var captcha_renderer = null;
|
||||
// Captcha widget id of the post form.
|
||||
var postCaptchaId = null;
|
||||
|
||||
{% if config.hcaptcha %} // If hcaptcha
|
||||
function onCaptchaLoadHcaptcha() {
|
||||
|
@ -256,7 +258,9 @@ function onCaptchaLoadHcaptcha() {
|
|||
callback: params['on-success'],
|
||||
}),
|
||||
remove: (widgetId) => { /* Not supported */ },
|
||||
reset: (widgetId) => hcaptcha.reset(widgetId)
|
||||
reset: (widgetId) => hcaptcha.reset(widgetId),
|
||||
hasResponse: (widgetId) => !!hcaptcha.getResponse(widgetId),
|
||||
execute: (widgetId) => hcaptcha.execute(widgetId)
|
||||
};
|
||||
|
||||
onCaptchaLoad(renderer);
|
||||
|
@ -291,7 +295,9 @@ function onCaptchaLoadTurnstile(action) {
|
|||
return widgetId;
|
||||
},
|
||||
remove: (widgetId) => turnstile.remove(widgetId),
|
||||
reset: (widgetId) => turnstile.reset(widgetId)
|
||||
reset: (widgetId) => turnstile.reset(widgetId),
|
||||
hasResponse: (widgetId) => !!turnstile.getResponse(widgetId),
|
||||
execute: (widgetId) => turnstile.execute(widgetId)
|
||||
};
|
||||
|
||||
onCaptchaLoad(renderer);
|
||||
|
@ -313,6 +319,7 @@ function onCaptchaLoad(renderer) {
|
|||
if (widgetId === null) {
|
||||
console.error('Could not render captcha!');
|
||||
}
|
||||
postCaptchaId = widgetId;
|
||||
document.addEventListener('afterdopost', function(e) {
|
||||
// User posted! Reset the captcha.
|
||||
renderer.reset(widgetId);
|
||||
|
@ -390,6 +397,11 @@ function doPost(form) {
|
|||
saved[document.location] = form.elements['body'].value;
|
||||
sessionStorage.body = JSON.stringify(saved);
|
||||
|
||||
if (captcha_renderer && postCaptchaId && !captcha_renderer.hasResponse(postCaptchaId)) {
|
||||
captcha_renderer.execute(postCaptchaId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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 != "");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue