仅在用户进行交互时触发加载reCAPTCHA库,但不要获取隐藏输入字段的值。

huangapple go评论85阅读模式
英文:

trigger of loading recaptcha library only when user interacts but not getting the value in hidden input field

问题

正如我们都知道,页面速度在搜索引擎优化(SEO)中起着重要作用,每当您尝试实施谷歌的Recaptcha v3时,它会显著减慢您的网站速度。这就是为什么我决定只在用户与输入表单进行交互时触发加载/执行/运行reCaptcha库。实际上,这是完全可能的...
这是我的代码和网站igsavers...

<form method="post" action="/video">
    {{ csrf_field() }}
    <div class="control is-loading">
        <input name="instauser" id="author" class="input is-rounded is-focused is-medium" type="text" placeholder="https://instagram.com/p/41SW_pmmq4/">
    </div>

    <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" value="">
    <div class="control has-text-centered">
        <br>
        <button type="submit" class="button is-danger is-active is-medium">Download &nbsp
            <img src="{{ asset('svg/downloads.svg') }}" width="25px" alt="Instagram video download" />
        </button>
    </div>
</form>
<script type="text/javascript">
    // 触发加载 api.js(recaptcha.js)脚本
    var reCaptchaFocus = function() {
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'https://www.google.com/recaptcha/api.js?render=6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy';
        head.appendChild(script);

        // 移除焦点以避免js错误:
        // Uncaught Error: reCAPTCHA has already been rendered in this element at Object.kh
        document.getElementById('author').removeEventListener('focus', reCaptchaFocus);
    };
    // 为我们的基本HTML表单添加初始事件监听器
    document.getElementById('author').addEventListener('focus', reCaptchaFocus, false);
</script>

这段代码运行良好,但问题在于隐藏的输入字段 g-recaptcha-response 的值将保持为空。以下是在此字段中添加值的代码:

<script>
grecaptcha.ready(function() {
    grecaptcha.execute('6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy', {action: 'homepage'}).then(function(token) {
        document.getElementById('g-recaptcha-response').value = token;
    });
});
</script>

帮助我找出需要在代码中做什么更改,以将值加载到隐藏的输入字段中。

英文:

As we all know that page speed plays an important role in SEO and whenever you tried to implement google Recaptcha v3 it will slow your site a lot. that's why I decided to trigger the loading/execution/running of the reCaptcha library only when the user interacts with the input form(s). This is, actually, quite possible ..
here is my code and website igsavers...

    &lt;form method=&quot;post&quot; action=&quot;/video&quot;&gt;
    {{ csrf_field() }}
    &lt;div class=&quot;control is-loading&quot;&gt;
        &lt;input name=&quot;instauser&quot; id=&quot;author&quot; class=&quot;input is-rounded is-focused is-medium&quot; type=&quot;text&quot; placeholder=&quot;https://instagram.com/p/41SW_pmmq4/&quot;&gt;
    &lt;/div&gt;

    &lt;input type=&quot;hidden&quot; id=&quot;g-recaptcha-response&quot; name=&quot;g-recaptcha-response&quot; value=&quot;&quot;&gt;
    &lt;div class=&quot;control has-text-centered&quot;&gt;
        &lt;br&gt;
        &lt;button type=&quot;submit&quot; class=&quot;button is-danger is-active is-medium&quot;&gt;Download &amp;nbsp
            &lt;img src=&quot;{{ asset(&#39;svg/downloads.svg&#39;) }}&quot; width=&quot;25px&quot; alt=&quot;Instagram video download&quot; /&gt;
        &lt;/button&gt;
    &lt;/div&gt;
&lt;/form&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    	// trigger loading api.js (recaptcha.js) script
    	var reCaptchaFocus = function() {
    	var head = document.getElementsByTagName(&#39;head&#39;)[0];
    	var script = document.createElement(&#39;script&#39;);
    	script.type = &#39;text/javascript&#39;;
    	script.src = &#39;https://www.google.com/recaptcha/api.js?render=6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy&#39;;
    	head.appendChild(script);
    
    	// remove focus to avoid js error:
    	// Uncaught Error: reCAPTCHA has already been rendered in this element at Object.kh
    	document.getElementById(&#39;author&#39;).removeEventListener(&#39;focus&#39;, reCaptchaFocus);
    	};
    	// add initial event listener to our basic HTML form
    	document.getElementById(&#39;author&#39;).addEventListener(&#39;focus&#39;, reCaptchaFocus, false);
    &lt;/script&gt;

this code is working well but the problem is that hidden input type g-recaptcha-response value will remain empty. here is my code of adding value in this field

&lt;script&gt;grecaptcha.ready(function() {
    grecaptcha.execute(&#39;6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy&#39;, {action: &#39;homepage&#39;}).then(function(token) {
       document.getElementById(&#39;g-recaptcha-response&#39;).value=token;
    });
});&lt;/script&gt;

help me to figure out what I change in the code to load the value in hidden input type.

答案1

得分: 1

你不能在没有先加载reCAPTCHA脚本的情况下使用grecaptcha.ready事件。你应该将以下代码段移动到加载脚本之后:

<script type="text/javascript">
    
var reCaptchaFocus = function() {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://www.google.com/recaptcha/api.js?render=6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy';
    head.appendChild(script);

    var recaptchaInterval = setInterval(function() {
        if( !window.grecaptcha || !window.grecaptcha.execute ) { return; }

        clearInterval( recaptchaInterval );

        grecaptcha.execute('6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy', {action: 'homepage'}).then(function(token) {
            document.getElementById('g-recaptcha-response').value=token;
        });
    }, 100 );
	document.getElementById('author').removeEventListener('focus', reCaptchaFocus);
};
document.getElementById('author').addEventListener('focus', reCaptchaFocus, false);
</script>
英文:

You can't use the grecaptcha.ready event without fist loading the recaptcha script. You should remove

&lt;script&gt;grecaptcha.ready(function() {
    grecaptcha.execute(&#39;6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy&#39;, {action: &#39;homepage&#39;}).then(function(token) {
       document.getElementById(&#39;g-recaptcha-response&#39;).value=token;
    });
});&lt;/script&gt;

And place it after you loaded the script:

&lt;script type=&quot;text/javascript&quot;&gt;

var reCaptchaFocus = function() {
    var head = document.getElementsByTagName(&#39;head&#39;)[0];
    var script = document.createElement(&#39;script&#39;);
    script.type = &#39;text/javascript&#39;;
    script.src = &#39;https://www.google.com/recaptcha/api.js?render=6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy&#39;;
    head.appendChild(script);

    var recaptchaInterval = setInterval(function() {
        if( !window.grecaptcha || !window.grecaptcha.execute ) { return; }

        clearInterval( recaptchaInterval );

        grecaptcha.execute(&#39;6LdvEswUAAAAADNVG2ng4ZIfA4mfKUJiuLmLgnUy&#39;, {action: &#39;homepage&#39;}).then(function(token) {
            document.getElementById(&#39;g-recaptcha-response&#39;).value=token;
        });
    }, 100 );
	document.getElementById(&#39;author&#39;).removeEventListener(&#39;focus&#39;, reCaptchaFocus);
};
document.getElementById(&#39;author&#39;).addEventListener(&#39;focus&#39;, reCaptchaFocus, false);
&lt;/script&gt;

huangapple
  • 本文由 发表于 2020年1月4日 01:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定