homeflowjs 1.0.17 → 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.
- package/app/recaptcha.js +150 -70
- package/hooks/use-scroll-to.hook.js +28 -11
- 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
|
-
|
31
|
-
const
|
32
|
-
|
33
|
-
|
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
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
45
|
+
const widget = window.grecaptcha.render(`recaptcha-wrapper-${recaptchaId}`, {
|
46
|
+
sitekey: '6Lf16S0UAAAAAL0YaWCLRhChd4Uk77b-4Ai0ZdRY',
|
47
|
+
callback: onRecaptchaSubmit,
|
48
|
+
size: 'invisible',
|
49
|
+
});
|
67
50
|
|
68
|
-
|
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
|
66
|
+
const onRecaptchaV2Ready = (triggeredFormEl) => {
|
72
67
|
if (window.recaptchaLoadingScriptInterval) {
|
73
|
-
|
68
|
+
clearInterval(window.recaptchaLoadingScriptInterval);
|
74
69
|
window.recaptchaLoadingScriptInterval = undefined;
|
75
70
|
}
|
76
71
|
|
77
|
-
|
72
|
+
if (triggeredFormEl) {
|
73
|
+
initUniqRecaptchaV2Wrapper({form: triggeredFormEl})
|
74
|
+
} else {
|
75
|
+
const forms = document.querySelectorAll(formSelectors.join(', '));
|
78
76
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
77
|
+
forms.forEach((form, i) => {
|
78
|
+
initUniqRecaptchaV2Wrapper({ form, id: i })
|
79
|
+
});
|
80
|
+
}
|
81
|
+
}
|
84
82
|
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
91
|
-
|
92
|
-
|
93
|
-
size: 'invisible',
|
94
|
-
});
|
89
|
+
const recaptchaScript = document.createElement('script');
|
90
|
+
recaptchaScript.src = srcConfig[version];
|
91
|
+
recaptchaScript.id = 'recaptcha-script';
|
95
92
|
|
96
|
-
|
97
|
-
? [...window.widgetIds, widget]
|
98
|
-
: [widget]
|
93
|
+
document.body.appendChild(recaptchaScript);
|
99
94
|
|
100
|
-
|
101
|
-
|
102
|
-
|
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(
|
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
|
-
|
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
|
-
|
118
|
-
|
119
|
-
|
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
|
-
|
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
|
-
|
125
|
-
|
126
|
-
} else {
|
127
|
-
addRecaptchaV2(formSelectors);
|
205
|
+
const initDynamicallyOpenFormsRecaptcha = () => {
|
206
|
+
document.body.addEventListener('focusin', handleRecaptchaRelatedBodyFocus);
|
128
207
|
}
|
129
208
|
|
130
|
-
|
209
|
+
initDynamicallyOpenFormsRecaptcha();
|
210
|
+
initRecaptcha()
|
131
211
|
});
|
132
212
|
};
|
@@ -1,7 +1,8 @@
|
|
1
1
|
import { useEffect } from 'react';
|
2
2
|
import { isFunction } from '../utils';
|
3
3
|
|
4
|
-
const scrollToTarget = (target, { yOffset = 0 } = {}) => {
|
4
|
+
const scrollToTarget = (target, { yOffset = 0, additionalYOffset = 0 } = {}) => {
|
5
|
+
|
5
6
|
// If target is chunk ID
|
6
7
|
let targetElement;
|
7
8
|
if (!isNaN(target)) {
|
@@ -25,33 +26,46 @@ const scrollToTarget = (target, { yOffset = 0 } = {}) => {
|
|
25
26
|
}
|
26
27
|
}
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
if(additionalYOffset) {
|
30
|
+
verticalOffset = verticalOffset + parseInt(additionalYOffset, 10);
|
31
|
+
}
|
32
|
+
|
33
|
+
let y = 0;
|
34
|
+
const targetTop = targetElement.getBoundingClientRect().top;
|
35
|
+
if(targetTop > 0) y =
|
36
|
+
targetElement.getBoundingClientRect().top +
|
37
|
+
window.pageYOffset +
|
38
|
+
verticalOffset;
|
30
39
|
|
31
40
|
window.scrollTo({ top: y, behavior: 'smooth' });
|
32
41
|
history.scrollRestoration = 'manual';
|
33
42
|
}
|
34
43
|
};
|
35
44
|
|
36
|
-
const
|
45
|
+
const getURLParams = (hash) => {
|
37
46
|
const [, search] = hash.split('?');
|
38
|
-
const { target } = Object.fromEntries(new URLSearchParams(search));
|
39
|
-
return target;
|
47
|
+
const { target, yOffset } = Object.fromEntries(new URLSearchParams(search));
|
48
|
+
return { target, yOffsetFromURLParams: yOffset };
|
40
49
|
};
|
41
50
|
|
42
51
|
function handleScrollToClick(options) {
|
43
52
|
// eslint-disable-next-line func-names
|
44
53
|
return function () {
|
45
|
-
const target =
|
54
|
+
const { target, yOffsetFromURLParams } = getURLParams(this.hash);
|
46
55
|
if (target) {
|
47
|
-
scrollToTarget(target,
|
56
|
+
scrollToTarget(target, {
|
57
|
+
...options,
|
58
|
+
additionalYOffset: yOffsetFromURLParams,
|
59
|
+
});
|
48
60
|
}
|
49
61
|
};
|
50
62
|
}
|
51
63
|
|
52
64
|
const useScrollTo = (options) => {
|
53
65
|
useEffect(() => {
|
54
|
-
const scrollToEls = document.querySelectorAll(
|
66
|
+
const scrollToEls = document.querySelectorAll(
|
67
|
+
'[href^="#scroll-to"], [href^="#/scroll-to"]'
|
68
|
+
);
|
55
69
|
scrollToEls.forEach((el) => {
|
56
70
|
el.addEventListener(
|
57
71
|
'click',
|
@@ -59,8 +73,11 @@ const useScrollTo = (options) => {
|
|
59
73
|
);
|
60
74
|
});
|
61
75
|
|
62
|
-
const target =
|
63
|
-
scrollToTarget(target,
|
76
|
+
const { target, yOffsetFromURLParams } = getURLParams(window.location.hash);
|
77
|
+
scrollToTarget(target, {
|
78
|
+
...options,
|
79
|
+
additionalYOffset: yOffsetFromURLParams,
|
80
|
+
});
|
64
81
|
}, []);
|
65
82
|
};
|
66
83
|
|