页面加载后立即执行隐形验证码

Buc*_*i83 4 javascript invisible-recaptcha

我正在尝试使用 google 的功能来检查页面请求是来自人类还是机器人。通过这个,我想包含一个不可见的验证码,并在页面加载完成后立即提交验证。我已经浏览了谷歌文档https://developers.google.com/recaptcha/docs/invisible,正常的例子对我来说很好用。但是我正在尝试以一种 gr​​ecaptcha.execute() 在页面加载后立即触发的方式对其进行调整,我已经尝试使用 window.onload 或 (window).ready() 并且我得到了相同的错误

grecaptcha is not defined
Run Code Online (Sandbox Code Playgroud)

当然我相信这是因为grecaptcha脚本没有准备好,实际上如果我设置了1秒或2秒的超时,它会执行得很好。

我试图将“onload”参数添加到 api.js 中,但没有运气......顺便说一下,api.js 只在 HEAD“recaptcha__en.js”中包含了实际的 recaptcha 脚本

我不需要实际的表格或提交任何联系信息,只需要验证查看页面的主题是人还是机器人

任何想法/建议将不胜感激!谢谢!!

编辑:按要求添加代码:

<html>
<head>
    <script>
        //called only when I set the timeout
        function onSubmit(){
            console.log("Submitted")
        }

        //not being called by the "onload parameter of the api.js script"
        function loadCaptcha(){
          console.log("loaded")
          setTimeout(function(){grecaptcha.execute()},2000)
        }

    </script>
    <script src='https://www.google.com/recaptcha/api.js' async defer></script>
    <!--<script src='https://www.google.com/recaptcha/api.js?onload="loadCaptcha"' async defer></script>----it doesnt make any difference for my case-->
</head>
<body>
<div class="g-recaptcha"
      data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxx"
      data-callback="onSubmit"
      data-size="invisible">
</div>
<script>
    //Window.onload = setTimeout(function(){grecaptcha.execute()},2000) this works!
    Window.onload = grecaptcha.execute() //----this doesn't work---//

</script
</body>
Run Code Online (Sandbox Code Playgroud)

Jon*_*lms 5

您可以为定义 onload 回调的 url 设置一个参数:

<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback">
</script>
<script>
function onloadCallback(){
  grecaptcha.execute();
 }
</script>
Run Code Online (Sandbox Code Playgroud)