-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload_Captcha_svHaUI.user.js
More file actions
140 lines (124 loc) · 5.44 KB
/
Copy pathDownload_Captcha_svHaUI.user.js
File metadata and controls
140 lines (124 loc) · 5.44 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
// ==UserScript==
// @name Download captcha svHaUI
// @namespace https://github.com/vuquan2005/ScriptsMonkey
// @version 4.2
// @description Tự động tải captcha của svHaUI
// @match https://sv.haui.edu.vn/*
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
(function () {
"use strict";
// ==================================================
const $ = (selector) => document.querySelector(selector);
// 1. Gộp logic xử lý Captcha tại trang đăng nhập/đăng ký
async function setupCaptchaObserver(inputSelector, imgSelector, isFarmMode = false) {
const captchaInput = $(inputSelector);
const captchaImg = $(imgSelector);
if (!captchaInput || !captchaImg) return;
// Kiểm tra: Nếu đã submit captcha trước đó mà vẫn ở trang login => Captcha SAI
const isLoginPassed = await GM_getValue("isLoginPassed", false);
if (isLoginPassed) {
const img64 = await GM_getValue("captchaImg", "");
const label = await GM_getValue("captchaLabel", "");
downloadCaptcha(img64, label, true); // true = wrong captcha
}
// Chờ ảnh load xong mới lấy Base64 thay vì setTimeout 2s
captchaImg.onload = () => {
const base64 = convertImageToBase64(captchaImg);
GM_setValue("captchaImg", base64);
};
// Nếu ảnh đã load từ trước (cache)
if (captchaImg.complete) {
GM_setValue("captchaImg", convertImageToBase64(captchaImg));
}
let captchaLabel = "";
captchaInput.addEventListener("input", (e) => {
captchaLabel = e.target.value;
});
window.addEventListener("beforeunload", () => {
GM_setValue("lastURL", window.location.href);
GM_setValue("isLoginPassed", true);
GM_setValue("isFarmMode", isFarmMode);
if (captchaLabel.length === 5) {
GM_setValue("captchaLabel", captchaLabel);
}
});
}
// 2. Tối ưu hàm download: Thêm kiểm tra dữ liệu
function downloadCaptcha(base64, label, isWrong = false) {
if (!base64 || !label) return;
const prefix = isWrong ? " WRONG " : "";
const a = document.createElement("a");
a.href = base64;
a.download = `captcha_svHaUI__${prefix}${label}.png`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
// Reset lại trạng thái
GM_setValue("isLoginPassed", false);
GM_setValue("captchaLabel", "");
}
// 3. Chuyển Canvas sang Base64 chuẩn hơn
function convertImageToBase64(img) {
const canvas = document.createElement("canvas");
canvas.width = img.naturalWidth || img.width;
canvas.height = img.naturalHeight || img.height;
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
return canvas.toDataURL("image/png");
}
// 4. Hàm điều phối chính (Main Run)
async function run() {
const currentURL = window.location.href;
const lastURL = await GM_getValue("lastURL", "");
const isLoginPassed = await GM_getValue("isLoginPassed", false);
// TRƯỜNG HỢP 1: Trang chỉ có ảnh captcha (Trang lấy mẫu)
// if (currentURL.includes("AImages.aspx")) {
// const image = $("img");
// if (image) {
// const base64 = convertImageToBase64(image);
// let label = prompt("Nhập label captcha (5 ký tự):", "");
// if (label && label.length === 5) {
// downloadCaptcha(base64, label);
// setTimeout(() => location.reload(), 500);
// } else {
// // location.reload();
// }
// }
// }
// TRƯỜNG HỢP 2: Trang đăng nhập SSO (có token)
if (currentURL.includes("sso?token=")) {
setupCaptchaObserver("input#ctl00_txtimgcode", "img#ctl00_Image1");
}
// TRƯỜNG HỢP 2b: Trang đăng nhập SSO (farm mode - không có token, chỉ /sso)
else if (/\/sso\/?$/.test(window.location.pathname)) {
setupCaptchaObserver("input#ctl00_txtimgcode", "img#ctl00_Image1", true);
}
// TRƯỜNG HỢP 3: Trang đăng ký học phần
else if (currentURL.includes("register/")) {
if (isLoginPassed && $("div#tab0_data")) {
const img64 = await GM_getValue("captchaImg", "");
const label = await GM_getValue("captchaLabel", "");
downloadCaptcha(img64, label);
} else {
setupCaptchaObserver("input#ctl02_txtimgcode", "img#ctl02_Image1");
}
}
// TRƯỜNG HỢP 4: Quay lại trang chủ sau khi login thành công
else if ($("span.user-name") && isLoginPassed) {
const img64 = await GM_getValue("captchaImg", "");
const label = await GM_getValue("captchaLabel", "");
const isFarmMode = await GM_getValue("isFarmMode", false);
downloadCaptcha(img64, label);
if (isFarmMode && label) {
GM_setValue("isFarmMode", false);
setTimeout(() => {
window.location.href = "/sso";
}, 10);
}
}
}
run();
setTimeout(run, 500);
})();