forked from cbwang2016/PKUGetClassHelper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha.js
68 lines (66 loc) · 2.31 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
window.Tesseract = Tesseract.create({
workerPath: chrome.extension.getURL('libs/tesseract/worker.js'),
corePath: chrome.extension.getURL('libs/tesseract/index-core.js'),
});
function detectCaptcha() {
var c = document.getElementById('canv');
var ctx = c.getContext("2d");
var img = document.getElementById('imgname');
ctx.drawImage(img, 0, 0);
var imgData = ctx.getImageData(0, 0, c.width, c.height);
var res = [];
for (var i = 0; i < 58; i++) {
res[i] = [];
for (var j = 0; j < 22; j++) {
var k = imgData.data[j * 58 * 4 + i * 4];
if (k > 128) {
res[i][j] = 0;
} else {
res[i][j] = 1;
}
}
}
var morphResult = [];
for (var i = 0; i < 58; i++) {
morphResult[i] = [];
for (var j = 0; j < 22; j++) {
if (i == 0 || j == 0) {
morphResult[i][j] = 0;
} else if (res[i - 1][j - 1] + res[i - 1][j] + res[i][j - 1] + res[i][j] <= 2) {
morphResult[i][j] = 0;
} else {
morphResult[i][j] = 1;
}
}
}
morph.drawImage(morphResult, c);
Tesseract.recognize(c, {
lang: 'eng',
tessedit_char_whitelist: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
tessedit_enable_doc_dict: 0,
language_model_penalty_non_dict_word: 0.0,
language_model_penalty_non_freq_dict_word: 0.0,
language_model_penalty_case: 0.0,
language_model_penalty_chartype: 0.0,
language_model_penalty_spacing: 0.0,
segment_penalty_dict_case_bad: 1.0,
segment_penalty_dict_case_ok: 1.0,
segment_penalty_dict_nonword: 1.0,
segment_penalty_garbage: 1.0,
segment_reward_chartype: 1.0,
}).then(function (result) {
const code = result.text.replace(/[^a-z0-9A-Z]/g, '')
if (code.length != 4) {
eventHandler.detectCaptchaError();
} else {
eventHandler.validatePass = function () {
$('#validCode').val(code);
eventHandler.detectCaptchaSuccess();
}
eventHandler.validateNotPass = function (message) {
eventHandler.detectCaptchaError();
}
validate(code);
}
})
}