(recaptchaApiBaseUrl = RECAPTCHA_API_URL)
| 68 | * @param {string} recaptchaApiBaseUrl |
| 69 | */ |
| 70 | export function initRecaptcha(recaptchaApiBaseUrl = RECAPTCHA_API_URL) { |
| 71 | const win = window; |
| 72 | |
| 73 | /** |
| 74 | * Get the data from our name attribute |
| 75 | * sitekey {string} - reCAPTCHA sitekey used to identify the site |
| 76 | * sentinel {string} - string used to psuedo-confirm that we are |
| 77 | * receiving messages from the recaptcha frame |
| 78 | */ |
| 79 | let dataObject; |
| 80 | try { |
| 81 | dataObject = parseJson(win.name); |
| 82 | } catch (e) { |
| 83 | throw new Error(TAG + ' Could not parse the window name.'); |
| 84 | } |
| 85 | |
| 86 | // Get our sitekey from the iframe name attribute |
| 87 | devAssert( |
| 88 | dataObject.sitekey, |
| 89 | 'The sitekey is required for the <amp-recaptcha-input> iframe' |
| 90 | ); |
| 91 | sitekey = dataObject.sitekey; |
| 92 | // Determine the recaptcha api URL based on global property |
| 93 | devAssert( |
| 94 | hasOwn(dataObject, 'global'), |
| 95 | 'The global property is required for the <amp-recaptcha-input> iframe' |
| 96 | ); |
| 97 | if (dataObject.global) { |
| 98 | recaptchaApiBaseUrl = GLOBAL_RECAPTCHA_API_URL; |
| 99 | } |
| 100 | const recaptchaApiUrl = recaptchaApiBaseUrl + sitekey; |
| 101 | |
| 102 | loadScript( |
| 103 | win, |
| 104 | recaptchaApiUrl, |
| 105 | function () { |
| 106 | const {grecaptcha} = win; |
| 107 | |
| 108 | grecaptcha.ready(function () { |
| 109 | initializeIframeMessagingClient(win, grecaptcha, dataObject); |
| 110 | iframeMessagingClient./*OK*/ sendMessage('amp-recaptcha-ready'); |
| 111 | }); |
| 112 | }, |
| 113 | function () { |
| 114 | dev().error(TAG + ' Failed to load recaptcha api script'); |
| 115 | } |
| 116 | ); |
| 117 | } |
| 118 | window.initRecaptcha = initRecaptcha; |
| 119 | |
| 120 | /** |
nothing calls this directly
no test coverage detected