-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha.userscript.js
178 lines (143 loc) · 6.77 KB
/
captcha.userscript.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
165
166
167
168
169
170
171
172
173
174
175
176
177
// ==UserScript==
// @name Captcha Recognizer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Recognize captcha on the page using template matching
// @author You
// @match https://cpabm.cpami.gov.tw/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
const digitTemplatesCompressed = [[0,0,1,1,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1,1,0,0,0],[0,1,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,1,0],[0,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0],[0,0,1,1,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,0,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,1,0,0],[0,0,1,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,1,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,1,1,0,0]];
const digitTemplates = digitTemplatesCompressed.map((template) => {
// from b/w to rbga
const rawBits = []
for (let i = 0; i < template.length; i++) {
const bit = template[i];
if (bit === 1) {
rawBits.push(0, 0, 0, 255);
} else {
rawBits.push(255, 255, 255, 255);
}
}
return rawBits;
});
function imageToCanvas(img) {
const canvas = document.createElement('canvas');
// Fixed size for the captcha image
canvas.width = 60;
canvas.height = 20;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
return canvas;
}
function mse(image, array2) {
const { data, width, height } = image
let sum = 0
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const idx = (y * width + x) * 4;
const diff = data[idx] - array2[idx];
sum += diff * diff;
}
}
return sum / (width * height);
}
function recognizeDigit(digitImage) {
let minEMSE = Number.MAX_VALUE;
let recognizedDigit = -1;
for (let i = 0; i < digitTemplates.length; i++) {
const error = mse(digitImage, digitTemplates[i]);
if (error < minEMSE) {
minEMSE = error;
recognizedDigit = i;
}
}
return recognizedDigit;
}
function processAndSplitDigits(canvas) {
const ctx = canvas.getContext('2d');
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
// Convert to grayscale and apply threshold filter
const threshold = 128;
for (let i = 0; i < data.length; i += 4) {
const grayscale = 0.3 * data[i] + 0.59 * data[i + 1] + 0.11 * data[i + 2];
const value = grayscale > threshold ? 255 : 0;
data[i] = data[i + 1] = data[i + 2] = value;
}
ctx.putImageData(imageData, 0, 0);
// Detect and extract individual digits
const digitWidth = canvas.width / 4;
const digitHeight = canvas.height;
const digitImages = [];
for (let i = 0; i < 4; i++) {
const digitCanvas = document.createElement('canvas');
const digitCtx = digitCanvas.getContext('2d');
digitCanvas.width = digitWidth;
digitCanvas.height = digitHeight;
digitCtx.drawImage(canvas, i * digitWidth, 0, digitWidth, digitHeight, 0, 0, digitWidth, digitHeight);
// Find edges by scanning rows and columns
let left = digitWidth;
let right = 0;
let top = digitHeight;
let bottom = 0;
const digitImageData = digitCtx.getImageData(0, 0, digitWidth, digitHeight);
const digitData = digitImageData.data;
for (let y = 0; y < digitHeight; y++) {
for (let x = 0; x < digitWidth; x++) {
const idx = (y * digitWidth + x) * 4;
const value = digitData[idx];
if (value === 0) {
left = Math.min(left, x);
right = Math.max(right, x);
top = Math.min(top, y);
bottom = Math.max(bottom, y);
}
}
}
// Crop the digit based on the detected edges
const croppedWidth = right - left + 1;
const croppedHeight = bottom - top + 1;
const croppedCanvas = document.createElement('canvas');
const croppedCtx = croppedCanvas.getContext('2d');
croppedCanvas.width = croppedWidth;
croppedCanvas.height = croppedHeight;
croppedCtx.drawImage(digitCanvas, left, top, croppedWidth, croppedHeight, 0, 0, croppedWidth, croppedHeight);
// get image Data from cropped canvas
const croppedImageData = croppedCtx.getImageData(0, 0, croppedWidth, croppedHeight);
digitImages.push(croppedImageData);
// Debug: draw the cropped image somewhere on the page
document.body.appendChild(croppedCanvas);
// set the position of the cropped canvas
croppedCanvas.style.position = 'absolute';
croppedCanvas.style.top = '0';
croppedCanvas.style.left = `${i * 100}px`;
}
return digitImages;
}
function recognizeCaptcha() {
const inputElement = document.querySelector('input[name="insrand"]');
const img = inputElement.nextElementSibling;
if (!img) {
console.error('Captcha image not found');
return;
}
const canvas = imageToCanvas(img);
const digitImages = processAndSplitDigits(canvas);
let captchaText = '';
for (let i = 0; i < 4; i++) {
const digitImage = digitImages[i];
const recognizedDigit = recognizeDigit(digitImage);
captchaText += recognizedDigit;
}
console.log(`Recognized captcha: ${captchaText}`);
// auto-fill the captcha input
inputElement.value = captchaText;
}
// Wait for the image to load before recognizing the captcha
window.addEventListener('load', () => {
setTimeout(recognizeCaptcha, 300);
});
})();