|
| 1 | +type RecaptchaApi = { |
| 2 | + render: (elementId: string, options: Record<string, unknown>) => number; |
| 3 | + reset: (widgetId: number) => void; |
| 4 | +}; |
| 5 | + |
| 6 | +type Config = { |
| 7 | + siteKey: string; |
| 8 | + scriptSrc: string; |
| 9 | + formControlId: string; |
| 10 | + type: "grecaptcha" | "hcaptcha"; |
| 11 | + onLoadCallbackName: string; |
| 12 | +}; |
| 13 | + |
1 | 14 | interface Window {
|
2 | 15 | [key: string]: any;
|
3 | 16 |
|
4 |
| - grecaptcha: { |
5 |
| - render: (elementId: string, options: Record<string, unknown>) => number; |
6 |
| - reset: (widgetId: number) => void; |
7 |
| - }; |
| 17 | + grecaptcha: RecaptchaApi; |
| 18 | + hcaptcha: RecaptchaApi; |
8 | 19 | }
|
9 | 20 |
|
10 | 21 | document.addEventListener("DOMContentLoaded", () => {
|
11 | 22 | document.querySelectorAll<HTMLFormElement>(".palmtree-form").forEach((form) => {
|
12 |
| - const element = form.querySelector<HTMLElement>(".g-recaptcha-autoload"); |
13 |
| - |
14 |
| - if (element && element.dataset.onload && element.dataset.script_url) { |
15 |
| - window[element.dataset.onload] = () => { |
16 |
| - const widgetId = window.grecaptcha.render(element.id, { |
17 |
| - sitekey: element.dataset.site_key, |
18 |
| - callback: (response: string) => { |
19 |
| - const formControl = document.querySelector<HTMLInputElement>(`#${element.dataset.form_control}`); |
20 |
| - if (formControl) { |
21 |
| - formControl.value = response; |
22 |
| - |
23 |
| - if (form.palmtreeForm("isInitialized")) { |
24 |
| - form.palmtreeForm("clearState", formControl); |
25 |
| - } |
| 23 | + const element = form.querySelector<HTMLElement>(".palmtree-captcha-autoload"); |
| 24 | + |
| 25 | + if (!element) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const config = JSON.parse(element.dataset.palmtreeFormCaptcha || "") as Config; |
| 30 | + |
| 31 | + if (!config) { |
| 32 | + console.error("Invalid configuration"); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if (config.type !== "grecaptcha" && config.type !== "hcaptcha") { |
| 37 | + console.error(`Captcha type ${config.type} is not supported.`); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + window[config.onLoadCallbackName] = () => { |
| 42 | + const api = window[config.type]; |
| 43 | + |
| 44 | + const widgetId = api.render(element.id, { |
| 45 | + sitekey: config.siteKey, |
| 46 | + callback: (response: string) => { |
| 47 | + const formControl = document.querySelector<HTMLInputElement>(`#${config.formControlId}`); |
| 48 | + if (formControl) { |
| 49 | + formControl.value = response; |
| 50 | + |
| 51 | + const palmtreeForm = window.palmtreeForm.getInstance(form); |
| 52 | + |
| 53 | + if (palmtreeForm) { |
| 54 | + palmtreeForm.clearState([formControl]); |
26 | 55 | }
|
27 |
| - }, |
28 |
| - }); |
| 56 | + } |
| 57 | + }, |
| 58 | + }); |
29 | 59 |
|
30 |
| - ["error", "success"].forEach((event) => { |
31 |
| - form.addEventListener(`${event}.palmtreeForm`, () => { |
32 |
| - window.grecaptcha.reset(widgetId); |
33 |
| - }); |
| 60 | + ["error", "success"].forEach((event) => { |
| 61 | + form.addEventListener(`palmtreeForm.${event}`, () => { |
| 62 | + api.reset(widgetId); |
34 | 63 | });
|
35 |
| - }; |
| 64 | + }); |
| 65 | + }; |
36 | 66 |
|
37 |
| - const script = document.createElement("script"); |
38 |
| - script.async = true; |
39 |
| - script.defer = true; |
40 |
| - script.src = element.dataset.script_url; |
| 67 | + const script = document.createElement("script"); |
| 68 | + script.async = true; |
| 69 | + script.defer = true; |
| 70 | + script.src = config.scriptSrc; |
41 | 71 |
|
42 |
| - document.head.append(script); |
43 |
| - } |
| 72 | + document.head.append(script); |
44 | 73 | });
|
45 | 74 | });
|
0 commit comments