AMP

amp-recaptcha-input

Description

Appends a reCAPTCHA v3 token to AMP form submissions.

 

Required Scripts

<script async custom-element="amp-recaptcha-input" src="https://cdn.ampproject.org/v0/amp-recaptcha-input-0.1.js"></script>

Supported Layouts

Usage

This extension adds a parameter containing a reCAPTCHA response token when a parent <form> element submits. amp-recaptcha-input does this by creating an iframe to load the reCAPTCHA v3 api script using the provided site key, and calling grecaptcha.execute with the provided site key and action.

Prerequisite

Integrating reCAPTCHA for any document on the internet requires several steps, as described in the official documentation for reCATPCHA. There are several steps, but generally this requires registering a sitekey, and setting up a server endpoint that can process the reCAPTCHA signal sent from an AMP or other HTML document.

One caveat to be aware when registering a sitekey: you will need to provide all the hostnames that you plan to use this sitekey. For instance, your.com and www.your.com are treated as different hostnames. Please note that this is different than the configuration for general HTML documents. See this issue for more details.

This doc will focus more on how reCAPTCHA is configured on AMP.

Example

This example demonstrates how <amp-recaptcha-input> usage on an AMP page correlates to calls on the grecaptcha object and form body. <amp-recaptcha-input> must be a child of a <form> element.

<amp-recaptcha-input> usage

<form amp-form-attributes-go-here>
  ...
  <amp-recaptcha-input layout="nodisplay" name="reCAPTCHA_body_key" data-sitekey="reCAPTCHA_site_key" data-action="reCAPTCHA_example_action">
  </amp-recaptcha-input>
  ...
</form>

Corresponding grecaptcha call

grecaptcha.execute('reCAPTCHA_site_key', {action: 'reCAPTCHA_example_action'});

Corresponding AMP form submit body

{
  ...other form params
  "reCAPTCHA_body_key": "returned_reCAPTCHA_response_token"
}

Attributes

layout (required)

Required value is nodisplay.

name (required)

Name of the <amp-recaptcha-input>. Will be used as a parameter key in the AMP form body submission.

data-sitekey (required)

reCAPTCHA v3 site key for the registered website.

data-action (required)

reCAPTCHA v3 action to be executed on form submission.

data-global (optional)

By default, the iframe loads the recaptcha api script using the www.google.com endpoint. There are some situations when this is not accessible. When the data-global attribute is included, the component will load the script from the www.recaptcha.net endpoint instead. More information can be found in the reCAPTCHA FAQ.

Validation

See <amp-recaptcha-input> rules in the AMP validator specification.

도움이 더 필요하신가요?

이 문서를 수십 번 읽었음에도 여전히 궁금한 점이 남아 있나요? 다른 개발자들도 같은 심정일지 모릅니다. Stack Overflow에서 소통해 보세요.

Stack Overflow로 이동
버그나 누락된 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub로 이동하기