GPACalculator

Calculate GPA on plus portals

目前為 2023-09-07 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GPACalculator
// @version      0.1
// @description  Calculate GPA on plus portals
// @author       Andrew Miller
// @match        https://plusportals.com/ParentStudentDetails/ParentStudentDetails/9754
// @grant        none
// @run-at       document-idle
// @license      MIT
// @namespace https://greasyfork.org/users/1167790
// ==/UserScript==

(function() {
    'use strict';

    const gpaWeights = {
  "A+": 4.33,
  "A": 4.17,
  "A-": 4.00,
  "B+": 3.67,
  "B": 3.33,
  "B-": 3.00,
  "C+": 2.67,
  "C": 2.33,
  "C-": 2.00,
  "D+": 1.67,
  "D": 1.33,
  "D-": 1.00,
  "F": 0.00
};
const checkElements = () => {
    const elements = document.querySelectorAll('[title="Subject"], [title="Grade"]');
    if(elements.length > 0) {
        clearInterval(intervalId);
        const subjectGradePairs = {};
        let currentSubject = '';
        let currentGrade = '';
        let totalGPA = 0;
        let elementCount = 0;

        elements.forEach(element => {
            const title = element.getAttribute('title');

            if (title === 'Subject') {
                currentSubject = element.textContent.trim();
            } else if (title === 'Grade') {
                currentGrade = element.textContent.trim();
                subjectGradePairs[currentSubject] = currentGrade;

                let gradeWeight = gpaWeights.hasOwnProperty(currentGrade) ? gpaWeights[currentGrade] : 0;

                if ((currentSubject.includes('Hnrs') || currentSubject.includes('Hon')) && gradeWeight >= 2.00) {
                    gradeWeight += 1;
                }

                totalGPA += gradeWeight;
                elementCount++;
            }
        });

        const averageGPA = elementCount > 0 ? (totalGPA / elementCount).toFixed(2) : 0;

        console.log("Average GPA:", averageGPA);

        function getLetterGrade(gpa) {
            let letterGrade = 'F'; // Default to F
            for (const [grade, weight] of Object.entries(gpaWeights)) {
                if (gpa >= weight) {
                    letterGrade = grade;
                    break;
                }
            }
            return letterGrade;
        }

        let tbody = document.querySelector('tbody[role="rowgroup"]');


        let newRow = document.createElement('tr');
        let newCell1 = document.createElement('td');
        let newCell2 = document.createElement('td');
        let newCell3 = document.createElement('td');


        newRow.setAttribute('role', 'row');
        newCell1.setAttribute('role', 'gridcell');
        newCell2.setAttribute('role', 'gridcell');
        newCell3.setAttribute('role', 'gridcell');


        newRow.setAttribute('role', 'row');
        newCell1.setAttribute('role', 'gridcell');
        newCell2.setAttribute('role', 'gridcell');
        newCell3.setAttribute('role', 'gridcell');

        newCell1.innerHTML = 'Total GPA';
        newCell2.innerHTML = averageGPA;
        newCell3.innerHTML = getLetterGrade(averageGPA);


        newRow.appendChild(newCell1);
        newRow.appendChild(newCell2);
        newRow.appendChild(newCell3);


        if (tbody.firstChild) {
            tbody.insertBefore(newRow, tbody.firstChild);
        } else {
            tbody.appendChild(newRow);
        }
    }
}
const intervalId = setInterval(checkElements, 750);
})();