captcha.js 796 B

123456789101112131415161718192021222324252627282930
  1. const grecaptcha = window.grecaptcha
  2. let recaptchaId
  3. const recaptchaCallbacks = []
  4. export async function validateCaptchaV2() {
  5. if (
  6. // Detect blocked recaptcha
  7. typeof grecaptcha === 'undefined' ||
  8. // Detect stubbed recaptcha
  9. typeof grecaptcha.render !== 'function' ||
  10. typeof grecaptcha.execute !== 'function' ||
  11. typeof grecaptcha.reset !== 'function'
  12. ) {
  13. return
  14. }
  15. if (recaptchaId === undefined) {
  16. const el = document.getElementById('recaptcha')
  17. recaptchaId = grecaptcha.render(el, {
  18. callback: token => {
  19. recaptchaCallbacks.splice(0).forEach(cb => cb(token))
  20. grecaptcha.reset(recaptchaId)
  21. },
  22. })
  23. }
  24. return await new Promise(resolve => {
  25. recaptchaCallbacks.push(resolve)
  26. grecaptcha.execute(recaptchaId)
  27. })
  28. }