// ==UserScript== // @name DDDD OCR 浏览器验证码自动填充|纠错+自动登录 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 本地ddddocr后端识别验证码,Z转2、J转1、O转0,识别填充后自动登录。⚠️需要本地启动8000端口python OCR服务! // @author ShenZhen // @match https://换成自己的浏览器域名/* // @grant GM_xmlhttpRequest // @license MIT // ==/UserScript== (function() { 'use strict'; const API = "http://127.0.0.1:8000/ocr"; let lastImgSrc = ""; let isRequesting = false; // 字符纠错映射 function fixText(raw) { let s = raw.trim(); s = s.replace(/[JjLl]/g, '1'); s = s.replace(/[Zz]/g, '2'); s = s.replace(/[Oo]/g, '0'); // 最后只保留0-9数字 s = s.replace(/[^0-9]/g, ''); return s; } async function runOcr() { const captchaInput = document.querySelector("input#captcha"); const captchaImg = document.querySelector("input#captcha + img"); // 登录按钮选择器 const loginBtn = document.querySelector("button[type='submit']"); console.log("检测验证码"); if (!captchaImg || !captchaInput || !loginBtn) { return; } if (captchaImg.src === lastImgSrc) { return; } if (isRequesting) { return; } lastImgSrc = captchaImg.src; isRequesting = true; await new Promise(resolve => { if (captchaImg.complete) resolve(); else captchaImg.onload = resolve; }); const canvas = document.createElement("canvas"); const ctx = canvas.getContext('2d'); canvas.width = captchaImg.naturalWidth; canvas.height = captchaImg.naturalHeight; ctx.drawImage(captchaImg, 0, 0); canvas.toBlob(blob => { const fd = new FormData(); fd.append("file", blob, "cap.png"); GM_xmlhttpRequest({ method: "POST", url: API, data: fd, onload: res => { try { const ret = JSON.parse(res.responseText); console.log("原始识别结果:", ret.text); const code = fixText(ret.text); console.log("纠错后结果:", code); captchaInput.value = code; captchaInput.dispatchEvent(new Event("input", { bubbles: true })); captchaInput.dispatchEvent(new Event("change", { bubbles: true })); captchaInput.dispatchEvent(new Event("blur", { bubbles: true })); // ✅ 关键:只有正好4位数字,才自动点登录 if(code.length === 4){ console.log("验证码4位正确,自动点击登录"); setTimeout(()=>{ loginBtn.click(); }, 200); }else{ console.log("识别结果不足4位,不自动登录,等待刷新重试"); } } catch (e) { console.error("识别出错:", e); } isRequesting = false; }, onerror: e => { console.error("请求失败:", e); isRequesting = false; } }) }, "image/png"); } function addBtn() { if (document.getElementById("ocrBtn")) return; const btn = document.createElement("button"); btn.id = "ocrBtn"; btn.innerText = "识别验证码"; btn.style.position = "fixed"; btn.style.bottom = "20px"; btn.style.right = "20px"; btn.style.zIndex = "999999"; btn.onclick = runOcr; document.body.appendChild(btn); } function watchImgLoad() { const captchaImg = document.querySelector("input#captcha + img"); if (!captchaImg) return; captchaImg.onload = () => { console.log("验证码图片刷新,立即识别"); setTimeout(runOcr, 100); }; } let timer = setInterval(() => { addBtn(); runOcr(); watchImgLoad(); }, 800); })();