油猴验证码自动填写(包括用户名和密码)+Django

油猴自动填写验证码,包括用户名和密码

环境

  • Mac
  • Django
  • Python3.8.10
  • Tampermonkey

油猴脚本内容

// ==UserScript==
// @name         一体化验证码脚本
// @namespace    http://8.219.170.236/
// @version      4.4
// @description  在将来的时间里将会在后台默默的为你自动识别页面是否存在验证码并填入。感谢哈士奇提供思路支持。
// @author       crab
// @match        http://10.44.1.41:8080/*
// @match        https://*/*
// @connect      8.219.170.236
// @connect      *
// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @require      http://ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min.js
// @resource     cktools https://greasyfork.org/scripts/429720-cktools/code/CKTools.js?version=1034581
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_listValues
// @grant        GM_openInTab
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_xmlhttpRequest
// @grant        GM_getResourceText
// @nocompat     Chrome
// @run-at      document-end
// ==/UserScript==

(function () {
  "use strict";
  let requestUrl = "http://10.44.97.228:800";
  addLoadEvent(testXpath);
  function testXpath() {
    getElementByXpath(
      "/html/body/div[1]/div[2]/div[2]/div[1]/div[3]/button[1]"
    ).onclick = function () {
      // 等待时间
      setTimeout(function () {
        log();
      }, 1000);
    };
    getElementByXpath(
      "/html/body/div[1]/div[1]/div[2]/div/div/form/div[3]/a/img"
    ).onclick = function () {
      // 等待时间
      setTimeout(function () {
        log(1);
      }, 1000);
    };
  }
  //运行函数
  function log(a = 0) {
    var imgData, response;
    console.log("开始了!");
    var element = getElementByXpath(
      "/html/body/div[1]/div[2]/div[2]/div[1]/div[2]/div[2]/span/select"
    ); //获取到select元素
    var index = element.selectedIndex; //获取元素下标
    if (a === 0) {
      var name = element.options[index].text;
    } else {
      var element = getElementByXpath(
        "/html/body/div[1]/div[1]/div[2]/div/div/div[1]/div[2]/button/text()"
      );
      var name = element.textContent
        .replace(/^\s\s*/, "")
        .replace(/\s\s*$/, "");
    }

    //定位图片,先等1秒

    var img_ele = getElementByXpath(
      "/html/body/div[1]/div[1]/div[2]/div/div/form/div[3]/a/img"
    );
    console.log(img_ele.src);
    //图片转换base64
    imgData = getCode(img_ele);
    console.log("图片长度:" + imgData.length);
    //发送请求
    response = reqobj(name, imgData);
  }
  //文档加载函数
  function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != "function") {
      window.onload = func;
    } else {
      window.onload = function () {
        oldonload();
        func();
      };
    }
  }
  //封装xpath
  function getElementByXpath(xpath) {
    var element = document.evaluate(xpath, document).iterateNext();
    return element;
  }
  function getCode(img) {
    var imgBase64;
    // console.log(img);
    imgBase64 = ConversionBase(img).toDataURL("image/png");
    var pastDate =
      "img=" + encodeURIComponent(imgBase64.replace(/.*,/, "").trim());
    return pastDate;
  }
  function ConversionBase(img) {
    // img = document.querySelector(img);
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0, img.width, img.height);
    // console.log(canvas)
    return canvas;
  }
  //填写内容
  function write(data) {
    var selector;
    console.log("开始填写验证码");
    selector = document.querySelector.bind(document);
    let uname = selector(
      "div#login_main > div > form > div:nth-of-type(1) > div > div > input"
    );
    let upswd = selector(
      "div#login_main > div > form > div:nth-of-type(2) > div > div > input"
    );
    let yanzhengma = selector(
      "div#login_main > div > form > div:nth-of-type(3) > div > div > input"
    );
    uname.value = data.uname; // 输入的内容
    upswd.value = data.upswd; // 输入的内容
    yanzhengma.value = data.data; // 输入的内容
    var event = new Event("input", {
      bubbles: true,
      cancelable: true,
    });
    uname.dispatchEvent(event);
    upswd.dispatchEvent(event);
    yanzhengma.dispatchEvent(event);
  }
  function reqobj(name, img) {
    var url = requestUrl + "/code";
    var Results;
    console.log("开始识别验证码");
    GM_xmlhttpRequest({
      url: url,
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        path: window.location.href,
      },
      data: img + "&name=" + name,
      responseType: "json",
      onload: (obj) => {
        var data = obj.response;
        console.log(data);

        if (data.code === 200) {
          console.log("请求成功!");
          write(data);
        } else {
          console.log("请求错误!");
        }
      },
      onerror: (err) => {
        console.log(err);
      },
    });
    console.log(Results);
    return Results;
  }
})();

django内容

# views.py
import ddddocr
from django.http import JsonResponse
def code(request):

    def dddd_ocr(img):
        ocr = ddddocr.DdddOcr(old=True, show_ad=False)

        res = ocr.classification(img)
        return res

    try:
        img = request.POST['img']
        name = request.POST['name'].replace(' ', '')
        # uname = FromData.objects.get(xian=name)
        data = {
            "code": 200,
            "data": dddd_ocr(img),
            # "uname": uname.uname,
            # "upswd": uname.upswd,
            "msg": "成功!",
        }
    except FromData.DoesNotExist:
        data = {"code": 201}
        return JsonResponse(data, safe=False)

    return JsonResponse(data, safe=False)

Gitee

https://gitee.com/hayratjan/

Github

https://github.com/hayratjan/

赞赏

赞赏

你可能感兴趣的:(django,javascript,python)