chg: usr: replace Google ReCaptcha with Cap instance #13
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
import 'cap-widget';
|
||||
|
||||
const FORMS_SELECTOR = '.auth-form';
|
||||
const SUBMIT_SELECTOR = 'button[type="submit"], input[type="submit"]';
|
||||
|
||||
const createInvisibleCap = apiEndpoint => {
|
||||
const host = document.createElement('cap-widget');
|
||||
host.style.display = 'none';
|
||||
|
||||
const cap = new window.Cap({ apiEndpoint }, host);
|
||||
|
||||
return { cap, host };
|
||||
};
|
||||
|
||||
const lockSubmit = (form, locked) => {
|
||||
const submit = form.querySelector(SUBMIT_SELECTOR);
|
||||
if (!submit) return;
|
||||
submit.disabled = locked;
|
||||
};
|
||||
|
||||
const bindInvisibleCap = (form, apiEndpoint) => {
|
||||
const { cap, host } = createInvisibleCap(apiEndpoint);
|
||||
form.appendChild(host);
|
||||
|
||||
let solving = null;
|
||||
|
||||
const solveToken = async () => {
|
||||
if (!solving) {
|
||||
solving = cap.solve()
|
||||
.finally(() => {
|
||||
solving = null;
|
||||
});
|
||||
}
|
||||
|
||||
const result = await solving;
|
||||
return result?.token ?? cap.token ?? '';
|
||||
};
|
||||
|
||||
lockSubmit(form, true);
|
||||
|
||||
solveToken()
|
||||
.catch(() => '')
|
||||
.finally(() => lockSubmit(form, false));
|
||||
|
||||
form.addEventListener('submit', async e => {
|
||||
if (cap.token) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
lockSubmit(form, true);
|
||||
|
||||
try {
|
||||
const token = await solveToken();
|
||||
if (!token) {
|
||||
lockSubmit(form, false);
|
||||
return;
|
||||
}
|
||||
form.requestSubmit();
|
||||
} catch (_) {
|
||||
lockSubmit(form, false);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const initInvisibleCap = () => {
|
||||
const endpointSource =
|
||||
document.querySelector('[data-cap-api-endpoint]') ||
|
||||
document.getElementById('mine-wrapper');
|
||||
const apiEndpoint = endpointSource?.dataset.capApiEndpoint;
|
||||
|
||||
if (!apiEndpoint || typeof window.Cap !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
document.querySelectorAll(FORMS_SELECTOR).forEach(form => bindInvisibleCap(form, apiEndpoint));
|
||||
};
|
||||
|
||||
initInvisibleCap();
|
||||
Reference in New Issue
Block a user