油猴脚本+python:验证码识别辅助器


脚本信息

  • 描述:当鼠标放在验证码图片上时,显示弹窗并提供识别选项

实现逻辑

  1. 定义了一个isRectangle函数,用于判断图片是否符合验证码的特征。判断条件:请根据自己需求修改
  2. 定义了一个showPopup函数,用于显示弹窗。该函数接收一个图片的URL作为参数。
    • 创建一个固定定位的div元素作为弹窗。
    • 设置弹窗的样式,包括位置、背景颜色、内边距、边框等。
    • 创建一个img元素,设置其src属性为传入的图片URL,并设置样式。
    • 创建一个识别按钮和一个取消按钮,并设置对应的文本和样式。
    • 识别按钮添加点击事件监听器,在点击时调用recognizeCaptcha函数进行验证码识别,并通过alert弹窗显示识别结果,最后移除弹窗。
    • 取消按钮添加点击事件监听器,在点击时移除弹窗。
    • img识别按钮和取消按钮添加到弹窗中。
    • 将弹窗添加到body元素中。
    • document添加点击事件监听器,当点击事件发生时,判断点击的目标元素是否在弹窗内,如果不在则移除弹窗。
  3. 定义了一个recognizeCaptcha函数,用于调用验证码识别的API接口,并返回识别结果。目前该函数只是返回传入的图片URL。
  4. document添加mouseover事件监听器,当鼠标放在某个元素上时触发,判断目标元素是否为IMG标签且符合验证码特征,如果是则调用showPopup函数显示弹窗,否则打印"不符合验证码特征"。 以上是该验证码识别脚本的实现逻辑,你可以根据自己的需求进行修改和优化。

     

python代码:
 

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import requests
import ddddocr
import base64
ocr = ddddocr.DdddOcr()
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
class Image(BaseModel):
    url: str
@app.post("/recognize/")
async def recognize_image(image: Image):
    try:
        image_data = base64.b64decode(image.url.split(",")[1])
        result = ocr.classification(image_data)
        print(result)
        return {"result": result}
    except Exception as e:
        print(e)
        return {"result": str(e)}
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

油猴脚本: 

// ==UserScript==
// @name         验证码识别脚本
// @namespace    your-namespace
// @version      1.0
// @description  当鼠标放在长方形验证码图片上时,显示弹窗并提供识别选项
// @author       your-name
// @match        *://*/*
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    var isRectangle = function(image) {
        console.log("比例: " + image.width / image.height);
        console.log("宽: " + image.width);
        console.log("高: " + image.height);
        return image.width / image.height > 1.5 && image.width / image.height < 3 && image.width >= 80 && image.height >= 30 && image.width <= 200 && image.height <= 100;
    };
    var showPopup = function(base64Value) {
        var popup = document.createElement('div');
        popup.style.position = 'fixed';
        popup.style.top = '50%';
        popup.style.left = '50%';
        popup.style.transform = 'translate(-50%, -50%)';
        popup.style.backgroundColor = 'white';
        popup.style.padding = '20px';
        popup.style.border = '1px solid black';
        var img = document.createElement('img');
        img.src = base64Value;
        img.style.display = 'block';
        img.style.marginBottom = '20px';
        var recognizeBtn = document.createElement('button');
        recognizeBtn.textContent = '识别';
        recognizeBtn.style.marginRight = '20px';
        var cancelBtn = document.createElement('button');
        cancelBtn.textContent = '取消';
        recognizeBtn.addEventListener('click', function() {
            var result = recognizeCaptcha(img.src, function(result) {
                console.log(result);
                alert("验证码识别结果: " + result.result + "(已复制到剪贴板)" );
                var tempInput = document.createElement('input');
                tempInput.value = result.result;
                document.body.appendChild(tempInput);
                tempInput.select();
                document.execCommand('copy');
                document.body.removeChild(tempInput);
                popup.remove();
            });
        });
        cancelBtn.addEventListener('click', function() {
            popup.remove();
        });
        popup.appendChild(img);
        popup.appendChild(recognizeBtn);
        popup.appendChild(cancelBtn);
        document.body.appendChild(popup);
        document.addEventListener('click', function(e) {
            if (!popup.contains(e.target)) {
                popup.remove();
            }
        });
    };
    var recognizeCaptcha = function(src, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://localhost:8000/recognize/", true);
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.onload = function() {
            if (xhr.status === 200) {
                callback(JSON.parse(xhr.responseText));
            } else {
                console.error("请求发生错误:", xhr.status);
            }
        };
        xhr.onerror = function() {
            console.error("请求发生错误:", xhr.status);
        };
        var data = JSON.stringify({ url: src });
        xhr.send(data);
    };
    document.addEventListener('mouseover', function(e) {
        var target = e.target;
        if (target.tagName === 'IMG' && isRectangle(target)) {
            var canvas = document.createElement('canvas');
            canvas.width = target.width;
            canvas.height = target.height;
            var ctx = canvas.getContext('2d');
            ctx.drawImage(target, 0, 0);
            var base64Value = canvas.toDataURL('image/png');
            console.log(base64Value);
            showPopup(base64Value);
        } else {
            console.log("不符合验证码特征");
        }
    });
})();


 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值