Greasy Fork is available in English.

jAccount 验证码在线 ResNet 高速高精度毫秒级识别

基于在线的 ResNet 神经网络实现,毫秒级高速高精度识别 jAccount 登录中的验证码

// ==UserScript==
// @name         jAccount 验证码在线 ResNet 高速高精度毫秒级识别
// @namespace    http://tampermonkey.net/
// @version      1.8
// @description  基于在线的 ResNet 神经网络实现,毫秒级高速高精度识别 jAccount 登录中的验证码
// @author       danyang685
// @match        https://jaccount.sjtu.edu.cn/jaccount/jalogin*
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAACPhJREFUWEfFl3mM1dUVx7/n3N/vzcIAI8x7Q2DezCBSVCLEMswMi7hFg7hExSVxqVZNWou1TZqa2jY10aRNGxNjtDYmtikVuxCtUWJjUYtxQWYcxaWWgbLMxgyzCgzM8t7vnm9zHx0LCMU2Tfr+/N3lfO653/O95wn+zz/5b+K3YFE8VnGoGBiZHJlpomUH8mXj+Qvb2sb+0/2+MMCWaWdMYTx2ljOtMaIcIomAk0g4KAZBiRV22CvbOIq/Lz3YNfRFYE4J8FbFvMkxxlYQnBsCqWdrAnajBEPjUcQQpCqK2Lt/fHpMPZOOs2AoUbGOZBzNpwI5KQABaanMzjcvyyjSqZBuo12lIuYNzy0ZbN929AmbKrJ1gNwA4DAEbRT2gDpXaE31A53vCVCAPf53QoAQvDmdXU7oAlH8qb23vaM2XXWJQecpuNUT5czxraNPtyVTs1rI2wCUC/DW4Un64KSDuTRdfCnI3rKByRvn45PcKQEKwTM1jYBVuny8qe7T3QfCoqaK6iu8WGt6anHn0P7RGnEua6SDoESp/TAfm7oKwBaC8lFjf8ezYV3LzJmllotuJniofqDzDwLY0RCfy0Dz9OxiqpxdrOPPLuztPTwx+Z0ZtbWa5+l0doGYLIDgbAJeAAGREOgR4SYBdlGi1oa+PR9OrN1cVVUSjblrKdbR0N/55kkBWmbOrPA5dyugO/IofqM4NezrurtHmtO1M8L9Q3GnAGUhxaQ0k9h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FhuF1UBNN+djPovGc4BeWxK82Du08eKLgpwSYWBSaUpKLKJZRkbyZ7HWC/VDL50EJWoG36RCrFA0pR48k/r3FQ91dJ/tDcsIqOBnlxPfQXrsxZoloFmGTnTAncEIy8tDhCNLjzfX8uxN/YQ2cCuZ/Nf4P8iTQXa0LxcMAAAAASUVORK5CYII=
// @grant        GM.xmlHttpRequest
// @run-at       document-end
// @connect      plus.sjtu.edu.cn
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';
    const captcha_input_selector = "#input-login-captcha";
    const captcha_image_selector = "#captcha-img";
    $(document).ready(function () {
        if (!$(captcha_image_selector).length) {
            return; // 没有验证码时退出
        }
        $("#captcha-box").stop();
        $("#captcha-box").attr("style", "overflow: hidden;");
        $("#operate-buttons").show();

        let raw_placeholder = $(captcha_input_selector).attr("placeholder");
        $("body").append($('<canvas width="110" height="40" style="display: none;" id="captcha-canvas"></canvas>'));
        let img_elem = $(captcha_image_selector)[0];
        let canvas_elem = document.getElementById("captcha-canvas");
        let recognize = function () {
            canvas_elem.getContext("2d").drawImage(img_elem, 0, 0);
            let dataURL = canvas_elem.toDataURL("image/jpeg");
            fetch(dataURL).then(function (response) {
                response.arrayBuffer().then(function (arrayBuffer) {
                    let formData = new FormData();
                    formData.append('image', new File([arrayBuffer], 'captcha.jpg'));
                    GM.xmlHttpRequest({
                        url: "https://plus.sjtu.edu.cn/captcha-solver/",
                        method: "POST",
                        data: formData,
                        onload: function (response) {
                            let captcha_text;
                            try {
                                captcha_text = JSON.parse(response.responseText)["result"];
                            } catch (e) {
                                $(captcha_input_selector).attr("placeholder", "识别服务异常");
                                return;
                            }
                            $(captcha_input_selector).attr("value", captcha_text);
                            $(captcha_input_selector).attr("spellcheck", "false");
                            $(captcha_input_selector).attr("placeholder", raw_placeholder);
                            $("#login-form").show();
                        }
                    });
                });
            });
        };
        const do_recognize = function () {
            $(captcha_image_selector).hide();
            $(captcha_input_selector).attr("placeholder", "正在加载验证码");
            $(captcha_input_selector).attr("value", "");
            $(captcha_input_selector).focus();
            let hue = Math.random();
            let saturation = Math.random() + 0.5;
            $(captcha_image_selector).css("filter", `hue-rotate(${hue}turn) saturate(${saturation}) blur(0.5px)`);
            let img_complete_check = setInterval(function () {
                if ($(captcha_image_selector)[0].complete) {
                    clearInterval(img_complete_check);
                    $(captcha_input_selector).attr("placeholder", "正在识别");
                    $(captcha_image_selector).show();
                    recognize();
                }
            }, 1);
        }
        do_recognize(); // 打开后执行一次
        //$(captcha_image_selector).on("click", do_recognize); // 刷新验证码后再执行一次

        if (typeof refreshCaptcha == "function") {
            let refreshCaptcha0 = refreshCaptcha;
            refreshCaptcha = function () {
                refreshCaptcha0();
                do_recognize(); // 刷新验证码后再执行一次
            }
        }
    }());
})();