average vulcan

calculate average grades in vulcan

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         average vulcan
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  calculate average grades in vulcan
// @author       bewu
// @license      MIT
// @match        https://uonetplus-uczen.vulcan.net.pl/*
// @icon         https://cdn-icons-png.flaticon.com/512/5084/5084428.png
// @grant        none
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// ==/UserScript==

(function() {
    'use strict';
    console.info("average vulcan by bewu");

    var subjectsDiv = $("#ext-element-110");
    var waitGrades = setInterval(function() {
        if (subjectsDiv.length != 0 && subjectsDiv[0].childElementCount != 0) {
            console.info("loaded grades!");
            clearInterval(waitGrades);

            start(subjectsDiv[0]);
        }
        else {
            subjectsDiv = $("#ext-element-110");
        }
    }, 100);
})();

function start(subjects) {
    console.info(subjects);
    for (var i = 0; i < subjects.childElementCount; i++) {
        avg(subjects.children[i], subjects.getElementsByClassName("x-label-el")[i]);
    }
}

function avg(s, sName) {
    s = s.querySelectorAll("span[aria-label]");

    if (s.length != 0) { // if there are any grades
        console.info(sName.innerText);
        var gSum = 0.0;
        var weightSum = 0;

        for (var i = 0; i < s.length; i++) {
            var tempG = s[i].innerText.replace(" (%)", "") // temp string to hold grade
            if (tempG.length > 1) { // if grade is not only + or -
                tempG = tempG.replace("+", ".5");

                if (tempG.includes("-")) { // handling grades with a -
                    tempG = tempG.replace("-", ".0");
                    if (!isNaN(tempG)) {
                        tempG = (parseInt(tempG) - 0.25).toString();
                    }
                }
            }

            if (!isNaN(tempG)) { // if the grade is numeric
                var tempW = s[i].getAttribute("aria-label").split("waga: ")[1].split(",")[0]; // temp string to hold grade's weight

                if (tempW != "0") { // if weight is not 0
                    gSum += parseInt(tempW) * parseFloat(tempG);
                    weightSum += parseInt(tempW);

                    console.info(tempG + " " + tempW);
                }
            }
        }

        var sAverage;
        if (weightSum > 0) { // to avoid dividing by 0
            sAverage = Math.round(gSum / weightSum * 100) / 100; // calculating the average

            console.info(sName.innerText + " " + sAverage);
            sName.innerHTML = sName.innerText + "<i> (" + sAverage + ")</i>";
        }
    }
}