forked from dhairyagothi/100_days_100_web_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptcha.js
164 lines (151 loc) · 5.76 KB
/
captcha.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
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
const captchaTypeSelect = document.getElementById('captchaType');
const captchaContainer = document.getElementById('captchaContainer');
const textInput = document.querySelector(".textcaptcha input");
const refreshButton = document.querySelector(".refresh");
const resultMessage = document.querySelector(".result");
const submitButton = document.querySelector(".button button");
let currentCaptcha = null;
let attempts = 0;
const maxAttempts = 3;
let lockoutEndTime = 0;
const generateTextCaptcha = () => {
return Math.random().toString(36).substring(2, 8).toUpperCase();
};
const generateImageCaptcha = () => {
const images = [
{ emoji: '🐶', name: 'dog' },
{ emoji: '🐱', name: 'cat' },
{ emoji: '🐭', name: 'mouse' },
{ emoji: '🐹', name: 'hamster' },
{ emoji: '🐰', name: 'rabbit' },
{ emoji: '🦊', name: 'fox' },
{ emoji: '🐻', name: 'bear' },
{ emoji: '🐼', name: 'panda' },
{ emoji: '🐨', name: 'koala' }
];
const correctIndex = Math.floor(Math.random() * images.length);
const shuffled = images.sort(() => 0.5 - Math.random()).slice(0, 6);
if (!shuffled.includes(images[correctIndex])) {
shuffled[Math.floor(Math.random() * 6)] = images[correctIndex];
}
return { images: shuffled, correct: images[correctIndex] };
};
const generateMathCaptcha = () => {
const num1 = Math.floor(Math.random() * 10) + 1;
const num2 = Math.floor(Math.random() * 10) + 1;
const operation = Math.random() < 0.5 ? '+' : '-';
const question = `${num1} ${operation} ${num2}`;
const answer = operation === '+' ? num1 + num2 : num1 - num2;
return { question, answer };
};
const speakCaptcha = (text, repeat = 2, speed = 0.5) => {
return new Promise((resolve) => {
const utterance = new SpeechSynthesisUtterance();
utterance.text = Array(repeat).fill(text.split('').join(' ')).join('. . . ');
utterance.rate = speed;
utterance.onend = resolve;
speechSynthesis.speak(utterance);
});
};
const generateCaptcha = () => {
const type = captchaTypeSelect.value;
switch (type) {
case 'text':
currentCaptcha = generateTextCaptcha();
captchaContainer.innerHTML = `<span style="font-size: 24px; letter-spacing: 5px;">${currentCaptcha}</span>`;
break;
case 'image':
const { images, correct } = generateImageCaptcha();
currentCaptcha = correct.name;
captchaContainer.innerHTML = `
<p>Select the ${correct.name}</p>
<div class="image-grid">
${images.map(img => `<div class="image-option">${img.emoji}</div>`).join('')}
</div>
`;
captchaContainer.querySelectorAll('.image-option').forEach(option => {
option.addEventListener('click', () => {
textInput.value = images.find(img => img.emoji === option.textContent).name;
});
});
break;
case 'audio':
currentCaptcha = generateTextCaptcha();
captchaContainer.innerHTML = `
<p>Click play and enter the spoken characters:</p>
<button id="playAudio">Play Audio</button>
`;
const playButton = document.getElementById('playAudio');
playButton.addEventListener('click', async () => {
playButton.disabled = true;
try {
await speakCaptcha(currentCaptcha);
} catch (error) {
console.error('Speech synthesis failed:', error);
alert('Audio playback failed. Please try again or use a different CAPTCHA type.');
} finally {
playButton.disabled = false;
}
});
break;
break;
case 'math':
const { question, answer } = generateMathCaptcha();
currentCaptcha = answer.toString();
captchaContainer.innerHTML = `<span style="font-size: 24px;">${question} = ?</span>`;
break;
}
};
const lockoutUser = () => {
const lockoutDuration = 60; // 60 seconds lockout
lockoutEndTime = Date.now() + lockoutDuration * 1000;
updateLockoutUI();
};
const updateLockoutUI = () => {
const now = Date.now();
if (now < lockoutEndTime) {
const remainingTime = Math.ceil((lockoutEndTime - now) / 1000);
submitButton.disabled = true;
resultMessage.textContent = `Too many attempts. Please wait ${remainingTime} seconds.`;
resultMessage.style.color = "red";
setTimeout(updateLockoutUI, 1000);
} else {
submitButton.disabled = false;
resultMessage.textContent = "";
attempts = 0;
generateCaptcha();
}
};
const verifyCaptcha = () => {
if (Date.now() < lockoutEndTime) {
return;
}
const userInput = textInput.value.trim().toLowerCase();
const isCorrect = userInput === currentCaptcha.toString().toLowerCase();
if (isCorrect) {
resultMessage.textContent = "Correct! CAPTCHA solved.";
resultMessage.style.color = "green";
attempts = 0;
setTimeout(() => {
textInput.value = "";
resultMessage.textContent = "";
generateCaptcha();
}, 1500);
} else {
attempts++;
if (attempts >= maxAttempts) {
lockoutUser();
} else {
resultMessage.textContent = `Incorrect. Please try again. (Attempt ${attempts}/${maxAttempts})`;
resultMessage.style.color = "red";
}
}
};
captchaTypeSelect.addEventListener('change', generateCaptcha);
refreshButton.addEventListener("click", () => {
if (Date.now() >= lockoutEndTime) {
generateCaptcha();
}
});
submitButton.addEventListener("click", verifyCaptcha);
generateCaptcha();