-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
116 lines (104 loc) · 3.38 KB
/
index.html
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ReCaptcha Test Form</title>
<!-- <script src="https://www.google.com/recaptcha/api.js"></script> -->
</head>
<body>
<div class="container">
<h1>
ReCaptcha Test Form
</h1>
<h3>
Setup
</h3>
<form name="params">
<input type="text" id="sitekey"
value=""
placeholder="Google ReCaptcha Site Key">
<input type="text" id="webhook"
value=""
placeholder="URL for your n8n webhook">
</form>
<h3>
Join Our Email List!
</h3>
<form name="email-list">
<fieldset role="group">
<input type="text" name="email" value="" placeholder="A test email">
<button id = 'form-submit'
class="g-recaptcha"
data-sitekey=""
data-callback='onSubmit'
data-action='button'>Submit</button>
</fieldset>
</form>
<article id="token">ReCaptcha Token will show here once you submit the form...</article>
</div>
<script>
document.getElementById('sitekey').addEventListener('change', updateSiteKey, false)
function updateSiteKey() {
const sitekey = document.getElementById('sitekey').value
// if the sitekey is valid
if (/^[0-9a-zA-Z_-]{40}$/.test(sitekey)) {
console.log('valid sitekey, initializing recaptcha')
// set the sitekey on the submit button
const submit = document.getElementById('form-submit')
submit.setAttribute('data-sitekey', sitekey)
// remove the script if it already exists
document.getElementById('grecaptcha-load')?.remove()
// and load the GRecaptcha script
const s = document.createElement('script')
s.setAttribute('src', 'https://www.google.com/recaptcha/api.js')
s.setAttribute('id', 'grecaptcha-load')
document.head.append(s)
} else {
console.log('invalid sitekey, not initializing')
}
}
async function onSubmit(token) {
// show the token we just got
console.log('got token:', token)
const sitekey = document.getElementById('token').innerText = token
const email = document.getElementsByName('email')[0].value
const webhook = document.getElementById('webhook').value
// build the form
const form = new FormData()
form.append("email", email)
form.append("g-recaptcha-response", token)
// send the request
console.log('sending request')
const response = await fetch(webhook, {
mode: "no-cors",
method: "POST",
headers: {
"Accept": "application/json"
},
body: form
})
const results = await response.text()
// there won't be any results since n8n doesn't send anything back
// 200 OK means it succeeded
}
</script>
</body>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css"
/>
<style>
.container {
margin: 14px;
max-width: 50%;
min-width: 500px;
}
form input {
margin-bottom: 6px;
}
form {
margin-bottom: 14px;
}
</style>
</html>