-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptcha.js
68 lines (52 loc) · 2.06 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
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const captchaLength = 6;
function generateCaptchaText() {
let captchaText = '';
for (let i = 0; i < captchaLength; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
captchaText += characters.charAt(randomIndex);
}
return captchaText;
}
function createCaptchaImage(text) {
const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 80;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = 'bold 40px Impact, Charcoal, sans-serif';
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
ctx.strokeStyle = 'black';
for (let i = 0; i < 5; i++) {
ctx.beginPath();
ctx.moveTo(0, Math.random() * canvas.height);
ctx.lineTo(canvas.width, Math.random() * canvas.height);
ctx.stroke();
}
return canvas.toDataURL();
}
function updateCaptcha() {
const captchaText = generateCaptchaText();
const captchaImage = createCaptchaImage(captchaText);
const captchaImageElement = document.getElementById('captcha-image');
captchaImageElement.innerHTML = `<img src="${captchaImage}" alt="${captchaText}">`;
}
function checkCaptcha() {
const userInput = document.getElementById('userInput').value;
const captchaText = document.querySelector('#captcha-image img').alt;
if (userInput.toLowerCase() === captchaText.toLowerCase()) {
const verificationStatus = document.getElementById('verificationStatus');
verificationStatus.textContent = 'CAPTCHA doğru!';
verificationStatus.style.color = 'green';
} else {
const verificationStatus = document.getElementById('verificationStatus');
verificationStatus.textContent = 'CAPTCHA yanlış! Lütfen tekrar deneyin.';
verificationStatus.style.color = 'red';
}
updateCaptcha();
}
updateCaptcha();