校情与教学质量问卷自动化(GXMU)

自动完成调查问卷

// ==UserScript==
// @name         校情与教学质量问卷自动化(GXMU)
// @namespace    http://tampermonkey.net/
// @version      0.4.8
// @description  自动完成调查问卷
// @author       叶月绘梨依
// @match        http://10.129.2.141/new/student/rank*
// @match        http://10.129.2.141/new/student/rank/*
// @match        http://10.129.2.141/new/student/lzdx_rank/*

// @match        http://10-129-2-141.svpn.gxmu.edu.cn:8118/new/student/rank/*
// @match        http://10-129-2-141.svpn.gxmu.edu.cn:8118/new/student/rank*
// @match        http://10-129-2-141.svpn.gxmu.edu.cn:8118/new/student/lzdx_rank/*

// @match        http://wspj.gxmu.edu.cn/new/student/rank*
// @match        http://wspj.gxmu.edu.cn/new/student/rank/*
// @match        http://wspj.gxmu.edu.cn/new/student/lzdx_rank/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=48.43

// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    /***** 参数配置 *****/
    var init_time=3000; //当页面加载完成后,延时3秒后启动评测脚本
    var finish_each_time=3600; //完成每次评测后间隔时间6s
    var close_time=1500; //关闭评测界面的对话框后延时2s;
    /***** 参数配置 *****/

    var all_tasks;
    var all_links=[];
    var isload=0;
    //返回主页
    function go_home(){
        //使用正则将'/*rank*'一大串字符替换成主页网址的‘studen_login/index.jsp’
        let return_home=window.location.href.replace(/(.*)(\/.*\/.*)\/.*rank.*/,'$1$2')+'/student_login/index.jsp';
        //跳转主页
        window.location.replace(return_home);
    }

    //关闭对话框
    function fclose(){
        //var gcloses=document.querySelectorAll("button[data-dismiss='modal'].blue");
        //for(let i=0;i<gcloses.length;i++){
        //  try{ gcloses[i].click();}catch(e){console.log("some error happened when try to find the function which enbale to close the modal...");}
        //}
        try{
            //这里关闭的是测评页面的对话框
            var finishDlg=document.getElementById("finishDlg");
            if(finishDlg){
                finishDlg.innerHTML+=`<button id="close_" data-dismiss="modal">close</button>`;
                var close_=document.getElementById("close_");
                if(close_){
                    close_.click();
                    finishDlg.removeChild(close_);
                }
            }
        }catch(e){
        }
    }

    setTimeout(function(){
        all_tasks=document.querySelectorAll("td>span.badge-important,td>span.badge-success");
        //console.log(all_tasks);
        var raw_all_links=document.querySelectorAll("td>div.tdrepaire>a[onclick].blue");
        for(let h=0;h<raw_all_links.length;h++){
            if(raw_all_links[h].innerText=="评价") all_links.push(raw_all_links[h]);
        }

        var undone=[];
        for(let i=0;i<all_tasks.length;i++){
            var badge_class=all_tasks[i].getAttribute("class");
            console.log(badge_class);
            if(badge_class=="badge badge-important"){
                undone.push(i);
            }
        }

        console.log(undone)
        console.log("undone_len: "+undone.length)
        var count=0;
        if(undone.length==0){
            alert("所有评测可能完成,请自行检查!");
            go_home();
        }
        else{
            var inter=setInterval(function(){
                if(count==undone.length){
                    clearInterval(inter);
                    fclose();
                    alert("所有评测可能完成,请自行检查!");
                    go_home();
                }

                if(all_links[undone[count]]){
                    all_links[undone[count]].click()
                    count++;
                }
                //找到所有选项(10分..0分)
                var opts=document.querySelectorAll("label.radio>div.radio>span");
                //console.log(opts);

                //筛选出10分的选项,自动选择10分的选项
                for(let i=0;i<opts.length;i++){
                    var op=opts[i].querySelector("input");
                    if(op.value=="10"||op.value=="1"){
                        var item=opts[i].parentNode.parentNode;
                        item.dispatchEvent(new MouseEvent("click"));
                    }
                }

                //由于问卷不能满分,所以倒数第11题固定选第二个
                var lastChange=opts[opts.length-9]?opts[opts.length-9]:opts[opts.length-8]
                lastChange.parentNode.parentNode.dispatchEvent(new MouseEvent("click"));

                //点击提交按钮
                var submit=document.getElementById("pjsubmit");

                //关闭按钮
                submit.click();
                fclose();
                //做一个延迟,避免太快后导致窗口黑屏了...会让人以为卡死了orz
                setTimeout(()=>console.log("awaiting..."),close_time);
            },finish_each_time);
        }
    },init_time)

})();