如何使用reCAPTCHA v3设置徽章位置?

Sti*_*ing 12 recaptcha

我想使用带有v3的内联徽章,但没有关于v3徽章位置的文档.

Sam*_*ton 17

您可以使用Javascript在V3中内联徽章,但尚未记录.


例如,将render参数设置为explicitonload为回调添加参数onRecaptchaLoadCallback.

<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=onRecaptchaLoadCallback"></script>
Run Code Online (Sandbox Code Playgroud)

然后设置你的回调,并且不要忘记输入你想要徽章去的地方的id /元素,在这种情况下,id是inline-badge.

<script>
    function onRecaptchaLoadCallback() {
        var clientId = grecaptcha.render('inline-badge', {
            'sitekey': '6Ldqyn4UAAAAAN37vF4e1vsebmNYIA9UVXZ_RfSp',
            'badge': 'inline',
            'size': 'invisible'
        });

        grecaptcha.ready(function() {
            grecaptcha.execute(clientId, {
                    action: 'action_name'
                })
                .then(function(token) {
                    // Verify the token on the server.
                });
        });
    }
</script>
Run Code Online (Sandbox Code Playgroud)

资源

  • 无需自定义回调,只需包含 JavaScript,如下所示:`&lt;script src="https://www.google.com/recaptcha/api.js?badge=bottomleft"&gt;&lt;/script&gt;`(感谢[这个答案](/sf/answers/4824777961/)在另一个线程上。) (3认同)

小智 14

您可以将其包含在您的 sass 或 css 中以在左侧对齐并保持悬停功能处于活动状态。

.grecaptcha-badge {
  width: 70px !important;
  overflow: hidden !important;
  transition: all 0.3s ease !important;
  left: 4px !important;
}

.grecaptcha-badge:hover {
  width: 256px !important;
}
Run Code Online (Sandbox Code Playgroud)


Omm*_*awn 8

我只是改变了grecaptcha-badge类的CSS,如下所示:

.grecaptcha-badge { 
    bottom:25px !important; 
}
Run Code Online (Sandbox Code Playgroud)


pxi*_*pxi 4

EDIT1:使用参数查看答案render=explicit。这个答案仍然有效,但这是在记录此功能之前并且不那么干净。


我已经成功地通过放置一个相关的框并将 reCaptcha v3 徽章分离到其中来做到这一点。然后对容器进行一些样式调整。

#grecaptcha-box {
    width: 260px;
    overflow: hidden;
    position: relative;
    height: 75px;
}
#grecaptcha-box .grecaptcha-badge {
    box-shadow: none !important;
}
Run Code Online (Sandbox Code Playgroud)

然后放置该框,然后放置 JavaScript。

<form>
    <!-- Rest of your form here... -->
    <input type="hidden" id="recaptcha-token"  name="recaptcha-token">
    <input type="hidden" id="recaptcha-action" name="recaptcha-action">
</form>

<div id="grecaptcha-box"></div>

<!-- Recaptcha v3 -->
<script src="https://www.google.com/recaptcha/api.js?render=xxxxxxxxxxxxxxxxxxxxxxxxxxxx"></script>
<script id="js-recaptcha-init">
const RECAPTCHA_PUBLIC_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
grecaptcha.ready(function() {
    // Remove badge from fixed place
    var gb = document.getElementsByClassName("grecaptcha-badge")[0];
    gb.style.position = 'absolute';
    document.getElementById('grecaptcha-box').appendChild(gb.cloneNode(true));

    gb.parentNode.removeChild(gb);

    // Do the normal recaptcha things...
    var grecaptcha_opts = {'action' : 'custom_action'};
    grecaptcha.execute(RECAPTCHA_PUBLIC_KEY, grecaptcha_opts)
        .then(function(token) {
            document.getElementById('recaptcha-token').value = token;
            document.getElementById('recaptcha-action').value = grecaptcha_opts.action;
        });
});
</script>
Run Code Online (Sandbox Code Playgroud)