【问题标题】:Invisible captcha executed as soon as page loads页面加载后立即执行不可见的验证码
【发布时间】:2017-05-06 17:43:02
【问题描述】:

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

grecaptcha is not defined

当然我相信是因为 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>

【问题讨论】:

  • 请分享您迄今为止尝试过的代码并尽可能截图
  • @Jonasw 如果我使用“render=explicit”,我必须使用 render() 方法,这将使 recaptcha 可见,我需要它不可见。
  • @Bucci83 好的,但 onload 应该仍然可以工作,不是吗?
  • @Bucci83 Window.onload = grecaptcha.execute() 将立即执行,您希望 window.onload = ()=>grecaptcha.execute();
  • 我把它改成 ()=>grecaptcha.execute() 并且它没有运行......有什么建议吗?你说 window.onload = grecaptcha.execute() 将在解析器到达代码后立即执行,我该如何解决?

标签: javascript invisible-recaptcha


【解决方案1】:

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

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

【讨论】:

  • 嗨,是的,我试过了,但没有被调用 :( 我添加了一个简单的 console.out,但我没有看到它被调用,当我添加 render=explicit 参数时它可以工作,但这需要我也添加了 render() 方法,所以它会添加 recaptcha 复选框,我不希望这样......
  • @Bucci83 我认为recaptcha 跟踪用户移动,所以该页面上必须有访问时间和一些用户交互......
【解决方案2】:

哦!我刚刚使用

让它工作
$(window).on('load',function(){
    grecaptcha.execute()
})

感谢@jonasw 指出问题出在 window.onload 事件 =D

【讨论】:

  • ...当然它不适用于 IE11...无论如何,现在我遇到了选择图像的重新验证挑战,我想执行速度太快了,API 没有不知道是人还是机器人,所以它要求选择图像,我想我需要找出其他方法......
  • 你最后是怎么解决这个问题的?我想这样做以避免超时,但不想要图像.....
猜你喜欢
  • 2015-11-03
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
相关资源
最近更新 更多