-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.user.js
More file actions
267 lines (248 loc) · 9 KB
/
Copy pathscript.user.js
File metadata and controls
267 lines (248 loc) · 9 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// ==UserScript==
// @name 我的小学数学非常好
// @namespace https://www.fzu.edu.cn/
// @version 0.3
// @description 自动识别fzu教务处的验证码(本地)并填充
// @author 柠檬味氨水, Cai
// @match https://jwcjwxt2.fzu.edu.cn:82/login.htm
// @grant GM_xmlhttpRequest
// @updateURL https://github.com/ACaiCat/mastering-primary-math/raw/refs/heads/main/script.user.js
// @downloadURL https://github.com/ACaiCat/mastering-primary-math/raw/refs/heads/main/script.user.js
// ==/UserScript==
(function () {
'use strict';
// 数字模板(0-8)
const captureTemplate = {
0: [
[255, 0, 0, 0, 0, 255],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[0, 255, 0, 0, 255, 0],
[0, 255, 0, 0, 255, 0],
[0, 255, 0, 0, 255, 0],
[0, 255, 0, 0, 255, 0],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[255, 0, 0, 0, 0, 255],
],
1: [
[255, 255, 0, 255, 255, 255],
[0, 0, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[0, 0, 0, 0, 0, 255],
],
2: [
[255, 0, 0, 0, 0, 255],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[255, 255, 255, 255, 255, 0],
[255, 255, 255, 255, 0, 255],
[255, 255, 255, 0, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 0, 255, 255, 255, 255],
[0, 255, 255, 255, 255, 0],
[0, 0, 0, 0, 0, 0],
],
3: [
[255, 0, 0, 0, 0, 255],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[255, 255, 255, 255, 0, 255],
[255, 255, 0, 0, 255, 255],
[255, 255, 255, 255, 0, 255],
[255, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[255, 0, 0, 0, 0, 255],
],
4: [
[255, 255, 255, 0, 255, 255],
[255, 255, 255, 0, 255, 255],
[255, 255, 0, 0, 255, 255],
[255, 0, 255, 0, 255, 255],
[0, 255, 255, 0, 255, 255],
[0, 255, 255, 0, 255, 255],
[0, 0, 0, 0, 0, 0],
[255, 255, 255, 0, 255, 255],
[255, 255, 255, 0, 255, 255],
[255, 255, 0, 0, 0, 0],
],
5: [
[0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 255, 255],
[0, 255, 255, 255, 255, 255],
[0, 255, 0, 0, 0, 255],
[0, 0, 255, 255, 255, 0],
[255, 255, 255, 255, 255, 0],
[255, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[255, 0, 0, 0, 0, 255],
],
6: [
[255, 255, 0, 0, 0, 255],
[255, 0, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 255],
[0, 255, 255, 255, 255, 255],
[0, 255, 0, 0, 0, 255],
[0, 0, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[255, 0, 0, 0, 0, 255],
],
7: [
[0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 0, 255],
[0, 255, 255, 255, 0, 255],
[255, 255, 255, 0, 255, 255],
[255, 255, 255, 0, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
[255, 255, 0, 255, 255, 255],
],
8: [
[255, 0, 0, 0, 0, 255],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[255, 0, 0, 0, 0, 255],
[255, 0, 255, 255, 0, 255],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 0],
[255, 0, 0, 0, 0, 255],
],
};
// 计算灰度,二值化处理图像
function preprocessImage(imageData) {
const data = imageData.data;
const width = imageData.width;
const height = imageData.height;
const binary = [];
for (let y = 0; y < height; y++) {
const row = [];
for (let x = 0; x < width; x++) {
const idx = (y * width + x) * 4;
const r = data[idx];
const g = data[idx + 1];
const b = data[idx + 2];
// 灰度化(转为0-255的灰度值)
const gray = 0.299 * r + 0.587 * g + 0.114 * b;
// 二值化(转为0或1)
// 128是阈值,可以根据实际情况调整
const binaryValue = gray < 128 ? 0 : 1;
row.push(binaryValue);
}
binary.push(row);
}
return binary;
}
// 相似度计算函数
function compare(array1, array2) {
if (!array1 || !array2 || array1.length !== array2.length || array1[0].length !== array2[0].length) {
return 0;
}
const rows = array1.length;
const cols = array1[0].length;
let same = 0;
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
if (array1[y][x] === array2[y][x]) same++;
}
}
return same / (rows * cols);
}
// 识别单个数字(6x10 图像)
function recognizeSingleDigit(imageData) {
// imageData 是 ImageData 对象(来自 canvas)
const data = imageData.data;
const width = imageData.width; // 应为 6
const height = imageData.height; // 应为 10
// 与模板比对
let bestMatch = 0;
let maxSim = -1;
for (let digit = 0; digit <= 8; digit++) {
const sim = compare( preprocessImage(imageData), captureTemplate[digit]);
if (sim > maxSim) {
maxSim = sim;
bestMatch = digit;
}
}
return bestMatch;
}
// 主识别函数:解析整个验证码图
function recognizeCaptcha(imgElement) {
return new Promise((resolve) => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = imgElement;
// 验证码图尺寸应为 50x10(根据裁剪坐标推断)
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
// 裁剪四个数字区域(x, y, w=6, h=10)
const regions = [
{ x: 2, y: 0 },
{ x: 12, y: 0 },
{ x: 32, y: 0 },
{ x: 42, y: 0 }
];
const digits = regions.map(region => {
const digitCanvas = document.createElement('canvas');
digitCanvas.width = 6;
digitCanvas.height = 10;
const digitCtx = digitCanvas.getContext('2d');
digitCtx.drawImage(canvas, region.x, region.y, 6, 10, 0, 0, 6, 10);
const imageData = digitCtx.getImageData(0, 0, 6, 10);
return recognizeSingleDigit(imageData);
});
console.log('🤔 识别结果:',digits );
const num1 = digits[0] * 10 + digits[1];
const num2 = digits[2] * 10 + digits[3];
resolve(num1 + num2);
});
}
// 主流程
async function processCaptcha() {
try {
const result = await recognizeCaptcha(captchaImage);
captchaInput.value = result.toString();
console.log('✅ 本地识别成功,结果:', result);
} catch (e) {
console.error('❌ 识别失败:', e);
}
}
const captchaImage = document.getElementById('yzm_pic');
const captchaInput = document.getElementById('Verifycode');
if (captchaImage && captchaInput) {
// 等待图片加载完成
if (captchaImage.complete && captchaImage.naturalHeight !== 0) {
processCaptcha();
} else {
captchaImage.onload = processCaptcha;
}
// 监听验证码刷新
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'attributes' && mutation.attributeName === 'src') {
captchaImage.onload = processCaptcha;
}
});
});
observer.observe(captchaImage, {
attributes: true,
attributeFilter: ['src']
});
} else {
console.error('❌ 未找到验证码元素');
}
})();