数字暗号版

自动识别验证码并填充“数字暗号”输入框

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

作者
Ruclong
今日安裝
0
安裝總數
4
評價
0 0 0
版本
0.6
建立日期
2025-09-10
更新日期
2025-09-10
尺寸
3.2 KB
授權條款
MIT
腳本執行於
所有網站

// ==UserScript==
// @name 数字暗号版
// @namespace http://tampermonkey.net/
// @version 0.6
// @description 自动识别验证码并填充“数字暗号”输入框
// @match *://*/*
// @grant GM_xmlhttpRequest
// @license MIT
// ==/UserScript==

(function () {
'use strict';

const ocrApi = "http://101.126.130.186:5001/predict";

// 延迟函数
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

// 找验证码图片
function findCaptchaElement() {
return document.querySelector(".login-code img");
}

// 找输入框
function findInputBox() {
return document.querySelector("input[placeholder='数字暗号']");
}

// OCR识别并填充
function recognizeFromImg(imgElement) {
if (!imgElement) return;

let imgBase64 = "";
if (imgElement.src.startsWith("data:image")) {
imgBase64 = imgElement.src.split(",")[1];
} else {
console.warn("当前图片不是base64格式");
return;
}

GM_xmlhttpRequest({
method: "POST",
url: ocrApi,
headers: { "Content-Type": "application/json" },
data: JSON.stringify({ image: imgBase64 }),
onload: function (resp) {
try {
const result = JSON.parse(resp.responseText);
console.log("OCR返回:", result);

if (result.success && result.data) {
let inputBox = findInputBox();
if (inputBox) {
inputBox.value = result.data;
inputBox.dispatchEvent(new Event("input", { bubbles: true }));
console.log("验证码已填充:", result.data);
} else {
console.warn("未找到输入框");
}
} else {
console.warn("识别失败:", result.msg);
}
} catch (e) {
console.error("解析OCR结果失败:", e);
}
},
onerror: function (err) {
console.error("OCR请求出错:", err);
}
});
}

// 主逻辑:等待验证码图片出现
async function run() {
for (let i = 0; i < 20; i++) {
const captchaImg = findCaptchaElement();
if (captchaImg) {
console.log("找到验证码图片,开始识别...");
recognizeFromImg(captchaImg);

// 监听图片src变化(刷新验证码时自动识别)
const observer = new MutationObserver(() => {
console.log("验证码刷新,重新识别...");
recognizeFromImg(captchaImg);
});
observer.observe(captchaImg, { attributes: true, attributeFilter: ["src"] });
return;
}
await sleep(500);
}
console.warn("超时:未找到验证码图片");
}

window.addEventListener("load", run);
})();