WHU Gpa Calc

WHU Gpa Calculator which will remove unrelated courses and calculate your gpa automatically.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         WHU Gpa Calc
// @license      GPLV3
// @namespace    https://github.com/liaosunny123/WHUGpaCalc
// @version      1.0.0
// @description  WHU Gpa Calculator which will remove unrelated courses and calculate your gpa automatically.
// @author       EpicMo
// @match        https://jwgl.whu.edu.cn/cjcx/cjcx_cxDgXscj.html?gnmkdm=N305005*
// @require      https://code.jquery.com/jquery-3.6.1.min.js
// ==/UserScript==

(async function() {
    //void conflict with the default JQ of the page
    window.jQuery361 = $.noConflict(true)
    
    //get the archivement element group
    var classArrs = []
    var gpa = 0.0
    function gpaCalc(classArrs){
        var sum = 0.0
        var sumStandardScore = 0.0
        classArrs.forEach(sp => {
            if(sp.type == '通识教育选修'){
                return;
            }
            sum += parseFloat(sp.score) * parseFloat(sp.standardScore)
            sumStandardScore += parseFloat(sp.standardScore)
        })
        return sum / sumStandardScore
    }

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms))
    }

    var arrs = $('#tabGrid').children().children('.ui-row-ltr')
    //await sleep(5000)

    async function getScore(){
        var count = 0
        while(count <= 21){
            if(arrs.length == 0){
                await sleep(500)
                arrs = $('#tabGrid').children().children('.ui-row-ltr')
                count++
            }else{
                arrs = $('#tabGrid').children().children('.ui-row-ltr')
                break
            }
        }

        if(count == 21){
            return 4.0
        }else{
            $.each(arrs, function(index, value){
                var cell = $(value).children()
                var classArr = {
                    classNo: $(cell[3]).text(),
                    className: $(cell[4]).text(),
                    standardScore: $(cell[6]).text(),
                    score: $(cell[9]).text(),
                    type: $(cell[5]).text()
                }
                Object.assign(classArrs, {[index]: classArr})
            })
            return gpaCalc(classArrs)
        }
    }
    
    gpa = (await getScore())

    console.log('GPA Calc: ' + gpa)

    var span = $('<span>有效绩点:' + gpa.toFixed(2) +' </span>')
    $('.col-sm-2').children().before(span)

    document.getElementById('search_go').addEventListener('click', async function(){
        console.log('GPA Calc: ' + (await getScore()))
        $('.col-sm-2').children().eq(0).text('有效绩点:' + (await getScore()).toFixed(2) + ' ')
    })
})()