更好用的NXU

让NXU更好用!

目前為 2023-06-29 提交的版本,檢視 最新版本

// ==UserScript==
// @name         更好用的NXU
// @namespace    http://tampermonkey.net/
// @version      0.0.6
// @description  让NXU更好用!
// @author       H
// @license      Apache Licence
// @match        https://webvpn.nxu.edu.cn/*
// @match        https://jwgl.nxu.edu.cn/*
// @grant        unsafeWindow
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_getResourceText
// @grant        GM_getResourceURL
// @grant        window.close
// @require      https://cdn.jsdelivr.net/npm/tesseract.js@4/dist/tesseract.min.js
// @require      https://greasyfork.org/scripts/469717-nxu%E5%82%A8%E5%AD%98%E5%BA%93/code/NXU%E5%82%A8%E5%AD%98%E5%BA%93.js?version=1212570
// @connect      webvpn.nxu.edu.cn/*
// @resource     svg-logo https://cdn.bootcdn.net/ajax/libs/font-awesome/6.2.1/css/all.min.css
// @noframes
// ==/UserScript==


//==需要调用的函数==

//关闭窗口
function closeWindow() {
    try {
        window.opener = window;
        var win = window.open("", "_self");
        win.close();
        top.close();
    } catch (error) {
        console.log("关闭窗口:失败\n错误信息:"+error);
    }
}

//随机数
function random(min, max){
    return parseInt(Math.random()*(max-min+1)+min,10);
}

//等待执行
function justWait(min, max, log = true){
    var waitmsg,waittime;
    if (max == 0) {
        waittime = min;
        waitmsg = "==等待了:"+(waittime / 1000)+" 秒==";
    } else {
        waittime = random(min, max);
        waitmsg = "==随机等待了:"+(waittime / 1000)+" 秒==";
    }
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            if (log) {
                console.log(waitmsg);
            }
            resolve();
        }, waittime);
    });
}

//toast输出
function createToast(id, mes, time = 5) {
    const notifications = document.querySelector('.notifications');
    const toastDetails = {
        success:{
            icon: 'fa-circle-check',
            text: 'Success: This is a success toast.'
        },
        error:{
            icon: 'fa-circle-xmark',
            text: 'Error: This is a error toast.'
        },
        warning:{
            icon: 'fa-circle-exclamation',
            text: 'Warning: This is a warning toast.'
        },
        info:{
            icon: 'fa-circle-info',
            text: 'Info: This is a info toast.'
        }
    };
    if (document.querySelector("style.i"+time.toString().replace(".", "\\."))){
        document.querySelector("style.i"+time.toString().replace(".", "\\.")).innerHTML = ".toast.i"+time+"::before{animation-duration: "+time+"s;}";
    } else {
        var style = document.createElement("style");
        style.className = "i"+time;
        style.innerHTML = ".toast.i"+time+"::before{animation-duration: "+time+"s;}";
        document.getElementsByTagName("head")[0].appendChild(style);
    }
    // console.log(id)
    const {icon, text} = toastDetails[id];
    let txt;
    if (mes == "" || mes == null || typeof(mes) == "undefined"){
        txt = text;
    } else {
        txt = mes;
    }
    const toast = document.createElement('li') // 创建li元素
    toast.setAttribute("time", time);
    toast.setAttribute("style", "::before{animation-duration: "+time+"}")
    toast.className = `toast ${id} i${time}` // 为li元素新增样式
    toast.innerHTML = `
    <div class="column">
        <i class="fa-solid ${icon}"></i>
        <span>${txt}</span>
    </div>
    <i class="fa-solid fa-xmark" onClick="removeToast(this.parentElement)"></i>`
    notifications.appendChild(toast) // 添加元素到 notifications ul
    // 隐藏toast
    if (time != 0){
        toast.timeoutId = setTimeout(()=> removeToast(toast), time * 1000);
    }
}

//toast移除
function removeToast(toast) {
    toast.classList.add('hide')
    if( toast.timeoutId) clearTimeout(toast.timeoutId) // 清楚setTimeout
    // 移除li元素
    setTimeout(() => {
        toast.remove()
    },500)
}

//创建XHR
function createXHR() {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                console.log("您的浏览器不支持AJAX!");
                return false;
            }
        }
    }
    return xmlHttp;
}

function titleChange(dom) {
    //console.log("更改视图...")
    if (dom.id == "plugin-settings") {
        //document.querySelector(".anchor__item.is-active").classList.remove("is-active");
        //dom.classList.add("is-active");
        //var scrollableDiv = document.querySelector("div.portal-content__block").querySelector(".el-scrollbar__view");
        //var contentDiv = document.querySelector("div#plugin-settings-content");
        //document.querySelector("div#plugin-settings-content").scrollIntoView({ behavior: 'auto', inline: 'center' });
        let div = document.querySelector("div#plugin-settings-content");
        let div_parent = document.querySelector("div.portal-content__block").querySelector(".el-scrollbar__wrap");
        document.querySelector("div.portal-content__block").querySelector(".el-scrollbar__wrap").scrollBy(0, div.getBoundingClientRect().top - div_parent.getBoundingClientRect().top)
    } else {
        //dom.classList.add("is-active");
        //document.querySelector("div.anchor__item#plugin-settings").classList.remove("is-active");
    }
}

//绑定
unsafeWindow.createToast = createToast;
unsafeWindow.removeToast = removeToast;
unsafeWindow.window.close = window.close;
unsafeWindow.titleChange = titleChange;
//==/需要调用的函数==


//==预关闭==
if (!document.querySelector("title")){
    if (GM_getValue("auto-close")) {
        window.close();
    } else {
        return;
    }
}
//==/预关闭==


//==常量==
const url = window.location.href;
const type = document.querySelector("title").innerHTML;
const load_message = {"loading tesseract core":"核心加载","initializing tesseract":"初始化","loading language traineddata":"加载语言训练数据","initializing api":"初始化接口","recognizing text":"识别验证码"};
//==/常量==


//==开始==
console.log("插件运行...");

//添加css样式
GM_addStyle(css_content);
GM_addStyle(GM_getResourceText("svg-logo").replace(/\.\.\/webfonts/g, "https://cdn.bootcdn.net/ajax/libs/font-awesome/6.2.1/webfonts"));

//插入toast列表
var ul = document.createElement("ul");
ul.className = "notifications";
document.getElementsByTagName("body")[0].appendChild(ul);
//createToast("success", "测试消息", 0);

//自动关闭处理
if ((document.querySelector("body").innerHTML.indexOf("啊呀,服务器开小差了。3秒后自动返回上一页") != -1)
    || (document.querySelector("body").innerHTML.indexOf("出错啦!该网站无法访问!") != -1)
    || (url.indexOf("/login.action") != -1 && url != "https://jwgl.nxu.edu.cn/login.action")){
    if (GM_getValue("auto-close")) {
        window.close();
    } else {
        return;
    }
} else if (url.indexOf("/index.action?wrdrecordvisit") != -1) {
    if (type == "宁夏大学教务管理信息系统") {
        let div = document.createElement("div");
        div.setAttribute("style", "z-index: 9999;padding: 2em 3em;white-space: nowrap;;font-size: 16px;position: fixed;top: 50%;left: 50%;transform: translate(-50%,-50%);border-radius: 10px;border: 1px white solid;background: #49d3ef;background: -webkit-linear-gradient(to left, #49d3ef, #016ba9);background: linear-gradient(to right bottom, #49d3ef, #016ba9);display: flex;flex-direction: column;align-items: center;color: white;justify-content: center;");
        div.innerHTML = jwgl_error;
        document.querySelector("body").appendChild(div);
        GM_addElement(document.querySelector("div#control-button"), "button", {textContent: '立即跳转', onclick: "location.href = 'https\:\/\/jwgl.nxu.edu.cn'", style: "height: auto;margin: 0.5em 1em;padding: 0.5em 1em;border: 1px white solid;border-radius: 4px;font-weight: bold;color: white;background:#2193b0;background: -webkit-linear-gradient(to right bottom, #6dd5ed, #2193b0);background: linear-gradient(to right bottom, #6dd5ed, #2193b0);"});
    }
}

//网页判断
if (url == "https://jwgl.nxu.edu.cn/index.action" || url == "https://jwgl.nxu.edu.cn/login.action" ) {
    console.log("检测到教务系统登录页面");
    //自动登录
    if (!GM_getValue("auto-login") || GM_getValue("username") == undefined || GM_getValue("username") == "" || GM_getValue("username") == null || GM_getValue("password") == undefined || GM_getValue("password") == "" || GM_getValue("password") == null) {
        return;
    }
    createToast("info", "自动登录...", 5);
    //自动识别验证码
    Tesseract.recognize(
        'https://jwgl.nxu.edu.cn/captcha/image.action',
        'eng',
        { logger: m => load_message[m.status] ? (createToast("info", load_message[m.status], 1)) : (null) }
    ).then(({ data: { text } }) => {
        jwglLogin(text);
    })
} else if (url.indexOf("https-443") != -1) {
    if (document.getElementsByTagName("h1")[0] && document.getElementsByTagName("h1")[0].innerHTML) {
        if (document.getElementsByTagName("h1")[0].innerHTML == 'An error occurred.') {
            console.log("检测到服务器错误页面");
            if (GM_getValue("auto-close")) {
                window.close();
            } else {
                return;
            }
        }
    } else if (type == "宁夏大学教务管理信息系统"){
        console.log("检测到宁夏大学教务管理信息系统页面");
        console.log("等待800ms");
        if (document.getElementsByName("loginForm.name")[0]) {
            if (GM_getValue("auto-close")) {
                window.close();
            }
            return;
        }
        //成功进入,监听课表页面
        isIframeChange();
    }
} else if (url.indexOf("login") != -1){
    console.log("疑似登录页面,确认中...");
    if (document.querySelector("title").innerHTML.indexOf("503 Service Temporarily Unavailable") != -1) {
        location.reload();
    }
    if (!document.querySelector(".auth_tab")) {
        return;
    }
    console.log("登录页面");
    if (GM_getValue("username") == undefined || GM_getValue("username") == "" || GM_getValue("username") == null || GM_getValue("password") == undefined || GM_getValue("password") == "" || GM_getValue("password") == null) {
        return;
    }
    //自动登录
    if (GM_getValue("auto-login")) {
        console.log("自动登录...");
        createToast("info", "自动登录...", 5);
        if (document.querySelector("#msg.auth_error")) {
            createToast("error", "输入的学号密码有误", 5);
            return;
        }
        document.querySelector("input#username").value = GM_getValue("username");
        document.querySelector("input#password").value = GM_getValue("password");
        document.querySelector("button.auth_login_btn").click();
    } else {
        return;
    }
} else if (url == "https://webvpn.nxu.edu.cn/" || url == "https://webvpn.nxu.edu.cn"){
    console.log("资源访问控制系统首页");
    resourceIndex();
}

async function jwglLogin(text) {
    document.getElementsByName("loginForm.captcha")[0].onfocus();
    //document.querySelector("input#textfield.loginput").value = "";
    //document.querySelector("input#textfield2.loginput").value = "";
    document.querySelector("input#textfield.loginput").value = GM_getValue("username");
    document.querySelector("input#textfield2.loginput").value = GM_getValue("password");
    document.querySelector("input#textfield3.loginput").value = text;
    document.querySelector("input#submitButton").click();
}

async function resourceIndex() {
    //等待标签加载完毕
    while(true) {
        if (document.querySelector("div.el-scrollbar__view") && document.querySelector("div.el-scrollbar__view").innerHTML) {
            break;
        }
        await justWait(100, 0);
    }
    var div_title = document.querySelector("div.el-scrollbar__view");
    var div_setting_title = document.createElement("div");
    div_setting_title.innerHTML = "插件设置";
    div_setting_title.classList.add("anchor__item");
    div_setting_title.id = "plugin-settings";
    div_title.appendChild(div_setting_title);
    var div_settings = document.createElement("div");
    div_settings.innerHTML = settings_div;
    div_settings.id = "plugin-settings-content";
    document.querySelector("div.portal-content__block").querySelector(".el-scrollbar__view").appendChild(div_settings);
    var div_option = document.querySelectorAll("div.anchor__item");
    for (let i = 0; i < div_option.length; i++) {
        div_option[i].setAttribute("onclick", "titleChange(this)");
    }
    document.querySelector("div.portal-content__block").querySelector(".el-scrollbar__wrap").onscroll = () => {
        let div = document.querySelector("div#plugin-settings-content");
        let div_parent = document.querySelector("div.portal-content__block").querySelector(".el-scrollbar__wrap");
        if (div.getBoundingClientRect().top <= div_parent.getBoundingClientRect().top) {
            document.querySelector("div.anchor__item#plugin-settings").classList.add("is-active");
        } else {
            document.querySelector("div.anchor__item#plugin-settings").classList.remove("is-active");
        }
    }
    var button = document.querySelector("#reflash-user-info");
    var input_username = document.querySelector("input#username");
    var input_password = document.querySelector("input#password");
    if (GM_getValue("username") == undefined || GM_getValue("username") == "" || GM_getValue("username") == null || GM_getValue("password") == undefined || GM_getValue("password") == "" || GM_getValue("password") == null) {
        input_username.value = "";
        input_password.value = "";
    } else {
        input_username.value = GM_getValue("username");
        input_password.value = GM_getValue("password");
    }
    reflashUserInfoCheck()
    try {
        // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
        button.addEventListener("click", function(){
            reflashUserInfo();
        }, false);
        input_username.addEventListener("input", function(){
            reflashUserInfoCheck();
        }, false);
        input_password.addEventListener("input", function(){
            reflashUserInfoCheck();
        }, false);
    } catch (error) {
        try {
            // IE8.0及其以下版本
            button.attachEvent("onclick", function(){
                reflashUserInfo();
            }, false);
            input_username.addEventListener("oninput", function(){
                reflashUserInfoCheck();
            }, false);
            input_password.addEventListener("oninput", function(){
                reflashUserInfoCheck();
            }, false);
        } catch (error) {
            // 早期浏览器
            alert("错误: 按钮绑定事件失败\n很抱歉,该脚本并不适合您的浏览器\n请尝试安装最新版Edge、Chrome、FireFox、Opera、Safari或其他浏览器后获得支持");
            return;
        }
    }
    var input_switch_total = document.querySelectorAll("label.switch");
    for (let i = 0; i < input_switch_total.length; i++) {
        var input_switch = input_switch_total[i].querySelector("input");
        input_switch.checked = GM_getValue(input_switch.id);
        try {
            // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
            input_switch.addEventListener("input", function(){
                switchHandle(this);
            }, false);
        } catch (error) {
            try {
                // IE8.0及其以下版本
                input_switch.addEventListener("oninput", function(input_switch){
                    switchHandle(this);
                }, false);
            } catch (error) {
                // 早期浏览器
                alert("错误: 按钮绑定事件失败\n很抱歉,该脚本并不适合您的浏览器\n请尝试安装最新版Edge、Chrome、FireFox、Opera、Safari或其他浏览器后获得支持");
                return;
            }
        }
    }
}

function reflashUserInfo() {
    var warning = document.querySelectorAll("p.warning");
    for (let i = 0; i < warning.length; i++) {
        warning[i].style.display = "none";
    }
    var username = document.querySelector("input#username").value;
    var password = document.querySelector("input#password").value;
    //console.log(username);
    var pass = true;
    if (username == null || username == "") {
        document.querySelector("p.warning#username-empty").style.display = "block";
        pass = false;
    } else if (!/^1\d{9}\d$/.test(username)) {
        console.log(username);
        document.querySelector("p.warning#username-error").style.display = "block";
        pass = false;
    }
    if (password == null || password == "") {
        document.querySelector("p.warning#password-empty").style.display = "block";
    }
    if (pass) {
        //console.log("pass");
        GM_setValue("username", username);
        GM_setValue("password", password);
        createToast("success", "账号设置成功!", 2);
        document.querySelector("#reflash-user-info").disabled = true;
    }
}

function reflashUserInfoCheck() {
    var username = document.querySelector("input#username").value;
    var password = document.querySelector("input#password").value;
    if (GM_getValue("username") == undefined || GM_getValue("username") == "" || GM_getValue("username") == null || GM_getValue("password") == undefined || GM_getValue("password") == "" || GM_getValue("password") == null) {
        return;
    }
    //console.log(username)
    //console.log(password)
    if (username == GM_getValue("username") && password == GM_getValue("password")) {
        document.querySelector("#reflash-user-info").disabled = true;
    } else {
        document.querySelector("#reflash-user-info").disabled = false;
    }
}

function switchHandle(input) {
    GM_setValue(input.id, input.checked);
    var message = input.parentNode.parentNode.querySelector("p").innerHTML;
    if (input.checked) {
        message += "&nbsp;&nbsp;&nbsp;&nbsp;开";
    } else {
        message += "&nbsp;&nbsp;&nbsp;&nbsp;关";
    }
    createToast("info", message, 2);
}

function isIframeChange() {
    var elemIfram = document.querySelector("iframe#main");
    if (window.MutationObserver || window.webkitMutationObserver) {
        // chrome
        var callback = function(mutations) {
            mutations.forEach(function() {
                iframeChange();
            });
        };
        var observer
        if (window.MutationObserver) {
            observer = new MutationObserver(callback);
        } else {
            observer = new webkitMutationObserver(callback);
        }
        observer.observe(elemIfram, {
            subtree: true,
            attributes: true,
            attributeOldValue: true
        });
    } else if (elemIfram.addEventListener) {
        // Firefox, Opera and Safari
        elemIfram.addEventListener("DOMAttrModified", function(){iframeChange();}, false);
    } else if (elemIfram.attachEvent) {
        // Internet Explorer
        elemIfram.attachEvent("onpropertychange", function(){iframeChange();});
    }
}

function iframeChange() {
    console.log("111");
}

async function needLogin() {
    GM_xmlhttpRequest({
        method: "GET",
        url: "https://webvpn.nxu.edu.cn/",
        onload: function(response) {
            console.log(response);
        }
    });
    await justWait(1000 * 60 * 10, 0);
}

document.onvisibilitychange = ()=>{console.log(document.visibilityState)}