mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-03 09:58:37 +09:00
The `gitea.com/go-chi/captcha` package only exists for Gitea, so it moves into `modules/imagecaptcha`. - Reloading an expired challenge shows a new image instead of a broken one - Every answer is consumed on its first check - OpenID registration stops after a failed captcha --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import {isDarkTheme} from '../utils.ts';
|
|
import {registerGlobalEventFunc} from '../modules/observer.ts';
|
|
|
|
export async function initCaptcha() {
|
|
registerGlobalEventFunc('click', 'onImageCaptchaReload', (img: HTMLImageElement) => {
|
|
const url = new URL(img.src);
|
|
url.searchParams.set('reload', String(Date.now()));
|
|
img.src = url.href;
|
|
});
|
|
|
|
const captchaEl = document.querySelector('#captcha');
|
|
if (!captchaEl) return;
|
|
|
|
const siteKey = captchaEl.getAttribute('data-sitekey')!;
|
|
const isDark = isDarkTheme();
|
|
|
|
const params = {
|
|
sitekey: siteKey,
|
|
theme: isDark ? 'dark' : 'light',
|
|
};
|
|
|
|
switch (captchaEl.getAttribute('data-captcha-type') ?? '') {
|
|
case 'g-recaptcha': {
|
|
if (window.grecaptcha) {
|
|
window.grecaptcha.ready(() => {
|
|
window.grecaptcha.render(captchaEl, params);
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'cf-turnstile': {
|
|
if (window.turnstile) {
|
|
window.turnstile.render(captchaEl, params);
|
|
}
|
|
break;
|
|
}
|
|
case 'h-captcha': {
|
|
if (window.hcaptcha) {
|
|
window.hcaptcha.render(captchaEl, params);
|
|
}
|
|
break;
|
|
}
|
|
case 'm-captcha': {
|
|
// ref: https://github.com/mCaptcha/glue/blob/master/packages/vanilla/README.md
|
|
// sample: https://github.com/mCaptcha/glue/blob/master/packages/vanilla/static/embeded.html
|
|
// @mcaptcha/vanilla-glue 0.1.0-rc2 auto-runs on module load, use the existing elements to render.
|
|
await import('@mcaptcha/vanilla-glue');
|
|
break;
|
|
}
|
|
default:
|
|
}
|
|
}
|