-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfCaptcha.js
More file actions
211 lines (115 loc) · 4.18 KB
/
fCaptcha.js
File metadata and controls
211 lines (115 loc) · 4.18 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function textfCaptcha(elemSEL, onSuccess, onSubmit = null, onChange = null, onError = null, caseSensitive = true, mess = "Enter the text you see", pos = "left") {
if (mess == null) mess = "Enter the text you see"
if (pos == null) pos = "left"
function uuid(len) {
const chars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
let res = "";
for (let i = 0; i < len; i++) {
let indexChar = Math.floor(Math.random() * chars.length);
res += chars[indexChar];
}
return res;
}
const width = 500;
const height = 150;
function drawText(g2d) {
g2d.clearRect(0, 0, width, height);
function randomColor() {
let options = "abcdef0123456789";
let newStr = "#";
for (let i = 0; i < 6; i++) {
newStr += options[Math.round(Math.random() * (options.length - 1))]
}
return newStr;
}
g2d.fillStyle = randomColor()
g2d.fillRect(0, 0, 500, 150)
for (let i = 0; i < 175; i++) {
g2d.fillStyle = randomColor()
let pos = {
x: Math.round(Math.random() * width),
y: Math.round(Math.random() * height),
width: Math.floor(Math.random() * (100 + 20)),
height: Math.floor(Math.random() * (30 + 20))
}
g2d.fillRect(pos.x, pos.y, pos.width, pos.height)
}
let text = uuid(8);
g2d.font = "25px 'Times New Roman', Times, serif"
g2d.fillStyle = "#ffffff"
let sx = 35;
let sy = 75;
for (let i = 0; i < text.length; i++) {
if (Math.random() > 0.5) {
g2d.fillStyle = "#ffffff"
} else {
g2d.fillStyle = "#000000"
}
g2d.fillText(text[i], sx, sy);
sx += 50
sy -= 3
}
return text;
}
let elem = $(elemSEL)
for (let i = 0; i < elem.length; i++) {
let curr = $(elem[i]);
if (pos == "left") {
curr.addClass("fCaptcha left")
}
if (pos == "right") {
curr.addClass("fCaptcha right")
}
if (pos == "center") {
curr.addClass("fCaptcha center")
}
if (pos == "justify") {
curr.addClass("fCaptcha justify")
}
let canvas = $("<canvas></canvas>");
let title = $("<h1></h1>");
let input = $("<input></input>");
let submitBtn = $("<button></button>");
let remakeSpBt = $("<span></span>")
title.text(mess)
canvas[0].width = 500
canvas[0].height = 150
submitBtn.text("Verify")
remakeSpBt.text("Regenerate")
let txt = drawText(canvas[0].getContext("2d"));
title.addClass("fCaptcha title")
canvas.addClass("fCaptcha canvas")
input.addClass("fCaptcha input")
submitBtn.addClass("fCaptcha btn")
remakeSpBt.addClass("fCaptcha span")
remakeSpBt.on("click", function () {
txt = drawText(canvas[0].getContext("2d"));
if (onChange != null) onChange()
})
submitBtn.on("click", function () {
let inTxt = input.val();
if (onSubmit != null) onSubmit()
let ttxt = txt;
if (!caseSensitive) {
ttxt = txt.toLowerCase();
inTxt = inTxt.toLowerCase()
}
if (inTxt == ttxt) {
onSuccess()
} else {
if (onError != null) onError()
txt = drawText(canvas[0].getContext("2d"));
if (onChange != null) onChange()
input.val("")
}
})
curr.append(title)
curr.append($("<br>"))
curr.append(canvas);
curr.append($("<br>"))
curr.append($("<br>"))
curr.append(input)
curr.append(submitBtn)
curr.append(remakeSpBt)
}
}