gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {isDarkTheme} from '../utils.ts';
  2. export async function initCaptcha() {
  3. const captchaEl = document.querySelector('#captcha');
  4. if (!captchaEl) return;
  5. const siteKey = captchaEl.getAttribute('data-sitekey');
  6. const isDark = isDarkTheme();
  7. const params = {
  8. sitekey: siteKey,
  9. theme: isDark ? 'dark' : 'light',
  10. };
  11. switch (captchaEl.getAttribute('data-captcha-type')) {
  12. case 'g-recaptcha': {
  13. if (window.grecaptcha) {
  14. window.grecaptcha.ready(() => {
  15. window.grecaptcha.render(captchaEl, params);
  16. });
  17. }
  18. break;
  19. }
  20. case 'cf-turnstile': {
  21. if (window.turnstile) {
  22. window.turnstile.render(captchaEl, params);
  23. }
  24. break;
  25. }
  26. case 'h-captcha': {
  27. if (window.hcaptcha) {
  28. window.hcaptcha.render(captchaEl, params);
  29. }
  30. break;
  31. }
  32. case 'm-captcha': {
  33. const mCaptcha = await import(/* webpackChunkName: "mcaptcha-vanilla-glue" */'@mcaptcha/vanilla-glue');
  34. // FIXME: the mCaptcha code is not right, it's a miracle that the wrong code could run
  35. // * the "vanilla-glue" has some problems with es6 module.
  36. // * the INPUT_NAME is a "const", it should not be changed.
  37. // * the "mCaptcha.default" is actually the "Widget".
  38. // @ts-expect-error TS2540: Cannot assign to 'INPUT_NAME' because it is a read-only property.
  39. mCaptcha.INPUT_NAME = 'm-captcha-response';
  40. const instanceURL = captchaEl.getAttribute('data-instance-url');
  41. new mCaptcha.default({
  42. siteKey: {
  43. instanceUrl: new URL(instanceURL),
  44. key: siteKey,
  45. },
  46. });
  47. break;
  48. }
  49. default:
  50. }
  51. }