-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptchaGenerator.js
More file actions
190 lines (152 loc) · 5.71 KB
/
captchaGenerator.js
File metadata and controls
190 lines (152 loc) · 5.71 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
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
'use strict';
let string = undefined;
let canvas = undefined;
init();
function init() {
let captcha = document.getElementsByTagName('captcha')[0];
if (captcha !== undefined) {
let form = createForms(captcha);
for (let i = 0; i < form.childNodes.length; i++) {
if (form.childNodes[i].tagName.toUpperCase() === 'CANVAS')
canvas = form.childNodes[i];
}
if (canvas !== undefined) {
drawCanvas(canvas);
string = createRandomString();
drawStringOnCanvas(canvas, string);
captcha.appendChild(form);
}
}
}
function createRandomString() {
const allowedChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let strength = 8;
let randomString = '';
for (let i = 0; i < strength; i++) {
randomString += allowedChars[randomInt(0, 25)]
}
return randomString;
}
function createForms() {
let parent = document.createElement('div');
parent.style.display = 'block';
parent.style.width = '200px';
parent.style.maxHeight = '175px';
parent.style.font = '12px Helvetica';
parent.style.border = '2px solid #d3d3d3';
parent.style.borderRadius = '3px';
parent.style.padding = '10px';
let canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 50;
canvas.style.display = 'block';
let text = document.createElement('p');
text.innerHTML = 'Type the text above: ';
let notification = document.createElement('P');
notification.id = 'notification';
notification.style.display = 'none';
let inputParent = document.createElement('div');
inputParent.style.display = 'flex';
let input = document.createElement('input');
input.type = 'text';
input.id = 'input';
input.style.width = '145px';
input.style.padding = '5px 10px';
let refreshIcon = document.createElement('img');
refreshIcon.src = 'https://icon-library.net/images/refresh-icon-png/refresh-icon-png-17.jpg';
refreshIcon.id = 'refresh';
refreshIcon.alt = 'Refresh captcha';
refreshIcon.style.width = '28px';
refreshIcon.style.height = '28px';
refreshIcon.style.paddingLeft = '5px';
refreshIcon.addEventListener('click', refresh, false);
inputParent.appendChild(input);
inputParent.appendChild(refreshIcon);
let submit = document.createElement('input');
submit.type = 'submit';
submit.id = 'submit';
submit.value = 'Submit';
submit.style.width = '75px';
submit.style.height = '25px';
submit.style.marginTop = '10px';
submit.style.backgroundColor = '#287bff';
submit.style.color = '#ffffff';
submit.style.border = 'none';
submit.style.borderRadius = '3px';
submit.addEventListener('click', validate, false);
parent.appendChild(canvas);
parent.appendChild(text);
parent.appendChild(notification);
parent.appendChild(inputParent);
parent.appendChild(submit);
return parent;
}
function drawCanvas(canvas) {
let ctx = canvas.getContext('2d');
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height);
const red = randomInt(125, 175);
const green = randomInt(125, 175);
const blue = randomInt(125, 175);
ctx.fillStyle = createRgb(red, green, blue);
ctx.fillRect(0, 0, 200, 50);
let colours = [];
for (let i = 0; i < 5; i++) {
colours.push(createRgb(red - (20*i),green - (20*i),blue - (20*i)));
}
for (let i = 0; i < 20; i++) {
ctx.fillStyle = colours[randomInt(0, 4)];
const x = randomInt(-10, 190);
const y = randomInt(-10, 10);
ctx.fillRect(x, y, randomInt(0, (210 - x)), randomInt(0, (60 - y)));
}
return canvas;
}
function drawStringOnCanvas(canvas, string) {
let ctx = canvas.getContext('2d');
const black = createRgb( 0, 0, 0);
const white = createRgb(255, 255, 255);
const textColours = [black, white];
const textFonts = ['Arial', 'Georgia', 'Helvetica', 'Impact'];
for (let i = 0; i < string.length; i++) {
const letterSpace = 170/string.length;
const initial = 15;
ctx.font = `20px ${textFonts[randomInt(0, textFonts.length - 1)]}`;
ctx.fillStyle = textColours[randomInt(0, 1)];
ctx.rotate(randomInt(-5, 5) * Math.PI / 180);
ctx.fillText(string[i], initial + (i*letterSpace), randomInt(20, 40), 200);
}
}
function refresh() {
drawCanvas(canvas);
string = createRandomString();
drawStringOnCanvas(canvas, string);
document.getElementById('input').value = '';
document.getElementById('notification').style.display = 'none';
}
function validate() {
if (document.getElementById('input').value === string) {
const date = new Date();
date.setTime(date.getTime() + (60*1000));
document.cookie = `captcha=true;expires=${date.toUTCString()};path=/`;
let notification = document.getElementById('notification');
notification.innerHTML = 'Success';
notification.style.color = '#00ff7f';
notification.style.display = 'block';
document.getElementById('input').disabled = 'true';
document.getElementById('refresh').removeEventListener('click', refresh, false);
document.getElementById('submit').disabled = 'true';
document.getElementById('submit').removeEventListener('click', validate, false);
} else {
let notification = document.getElementById('notification');
notification.innerHTML = 'Incorrect text';
notification.style.color = '#ff0000';
notification.style.display = 'block';
}
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function createRgb(red, green, blue) {
return `rgb(${red}, ${green}, ${blue})`;
}