homeflowjs 1.0.18 → 1.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/app/recaptcha.js +150 -70
  2. package/package.json +1 -1
package/app/recaptcha.js CHANGED
@@ -20,6 +20,8 @@ export default () => {
20
20
  window.addEventListener('DOMContentLoaded', () => {
21
21
  if (window.noRecaptcha) return null;
22
22
 
23
+ const recaptchaFormActions = ['leads', 'form_submissions', 'properties', 'user/send_to_friend'];
24
+
23
25
  const formSelectors = [
24
26
  'form[action="/leads"]',
25
27
  'form[action="/form_submissions"]',
@@ -27,106 +29,184 @@ export default () => {
27
29
  'form[action="/user/send_to_friend"]',
28
30
  ];
29
31
 
30
- function addRecaptchaV3(formSelectors) {
31
- const recaptchaScript = document.createElement('script');
32
- recaptchaScript.src = 'https://www.google.com/recaptcha/api.js?render=6LeKPOUUAAAAAEKNWIu2qkNwdOqVxH41v0D7fe-K';
33
- recaptchaScript.id = 'recaptcha-script';
32
+ const initUniqRecaptchaV2Wrapper = ({form, id}) => {
33
+ const recaptchaId = id || Math.random().toString().substring(2);
34
+ const formRecaptchaWrapper = document.createElement('div');
35
+ formRecaptchaWrapper.classList.add('recaptcha-wrapper');
36
+ formRecaptchaWrapper.id = `recaptcha-wrapper-${recaptchaId}`;
34
37
 
35
- // add the recaptcha script only after a form input is focused
36
- for (let i = 0; i < formSelectors.length; i++) {
37
- const formInputs = document.querySelectorAll(`${formSelectors[i]} input, ${formSelectors[i]} textarea`);
38
- formInputs.forEach((input) => {
39
- input.addEventListener('focus', () => {
40
- if (!document.getElementById('recaptcha-script')) {
41
- document.body.appendChild(recaptchaScript);
42
- }
43
- });
44
- });
38
+ form.appendChild(formRecaptchaWrapper);
45
39
 
46
- const forms = document.querySelectorAll(formSelectors[i]);
47
- forms.forEach((form) => {
48
- form.addEventListener('submit', (e) => {
49
- if (!document.getElementById('g-recaptcha-response') || !document.getElementById('g-recaptcha-response').value) {
50
- e.preventDefault();
51
- Homeflow.set('submitted_form', e.target);
52
- grecaptcha.execute('6LeKPOUUAAAAAEKNWIu2qkNwdOqVxH41v0D7fe-K', { action: 'homepage' })
53
- .then((token) => {
54
- submitRecaptchaForm(token);
55
- });
56
- }
57
- });
58
- form.classList.add('has-recaptcha');
59
- });
40
+ const onRecaptchaSubmit = () => {
41
+ form.submit();
42
+ Homeflow.kickEvent('after_successful_recaptcha', form);
60
43
  }
61
- }
62
44
 
63
- const appendRecaptchaScript = () => {
64
- const recaptchaScript = document.createElement('script');
65
- recaptchaScript.src = 'https://www.google.com/recaptcha/api.js';
66
- recaptchaScript.id = 'recaptcha-script';
45
+ const widget = window.grecaptcha.render(`recaptcha-wrapper-${recaptchaId}`, {
46
+ sitekey: '6Lf16S0UAAAAAL0YaWCLRhChd4Uk77b-4Ai0ZdRY',
47
+ callback: onRecaptchaSubmit,
48
+ size: 'invisible',
49
+ });
67
50
 
68
- document.body.appendChild(recaptchaScript);
51
+ window.widgetIds = window.widgetIds
52
+ ? [...window.widgetIds, widget]
53
+ : [widget]
54
+
55
+ form.addEventListener('submit', (e) => {
56
+ if (e.target.classList.contains('hfjs-form-invalid')) {
57
+ return;
58
+ }
59
+
60
+ e.preventDefault();
61
+ Homeflow.set('submitted_form', e.target);
62
+ grecaptcha.execute(widget);
63
+ });
69
64
  }
70
65
 
71
- const onRecaptchaReady = (formSelectors) => {
66
+ const onRecaptchaV2Ready = (triggeredFormEl) => {
72
67
  if (window.recaptchaLoadingScriptInterval) {
73
- clearTimeout(window.recaptchaLoadingScriptInterval);
68
+ clearInterval(window.recaptchaLoadingScriptInterval);
74
69
  window.recaptchaLoadingScriptInterval = undefined;
75
70
  }
76
71
 
77
- const forms = document.querySelectorAll(formSelectors.join(', '));
72
+ if (triggeredFormEl) {
73
+ initUniqRecaptchaV2Wrapper({form: triggeredFormEl})
74
+ } else {
75
+ const forms = document.querySelectorAll(formSelectors.join(', '));
78
76
 
79
- forms.forEach((form, i) => {
80
- const formRecaptchaWrapper = document.createElement('div');
81
- formRecaptchaWrapper.id = `recaptcha-wrapper-${i}`;
82
-
83
- form.appendChild(formRecaptchaWrapper);
77
+ forms.forEach((form, i) => {
78
+ initUniqRecaptchaV2Wrapper({ form, id: i })
79
+ });
80
+ }
81
+ }
84
82
 
85
- const onRecaptchaSubmit = () => {
86
- form.submit();
87
- Homeflow.kickEvent('after_successful_recaptcha', form);
88
- }
83
+ const appendRecaptchaScript = (version) => {
84
+ const srcConfig = {
85
+ v2: 'https://www.google.com/recaptcha/api.js',
86
+ v3: 'https://www.google.com/recaptcha/api.js?render=6LeKPOUUAAAAAEKNWIu2qkNwdOqVxH41v0D7fe-K',
87
+ }
89
88
 
90
- const widget = window.grecaptcha.render(`recaptcha-wrapper-${i}`, {
91
- sitekey: '6Lf16S0UAAAAAL0YaWCLRhChd4Uk77b-4Ai0ZdRY',
92
- callback: onRecaptchaSubmit,
93
- size: 'invisible',
94
- });
89
+ const recaptchaScript = document.createElement('script');
90
+ recaptchaScript.src = srcConfig[version];
91
+ recaptchaScript.id = 'recaptcha-script';
95
92
 
96
- window.widgetIds = window.widgetIds
97
- ? [...window.widgetIds, widget]
98
- : [widget]
93
+ document.body.appendChild(recaptchaScript);
99
94
 
100
- form.addEventListener('submit', (e) => {
101
- if (e.target.classList.contains('hfjs-form-invalid')) {
102
- return;
95
+ if (version === 'v2') {
96
+ window.recaptchaLoadingScriptInterval = setInterval(() => {
97
+ if (window.grecaptcha?.render) {
98
+ onRecaptchaV2Ready()
103
99
  }
100
+ }, 300);
101
+ }
102
+ }
104
103
 
104
+ const initRecaptchaV3 = (form) => {
105
+ form.addEventListener('submit', (e) => {
106
+ if (!document.getElementById('g-recaptcha-response') || !document.getElementById('g-recaptcha-response').value) {
105
107
  e.preventDefault();
106
108
  Homeflow.set('submitted_form', e.target);
107
- grecaptcha.execute(widget);
108
- });
109
+ grecaptcha.execute('6LeKPOUUAAAAAEKNWIu2qkNwdOqVxH41v0D7fe-K', { action: 'homepage' })
110
+ .then((token) => {
111
+ submitRecaptchaForm(token);
112
+ });
113
+ }
109
114
  });
115
+ form.classList.add('has-recaptcha');
110
116
  }
111
117
 
112
- function addRecaptchaV2(formSelectors) {
118
+ const handleRecaptchaV3ForTargetForm = (targetFormEl) => {
113
119
  if (!document.getElementById('recaptcha-script')) {
114
- appendRecaptchaScript();
120
+ appendRecaptchaScript('v3')
115
121
  }
122
+
123
+ initRecaptchaV3(targetFormEl)
124
+ }
116
125
 
117
- window.recaptchaLoadingScriptInterval = setInterval(() => {
118
- if (window.grecaptcha?.render) {
119
- onRecaptchaReady(formSelectors)
126
+ function addRecaptchaV3(targetFormEl) {
127
+ for (let i = 0; i < formSelectors.length; i++) {
128
+ if (targetFormEl) {
129
+ handleRecaptchaV3ForTargetForm(targetFormEl)
130
+ } else {
131
+ const formInputs = document.querySelectorAll(`${formSelectors[i]} input, ${formSelectors[i]} textarea`);
132
+ formInputs.forEach((input) => {
133
+ input.addEventListener('focus', () => {
134
+ if (!document.getElementById('recaptcha-script')) {
135
+ appendRecaptchaScript('v3');
136
+ }
137
+ });
138
+ });
120
139
  }
121
- }, 300);
140
+
141
+ const forms = document.querySelectorAll(formSelectors[i]);
142
+ forms.forEach((form) => {
143
+ handleRecaptchaV3ForTargetForm(form)
144
+ });
145
+ }
146
+ }
147
+
148
+ const handleRecaptchaV2ForTargetForm = (targetFormEl) => {
149
+ const hasRecaptcha = targetFormEl?.querySelector('.recaptcha-wrapper');
150
+
151
+ if(hasRecaptcha) {
152
+ return;
153
+ }
154
+
155
+ if (!document.getElementById('recaptcha-script')) {
156
+ appendRecaptchaScript('v2')
157
+ }
158
+ if (window.grecaptcha) {
159
+ onRecaptchaV2Ready(targetFormEl)
160
+ }
161
+ }
162
+
163
+ function addRecaptchaV2(targetFormEl) {
164
+ if (targetFormEl) {
165
+ handleRecaptchaV2ForTargetForm(targetFormEl)
166
+ } else {
167
+ for (let i = 0; i < formSelectors.length; i++) {
168
+ const formInputs = document.querySelectorAll(
169
+ `${formSelectors[i]} input, ${formSelectors[i]} textarea, ${formSelectors[i]} select`
170
+ );
171
+ formInputs.forEach((input) => {
172
+ input.addEventListener('focusin', (event) => {
173
+ event.stopPropagation();
174
+ if (!document.getElementById('recaptcha-script')) {
175
+ appendRecaptchaScript('v2')
176
+ }
177
+ });
178
+ });
179
+ }
180
+ }
181
+ }
182
+
183
+ const initRecaptcha = (targetFormEl) => {
184
+ if (Homeflow.get('recaptcha_version') === 3) {
185
+ addRecaptchaV3(targetFormEl);
186
+ } else {
187
+ addRecaptchaV2(targetFormEl);
188
+ }
189
+ }
190
+
191
+ const handleRecaptchaRelatedBodyFocus = (event) => {
192
+ event.stopPropagation();
193
+ const isFormElementFocused = ['SELECT', 'INPUT', 'TEXTAREA'].includes(event.target.nodeName);
194
+ const targetFormEl = event.target.closest('form');
195
+ const targetFormAction = targetFormEl?.getAttribute('action');
196
+ const isRecaptchaForm = targetFormAction
197
+ ? recaptchaFormActions.some((action) => targetFormAction.includes(action))
198
+ : false
199
+
200
+ if (isFormElementFocused && isRecaptchaForm) {
201
+ initRecaptcha(targetFormEl);
202
+ }
122
203
  }
123
204
 
124
- if (Homeflow.get('recaptcha_version') === 3) {
125
- addRecaptchaV3(formSelectors);
126
- } else {
127
- addRecaptchaV2(formSelectors);
205
+ const initDynamicallyOpenFormsRecaptcha = () => {
206
+ document.body.addEventListener('focusin', handleRecaptchaRelatedBodyFocus);
128
207
  }
129
208
 
130
- return null;
209
+ initDynamicallyOpenFormsRecaptcha();
210
+ initRecaptcha()
131
211
  });
132
212
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homeflowjs",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "sideEffects": [
5
5
  "modal/**/*",
6
6
  "user/default-profile/**/*",