在我的网页中,我通过调用函数 grecaptcha.execute() 来验证基于Invisible Recaptcha的表单 . 通常这对用户来说是不可见的;所有他们看到的是在 grecaptcha 完成验证后触发的表单提交 .

有时,触发不那么不可见的Recaptcha行为(例如,如果用户表现得很可疑)并且向用户显示他们必须解决的常规验证码 . 发生这种情况时,我想以编程方式检测它 .

Is it possible to programmatically discover if a captcha was shown to the user after the call to grecaptcha.execute()?


到目前为止,我已经尝试使用 MutationEventsMutationObservers 观看DOM . 不幸的是,所有有趣的事情都发生在一个 iframe 内部,似乎无法判断是否显示了完整的验证码 .

我不想测量 grecaptcha 调用我给它的回调需要多长时间,因为那将是不可靠的(例如,慢速移动连接会使结果产生偏差) .


标记:

<form id="form" method="post" action="/form">
    Name: (required) <input id="name" name="name">
    <div id="g-recaptcha-div"
        class="g-recaptcha"
        data-sitekey="keeeey"
        data-callback="onRecaptchaDone"
        data-size="invisible"></div>
    <button id="thebutton">Submit</button>
</form>

码:

function onSubmit(event) {
    event.preventDefault();
    grecaptcha.reset();
    grecaptcha.execute();
}
function onRecaptchaDone(token) {
    form.submit();
}
window.onload = function() {
    form.onsubmit = onSubmit;
}