杭电选课助手

try to take over the world!

当前为 2020-06-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         杭电选课助手
// @icon         https://bkimg.cdn.bcebos.com/pic/7aec54e736d12f2e307562024fc2d56285356864?x-bce-process=image/resize,m_lfit,w_268,limit_1/format,f_jpg
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       RecLusIve_F
// @match        *://jxgl.hdu.edu.cn/*
// @require      https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/FileSaver.min.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/toastr.min.js
// @exclude      http://jxgl.hdu.edu.cn/CheckCode.aspx
// @resource toastrCss https://cdn.bootcdn.net/ajax/libs/toastr.js/2.1.4/toastr.min.css
// @grant        GM_xmlhttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// @grant        GM_notification
// @grant        GM_getResourceText
// @grant        GM_addStyle
// @run-at       document-idle
// ==/UserScript==
(function() {
	'use strict';
	const $ = window.jQuery;
	const saveAs = window.saveAs;
    const toastr = window.toastr;
    toastr.options = {
        "closeButton": false,
        "debug": false,
        "newestOnTop": false,
        "progressBar": false,
        "positionClass": "toast-bottom-center",
        "preventDuplicates": true,
        "onclick": null,
        "showDuration": "300",
        "hideDuration": "1000",
        "timeOut": "3000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };
    GM_addStyle(GM_getResourceText('toastrCss'));
    $("#iframeautoheight").attr("sandbox", "allow-same-origin allow-top-navigation allow-forms allow-scripts allow-downloads");
	var className = GM_getValue("CN", []),
		classTime = GM_getValue("CT", []);
	var flag = GM_getValue("FLAG", false);

	function insertSaveInfoBtn() {
		$("#Button2").after(`<input type="button" value="保存信息" id="saveBtn" class="button">`);
		$("#saveBtn").on("click", saveClassInfo);
		//$("#saveBtn").on("click", rec);
	}

	function saveClassInfo() {
		let str_store = "";
		let tr = $("#kcmcGrid > tbody > tr");
		for (let i = 0; i < tr.length; i++) {
			let td = $(tr[i]).children("td");
			for (let j = 2; j < td.length - 1; j++) {
				str_store += $(td[j]).text().replace(/^\s+|\s+$/g, '') + " ";
			}
			str_store += $(td[td.length - 1]).text().replace(/^\s+|\s+$/g, '') + "\n";
		}
		//console.log(str_store);
		var blob = new Blob([str_store], {
			type: "text/plain;charset=utf-8"
		});
		saveAs(blob, "ClassInfo.txt");
	}

	function insertCheckComponent() {
		$("#xsyxxxk_form > div.toolbox > div:nth-child(5) > p.search_con").after(
			`<p class="search_con" id="classInfo">输入需要选择的课程名称:<input type="text" id="ClassName" style="width:152px;">&nbsp;&nbsp;输入需要选择的课程时间:<input type="text" id="ClassTime" style="width:152px;">
						<input type="button" value="确定" id="addClassNameBtn" class="button" style="width:56px;"><input type="button" value="清空" id="deleteBtn" class="button" style="width:56px;"><input type="button" value="开始抢课" id="startBtn" class="button" style="width:66px;"><input type="button" value="停止抢课" id="stopBtn" class="button" style="width:66px;">已添加要选择课程名称为:</p>`
		);
		$("#addClassNameBtn").on("click", handleClassInfo);
		$("#deleteBtn").on("click", deleteClass);
		$("#startBtn").on("click", startToApply);
		$("#stopBtn").on("click", stopToApply);
		for (let i = 0; i < className.length; i++) {
			$("#classInfo").append(className[i] + " ");
			chooseClass(className[i], classTime[i]);
		}
	}

	function startToApply() {
        checkResult();
        if (className.length == 0) {
            flag = false;
            GM_setValue("FLAG", false);
            clearTimeout(GM_getValue("ID", null));
            GM_setValue("ID", null);
            toastr.error("<b>选择课程为空!</b>");
		} else{
            toastr.success("<b>开始抢课.....</b>");
            GM_setValue("FLAG", true);
            GM_setValue("ID", setTimeout(apply, 5000));
        }
	}

	function stopToApply() {
		flag = false;
		GM_setValue("FLAG", false);
		clearTimeout(GM_getValue("ID", null));
		GM_setValue("ID", null);
        toastr.warning("<b>停止抢课.....</b>");
	}

	function apply() {
        rec();
        $("#Button1").click();
        startToApply();
	}

	function handleClassInfo() {
		let cn = $("#ClassName").val();
		let ct = $("#ClassTime").val();
		if (cn != '' && ct != '') {
			className.push(cn);
			classTime.push(ct);
			$("#classInfo").append(cn + " ");
			$("#ClassName").val('');
			$("#ClassTime").val('');
			GM_setValue("CN", className);
			GM_setValue("CT", classTime);
			chooseClass(cn, ct);
		} else {
			alert("名称和时间不能为空!");
		}
	}

	function deleteClass() {
		GM_setValue("CN", []);
		GM_setValue("CT", []);
		className = GM_getValue("CN", []);
		classTime = GM_getValue("CT", []);
		window.location.reload();
	}

	function chooseClass(cn, ct) {
		let tr = $("#kcmcGrid > tbody > tr");
		for (let i = 1; i < tr.length; i++) {
			let td = $(tr[i]).children("td");
			if ($(td[2]).children("a").text().replace(/^\s+|\s+$/g, '') == cn && $(td[5]).text().replace(/^\s+|\s+$/g, '') ==
				ct) {
				for (let j = 0; j < 2; j++) {
					if (!$(td[j]).children("input").is(':checked')) {
						$(td[j]).children("input").click();
						//console.log($(td[2]).text());
					}
				}
			}
		}
	}

	function rec() {
		var image = document.querySelector(
			"#xsyxxxk_form > div.main_box > div > div.footbox > em > span.footbutton > span > img");
		var canvas = document.createElement('canvas');
		var ctx = canvas.getContext("2d");
		var numbers = ["110001110000000001100001110000111000011100001110000111000011100001110010000011100001",
			"111101111100111000011000001100100111110001111000111100111110011111001111100111110011",
			"110000100000000011100001110011111001111000111000111000111000111000111100000000000000",
			"110000100000010011000111100111100011100001111100011111000011100001100000000011100011",
			"111100111100011110001110000111000011001001001100100110010000000000000011110011111001",
			"100000010000001001111100111110000010000000001110011111000011100000110010000001100001",
			"110000110000000011100001111100100010000000001110000111000011100000110010000001100001",
			"000000000000001111100111100111100111110011110001111001111100111100011110011111001111",
			"110001110000000001100001110000010001000001100000100111000011100001110000000001100011"
		];
		var captcha = "";
		canvas.width = image.width;
		canvas.height = image.height;
		ctx.drawImage(image, 0, 0);
		for (var i = 0; i < 5; i++) {
			var pixels = ctx.getImageData(9 * i + 6, 5, 7, 12).data;
			var ldString = "";
			for (var j = 0, length = pixels.length; j < length; j += 4) {
				ldString = ldString + (+(pixels[j] * 0.3 + pixels[j + 1] * 0.59 + pixels[j + 2] * 0.11 >= 140));
			}
			var comms = numbers.map(function(value) {
				return ldString.split("").filter(function(v, index) {
					return value[index] === v
				}).length
			});
			captcha += comms.indexOf(Math.max.apply(null, comms));
		}
		$("#txtYz").val(captcha);
	}

    function checkResult(){
        let tr = $("#DataGrid2 > tbody > tr");
        let f = false;
        for (let i = 1; i < tr.length; i++) {
			let td = $(tr[i]).children("td");
            for (let j = 0; j < className.length; j ++){
                if (className[j] == $(td[0]).text()){
                    //console.log($(td[0]).text());
                    f = true;
                    className.splice(j, 1);
                    classTime.splice(j, 1);
                    GM_setValue("CN", className);
                    GM_setValue("CT", classTime);
                    break;
                }
            }
            //console.log($(td[0]).text());
		}
        if (f){
            window.location.reload();
        }
    }

	insertCheckComponent();
	if (flag) {
		startToApply();
	}
})();