HUT加权分数计算——学习完成情况(属性)

HUT加权分数计算

// ==UserScript==
// @name         HUT加权分数计算——学习完成情况(属性)
// @namespace    https://greasyfork.org/zh-CN/scripts/460850-hut%E5%8A%A0%E6%9D%83%E5%88%86%E6%95%B0%E8%AE%A1%E7%AE%97-%E5%AD%A6%E4%B9%A0%E5%AE%8C%E6%88%90%E6%83%85%E5%86%B5-%E5%B1%9E%E6%80%A7
// @version      0.5
// @description  HUT加权分数计算
// @author       Vanisper
// @match        http://218.75.197.123:83/jsxsd/xxwcqk/xxwcqkOnkcxz.do
// @grant        none
// @license MIT
// ==/UserScript==


(function() {
    'use strict';
    const table = document.querySelector("body > div > div > table:nth-child(7) > tbody");
    // 五等级制
    const fs = {
        "优": 90,
        "良": 80,
        "中": 70,
        "及格": 60,
        "不及格": 50
    }
    const stringHasZh = (words) => {
        const patrn = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
        if (!patrn.exec(words)) {
            return false;
        }
        else {
            return true;
        }
    }
    if (table){
        const i = 3;
        const j = table.childElementCount;
        // document.querySelector("body > div > div > table:nth-child(7) > tbody > tr:nth-child(3) > td:nth-child(5)")
        // document.querySelector("body > div > div > table:nth-child(7) > tbody > tr:nth-child(4) > td:nth-child(5)")
        // document.querySelector("body > div > div > table:nth-child(7) > tbody > tr:nth-child(87) > td:nth-child(5)")
        const ttt = document.querySelector("body > div > div > table:nth-child(4) > tbody > tr > td");
        const template =(i,j)=> `body > div > div > table:nth-child(7) > tbody > tr:nth-child(${i}) > td:nth-child(${j})`;
        let s = []; // 成绩
        let q = []; // 学分
        let g = []; // 绩点
        for (var index = i; index<=j; index++) {
            const dom = document.querySelector(template(index, 5)); // 成绩
            const dom1 = document.querySelector(template(index, 3)); // 学分
            const dom2 = document.querySelector(template(index, 4)); // 公选||必、选修
            // 筛选掉修读中 | 五等级制放行
            if(dom && dom.innerText.replace(/\s/g,"")!="" && dom.innerText!="修读中" && dom2.innerText!=="公选"){
                if(stringHasZh(dom.innerText)){
                    // 五等级制分数转换
                    dom.innerText = (Object.values(fs)[Object.keys(fs).indexOf(dom.innerText)]);
                }
                s.push(+dom.innerText);
                if(+dom.innerText>=60){
                    g.push((+dom.innerText-50)/10);
                }else if(+dom.innerText<60){
                    g.push(0);
                }
                q.push(+dom1.innerText.replace(/(计划内)/ig, ``));
            }
        }
        const len = q.reduce((c, R) => c + R, 0);
        const res = s.map((e,i)=>e*q[i]).reduce((c, R) => c + R, 0)/len;
        const gd = g.map((e,i)=>e*q[i]).reduce((c, R) => c + R, 0)/len;
        console.log(q, s, g, len, res, gd);
        const h2 = document.createElement("h2");
        h2.appendChild(document.createTextNode("加权平均分(除了公选课):"+res));
        h2.setAttribute('style', 'color: red;');
        ttt.appendChild(h2);
        const dgh2 = document.createElement("h2");
        dgh2.appendChild(document.createTextNode("平均绩点(除了公选课):"+gd));
        dgh2.setAttribute('style', 'color: red;');
        ttt.appendChild(dgh2);
    }
    // Your code here...
})();