mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-12 16:33:32 -05:00
317f8fa73f
[web] form-helpers/captcha: step up detection of loaded recaptcha GitOrigin-RevId: d9ed3b99277ffcdca68df9f8e0b162faeb363451
30 lines
796 B
JavaScript
30 lines
796 B
JavaScript
const grecaptcha = window.grecaptcha
|
|
|
|
let recaptchaId
|
|
const recaptchaCallbacks = []
|
|
|
|
export async function validateCaptchaV2() {
|
|
if (
|
|
// Detect blocked recaptcha
|
|
typeof grecaptcha === 'undefined' ||
|
|
// Detect stubbed recaptcha
|
|
typeof grecaptcha.render !== 'function' ||
|
|
typeof grecaptcha.execute !== 'function' ||
|
|
typeof grecaptcha.reset !== 'function'
|
|
) {
|
|
return
|
|
}
|
|
if (recaptchaId === undefined) {
|
|
const el = document.getElementById('recaptcha')
|
|
recaptchaId = grecaptcha.render(el, {
|
|
callback: token => {
|
|
recaptchaCallbacks.splice(0).forEach(cb => cb(token))
|
|
grecaptcha.reset(recaptchaId)
|
|
},
|
|
})
|
|
}
|
|
return await new Promise(resolve => {
|
|
recaptchaCallbacks.push(resolve)
|
|
grecaptcha.execute(recaptchaId)
|
|
})
|
|
}
|