-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
272 lines (229 loc) · 9.37 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Splittic Captcha - Frontend</title>
<meta property="og:title" content="Splittic Captcha - Frontend">
<meta property="og:description" content="Interactive captcha with image selection">
<meta property="og:image" content="https://cdn.discordapp.com/icons/1187442403871822004/875c92d082d2623cded9e58ec4e6ddba.webp?size=512">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
h2 {
text-align: center;
color: #333;
}
.captcha-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
margin-top: 20px;
}
.captcha-box {
position: relative;
width: 128px;
height: 128px;
border: 1px solid #ccc;
overflow: hidden;
cursor: pointer;
display: flex;
align-items: flex-start;
justify-content: flex-start;
position: relative;
}
.captcha-box img {
width: 100%;
height: 100%;
object-fit: cover;
}
.selected {
border: 2px solid blue;
}
.indicator-dot {
width: 15px;
height: 15px;
border-radius: 50%;
position: absolute;
top: 5px;
left: 5px;
display: none;
}
.correct {
background-color: green;
}
.incorrect {
background-color: red;
}
#submit,
#regenerate {
margin-top: 20px;
padding: 10px;
font-size: 16px;
background-color: #4caf50;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
}
#regenerate {
background-color: #337ab7;
margin-left: 10px;
}
</style>
</head>
<body>
<h2>Splittic Captcha - Frontend</h2>
<div>
<p id="captchaHeader"></p>
</div>
<div class="captcha-container" id="captchaContainer"></div>
<button id="submit" onclick="validateCaptcha()">Submit</button>
<button id="regenerate" onclick="generateCaptcha()">Regenerate</button>
<script>
let selectedCategory = "";
document.addEventListener("DOMContentLoaded", function () {
generateCaptcha();
});
function generateCaptcha() {
submitButton = document.getElementById("submit")
submitButton.disabled = true;
regenerateButton = document.getElementById("regenerate");
regenerateButton.disabled = true;
const captchaContainer = document.getElementById("captchaContainer");
const captchaHeader = document.getElementById("captchaHeader");
categories = [
"cat", "car", "train", "person", "dog", "lion", "elephant", "bird", "tree",
"flower", "building", "bicycle", "fish", "book", "computer", "keyboard",
"mountain", "sun", "moon", "star", "guitar", "pizza", "beach", "camera",
"umbrella", "clock", "apple", "robot", "shoe", "hat", "globe", "ship",
"cupcake", "rocket", "cloud", "key", "sword", "butterfly", "horse",
"moonlight", "fire", "cactus", "castle", "dragon", "volcano", "snowflake",
"lighthouse", "bridge", "rainbow", "planet", "ocean", "penguin", "unicorn",
"dragonfly", "tiger", "helicopter", "tractor", "sunflower", "dolphin",
"microphone", "pineapple", "robot", "whale", "hot air balloon", "airplane",
"carousel", "jellyfish", "piano", "coffee cup", "candle", "feather",
"giraffe", "owl", "hamburger", "telephone", "camera", "microscope",
"rocketship", "train station", "motorcycle", "chocolate", "ice cream",
"leopard", "deer", "waterfall", "parrot", "penguin", "strawberry",
"lighthouse", "butterfly", "caterpillar", "rose", "sunset", "sunrise",
"desert", "forest", "waterfall", "cottage", "hedgehog", "turtle", "palm tree",
"eagle", "koala", "snake", "panda", "cheetah", "zebra", "kangaroo",
"dragonfly", "bat", "dinosaur", "kite", "bee", "crown", "sailboat", "toothbrush",
"broom", "umbrella", "volcano", "crown", "couch", "train tracks", "calculator",
"snowman", "raindrop", "laptop", "globe", "puzzle piece", "sunglasses",
"compass", "light bulb", "mailbox", "pencil", "cherry", "mushroom",
"teapot", "jigsaw puzzle", "mailbox", "butterfly", "jacket", "maple leaf",
"penguin", "giraffe", "sunflower", "cactus", "eiffel tower", "hourglass",
"mermaid", "pirate ship", "wizard hat", "airship", "rocket", "telescope",
"moonwalk", "chess piece", "palm tree", "cactus", "cloud", "rainbow",
"snowflake", "volcano", "tornado", "whirlpool", "earthquake", "lightning"
];
const addArticle = (word) => {
const vowels = ["a", "e", "i", "o", "u"];
return vowels.includes(word[0].toLowerCase()) ? "an " + word : "a " + word;
};
selectedCategory = categories[Math.floor(Math.random() * categories.length)];
selectedCategoryTitle = addArticle(selectedCategory);
captchaHeader.textContent = `Select all images that contain ${selectedCategoryTitle}`;
captchaContainer.innerHTML = "";
// Ensure there are at least 3 images of the wanted category
const selectedCategoryIndices = [];
for (let i = 0; i < 9; i++) {
const categoryIndex = i < 3
? categories.indexOf(selectedCategory)
: Math.floor(Math.random() * categories.length);
selectedCategoryIndices.push(categoryIndex);
}
const fetchPromises = selectedCategoryIndices.map((categoryIndex, index) => {
return fetchImageBase64(categories[categoryIndex])
.then(base64 => {
const captchaBox = document.createElement("div");
captchaBox.classList.add("captcha-box");
captchaBox.dataset.category = categories[categoryIndex];
captchaBox.innerHTML = `<img src="${base64}" alt="${categories[categoryIndex]}">`;
const indicatorDot = document.createElement("div");
captchaBox.addEventListener("click", toggleSelection);
captchaContainer.appendChild(captchaBox); // Append the image to the container
});
});
// Wait for all images to be fetched and loaded
Promise.all(fetchPromises)
.then(() => {
console.log("All images loaded successfully!");
submitButton.disabled = false;
regenerateButton.disabled = false;
})
.catch(error => {
console.error("Error loading images:", error);
});
}
function fetchImageBase64(category) {
const imageUrl = `https://source.unsplash.com/random/?${category}`;
return fetch(imageUrl)
.then(response => response.blob())
.then(blob => new Promise(resolve => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
}));
}
function toggleSelection() {
this.classList.toggle("selected");
}
function validateCaptcha() {
submitButton.disabled = true;
regenerateButton.disabled = true;
const selectedBoxes = document.querySelectorAll(".selected");
const selectedCategories = Array.from(selectedBoxes).map(box => box.dataset.category);
const allImagesSelected = selectedCategories.every(category => category === selectedCategory);
const allImagesRequiredSelected = document.querySelectorAll(`.captcha-box[data-category="${selectedCategory}"]`).length === selectedBoxes.length;
const resultContainer = document.createElement("div");
resultContainer.style.position = "fixed";
resultContainer.style.top = "50%";
resultContainer.style.left = "50%";
resultContainer.style.transform = "translate(-50%, -50%)";
resultContainer.style.display = "flex";
resultContainer.style.flexDirection = "column";
resultContainer.style.alignItems = "center";
resultContainer.style.justifyContent = "center"; // Center the content vertically
resultContainer.style.zIndex = "9999";
resultContainer.style.width = "150px"; // Set the width to create a square shape
resultContainer.style.height = "150px"; // Set the height to create a square shape
resultContainer.style.borderRadius = "10px"; // Add rounded corners
resultContainer.style.boxShadow = "0 0 10px rgba(0, 0, 0, 0.3)"; // Add a subtle shadow
const resultSymbol = document.createElement("span");
resultSymbol.style.fontSize = "72px"; // Increase the font size for a larger symbol
resultSymbol.style.color = "white";
if (allImagesSelected && allImagesRequiredSelected) {
resultContainer.style.background = "green";
resultSymbol.innerHTML = "✔"; // Checkmark symbol
setTimeout(() => {
submitButton.disabled = false;
regenerateButton.disabled = false;
generateCaptcha();
resultContainer.remove();
}, 3000);
} else {
resultContainer.style.background = "red";
resultSymbol.innerHTML = "✘"; // Cross symbol
setTimeout(() => {
submitButton.disabled = false;
regenerateButton.disabled = false;
resultContainer.remove();
}, 3000);
}
resultContainer.appendChild(resultSymbol);
document.body.appendChild(resultContainer);
}
</script>
</body>
</html>