HDU自动登录

自动登录HDUOJ

当前为 2017-10-31 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         HDU自动登录
// @namespace    YinTianliang_i
// @version      0.1.1
// @description  自动登录HDUOJ
// @author       Yin Tianliang
// @include      *//hdu.hustoj.com/*
// @include      *//acm.hdu.edu.cn/*
// @include      *//acm.split.hdu.edu.cn/*
// @require      https://code.jquery.com/jquery-3.2.1.min.js
// @grant        none
// @run-at       document-start
// ==/UserScript==
//alert('YES');
//console.log(window.location.toString())
//TODO:判断当前登录状态
function getCookie(cookiename) {
    var reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
    var arr = document.cookie.match(reg);
    if (arr) {
        return unescape(arr[2]);
    } else {
        return null;
    }
}

function setUserInfo() {
    username = prompt("请输入用户名:");
    userpass = prompt("请输入密码:");
    document.cookie = "username=" + escape(username) + "; expires=Thu, 18 Dec 2333 12:00:00 GMT";
    document.cookie = "userpass=" + escape(userpass) + "; expires=Thu, 18 Dec 2333 12:00:00 GMT";
}

function contestLogin(contestID, contestPSW) {
    $.ajax({
        type: "post",
        url: "http://" + window.location.host + "/diy/contest_login.php?cid=" + contestID + "&action=login",
        data: {
            Origin: "http://" + window.location.host,
            Referer: "http://" + window.location.host,
            password: contestPSW
        },
        success: function () {
            //alert('Login Success!');
        }
    }).fail(() => { alert('登录失败,建议清空cookie重新尝试'); });
}


//如果跳转到了登录界面,则开始执行逻辑
//提取url中的contestID 判断是否在cookie中
var reg = new RegExp("diy/contest_login.php.cid=(\\d+)");
var arr = window.location.toString().match(reg);
if (arr) {


    //用户登录
    var username = getCookie("username");
    var userpass = getCookie("userpass");
    var contestID = getCookie("contestID");
    var contestPSW = getCookie("contestPSW");
    if (username === null || userpass === null) {
        setUserInfo();
    }

    $.ajax({
        type: "post",
        url: "http://" + window.location.host + "/userloginex.php?action=login",
        data: {
            Origin: "http://" + window.location.host,
            Referer: "http://" + window.location.host,
            username: username,
            userpass: userpass,
            login: "Sign In"
        },
        success: function () {
            //alert('Login Success!');
        }
    }).fail(() => { alert('登录失败,建议清空cookie重新尝试'); });


    //单项测试登录
    if (!new RegExp(arr[1]).test(contestID)) {
        var psw = prompt("该测试的口令未被记录,请输入该测试的口令");
        contestID += '|' + arr[1];
        contestPSW += '|' + psw;
        document.cookie = "contestID=" + contestID + "; expires=Thu, 18 Dec 2333 12:00:00 GMT";
        document.cookie = "contestPSW=" + contestPSW + "; expires=Thu, 18 Dec 2333 12:00:00 GMT";
    }
    contestID = contestID.split('|');
    contestPSW = contestPSW.split('|');
    for (var i = 0; i < contestID.length; i++) {
        contestLogin(contestID[i], contestPSW[i]);
    }


    //跳转到题目页面
    window.location.href = "http://" + window.location.host + '/diy/contest_show.php?cid=' + arr[1];

}