我想以编程方式调用不可见的reCaptcha挑战。根据reCaptcha document,我写了以下代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>reCaptcha test</title>
<script>
function onReCaptchaSubmit(token) {
alert("reCaptcha challenge response:\n" + token);
}
</script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<style>
.grecaptcha-badge {
display: none;
}
</style>
</head>
<body>
<a href="javascript:void(0);" onclick="grecaptcha.execute();">Challenge</a>
<div class="g-recaptcha"
data-sitekey="*******"
data-callback="onReCaptchaSubmit"
data-size="invisible">
</div>
</body>
</html>
从HTML链接调用grecaptcha.execute();
以显示reCaptcha challenge弹出窗口。在大多数情况下,它运作良好。但有时候,在调用grecaptcha.execute();
后,它会回拨onReCaptchaSubmit()
,而不显示reCapcha弹出窗口和用户挑战。
我该如何解决?
提前致谢。
由于recaptcha有一个到期时间,你必须等到你的recaptcha会话到期或在grecaptcha.reset()
中调用onRecaptchaSubmit
以确保每次用户点击链接时都会出现用户挑战:
function onRecaptchaSubmit(token) {
/* any behavior you want */
//...
window.grecaptcha.reset();
}
每次调用grecaptcha.execute()
时,您都希望隐形reCaptcha显示用户挑战,但它不会那样工作。根据我的经验,它只有在对你有疑问时才会显示出挑战。如果Google确定您是人,那么它会跳过用户质询的显示并继续回调。