📌  相关文章
📜  Google AMP amp-bind-recaptcha(1)

📅  最后修改于: 2023-12-03 14:41:35.409000             🧑  作者: Mango

Google AMP amp-bind-recaptcha

Google AMP (Accelerated Mobile Pages) is a framework that allows for the creation of lightweight and fast-loading web pages. The amp-bind-recaptcha component is a tool released by Google AMP framework that allows developers to include a Google reCAPTCHA in their AMP pages.

What is Google reCAPTCHA?

Google reCAPTCHA is a free tool that prevents spam and abuse on websites. It is a security measure that requires users to prove that they are human by completing a simple task, such as selecting images that match a given description.

What is amp-bind-recaptcha component?

The amp-bind-recaptcha component is a tool released by Google AMP framework that allows developers to include a Google reCAPTCHA in their AMP pages. It works by using the amp-bind component to dynamically bind the response of the reCAPTCHA to its parent document. This means that the reCAPTCHA response can be used by the developer to take actions such as submitting a form or blocking access to a page.

How to use amp-bind-recaptcha component?

To use the amp-bind-recaptcha component, you must first include the necessary scripts in the head of your AMP page:

<script async custom-element="amp-bind-recaptcha" src="https://cdn.ampproject.org/v0/amp-bind-recaptcha-0.1.js"></script>
<script async src="https://www.google.com/recaptcha/api.js"></script>

Next, add the amp-bind-recaptcha component to your AMP page with the following code:

<amp-bind-recaptcha id="recaptcha" data-sitekey="YOUR_SITE_KEY" layout="fixed" width="auto" height="auto"></amp-bind-recaptcha>

Replace YOUR_SITE_KEY with the site key you get from the Google reCAPTCHA admin console.

Finally, use the amp-bind component to bind the response of the reCAPTCHA to a variable. Here's an example of how to do this:

<amp-state id="captchaResponse">
  <script type="application/json">{ "captchaResponse": "" }</script>
</amp-state>

<div on="tap:AMP.setState({captchaResponse: recaptcha.value})">
  <button>Submit</button>
</div>

Here, we've created an amp-state variable called captchaResponse to hold the response of the reCAPTCHA. The user's response is captured when they tap the submit button, and is stored in the captchaResponse variable using the amp-bind component.

Conclusion

The amp-bind-recaptcha component is a useful tool for developers who want to improve the security of their AMP pages. By including a Google reCAPTCHA in your AMP page, you can prevent spam and abuse, while providing a smooth user experience.