Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
121 lines (108 loc) · 3.4 KB
/
Copy pathindex.html
File metadata and controls
121 lines (108 loc) · 3.4 KB
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
117
118
119
120
121
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Complexity V2</title>
</head>
<body>
<div id="app">
<script src="./index.js" type="module"></script>
</div>
<footer>
<div style="font-style: italic">
This is a work in progress! If you want to receive an email when this
project is finished, enter your email below:
</div>
<form id="signup-form" style="margin-top: 1rem">
<input
type="email"
name="email"
id="email"
placeholder="you@example.com"
required
/>
<div id="turnstile-container"></div>
<button type="submit">Subscribe</button>
<p id="form-status"></p>
</form>
</footer>
<script>
const TURNSTILE_SITEKEY = "0x4AAAAAACGTVUPmIFsJov8d";
const SUBMIT_URL = "https://api.ricky0123.com/api/new-sign-up";
let turnstileReady = false;
let turnstileToken = null;
let pendingSubmit = false;
function loadTurnstile() {
return new Promise((resolve) => {
if (window.turnstile) {
resolve();
return;
}
const script = document.createElement("script");
script.src =
"https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onTurnstileLoad";
script.async = true;
window.onTurnstileLoad = resolve;
document.head.appendChild(script);
});
}
async function initTurnstile() {
await loadTurnstile();
turnstile.render("#turnstile-container", {
sitekey: TURNSTILE_SITEKEY,
callback: (token) => {
turnstileToken = token;
turnstileReady = true;
if (pendingSubmit) {
submitForm();
}
},
});
}
async function submitForm() {
const status = document.getElementById("form-status");
const email = document.getElementById("email").value;
status.textContent = "Subscribing...";
try {
const response = await fetch(SUBMIT_URL, {
method: "POST",
body: JSON.stringify({
email,
token: turnstileToken,
source: window.location.hostname,
name: "unknown",
}),
headers: { "Content-Type": "application/json" },
});
const result = await response.json();
if (result.success) {
status.textContent = "Thanks!";
document.getElementById("signup-form").reset();
turnstile.reset();
turnstileToken = null;
turnstileReady = false;
} else {
status.textContent = "Something went wrong. Try again?";
}
} catch (err) {
status.textContent = "Something went wrong. Try again?";
}
pendingSubmit = false;
}
document
.getElementById("signup-form")
.addEventListener("submit", async (e) => {
e.preventDefault();
const status = document.getElementById("form-status");
if (!turnstileReady) {
status.textContent = "One moment...";
pendingSubmit = true;
initTurnstile();
return;
}
submitForm();
});
</script>
</body>
</html>