-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (59 loc) · 2.27 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { checkForSpam } from './checkForSpam.js';
document.addEventListener('DOMContentLoaded', function () {
function waitForForm() {
return new Promise((resolve) => {
const checkForm = setInterval(() => {
const form = document.querySelector('form[id^="hsForm_"]');
if (form) {
clearInterval(checkForm);
resolve(form);
}
}, 100); // Check every 100ms
});
}
waitForForm().then((form) => {
console.log('Form found:', form);
form.addEventListener('submit', async function (event) {
event.preventDefault(); // Stop the form from submitting immediately
event.stopImmediatePropagation(); // Prevent other listeners on submit from submitting
console.log('Gets inside waitForForm');
// Collect form data
const formData = new FormData(form);
// Convert FormData to JSON for spam check
const data = {};
formData.forEach((value, key) => {
data[key] = value;
});
console.log('form data: :', data);
// Perform your spam check
try {
const { isSpam, reasons, score } = await checkForSpam(data);
console.log('isSpam: ', isSpam);
if (isSpam) {
console.log('Spam detected for the following reasons:', reasons);
alert('This submission appears to be spam.');
return; // Exit the function to prevent form submission
}
console.log('Begin code below isSpam if condition');
// If not spam, create an XMLHttpRequest to submit the form data
const xhr = new XMLHttpRequest();
xhr.open('POST', form.action, true);
xhr.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded'
);
// Convert the form data back to URL-encoded string
const urlEncodedData = new URLSearchParams(formData).toString();
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
alert('Form submitted successfully!');
form.reset(); // Optional: reset the form after successful submission
}
};
xhr.send(urlEncodedData);
} catch (error) {
console.error('Error during spam check:', error);
}
});
});
});