Skip to content

Commit

Permalink
przegladaj_oceny revisited
Browse files Browse the repository at this point in the history
  • Loading branch information
docentYT committed Sep 23, 2023
1 parent bf0834b commit 7ffe55a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 92 deletions.
29 changes: 17 additions & 12 deletions extensions/combined/src/przegladaj_oceny/Grade.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";
class Grade {
constructor(value, weight, countToAverage) {
this.value = value;
Expand All @@ -8,7 +9,7 @@ class Grade {

function parseGradeFromHtmlObject(html, plusValue, minusValue, tylkoLiczDoSredniej) {
function parseWeight(text) {
weight = parseInt(text[6]);
const weight = parseInt(text[6]);
if (isNaN(weight)) return 1;
return weight;
};
Expand All @@ -19,19 +20,21 @@ function parseGradeFromHtmlObject(html, plusValue, minusValue, tylkoLiczDoSredni
};

function parseValue(text, plusValue, minusValue) {
value = parseInt(text);
let value = parseInt(text[0]);
if (isNaN(value)) return text;
if (text[1] == '+') value += plusValue; // grade with '+'
else if (text[1] == '-') value -= minusValue; // grade with '-'
if (text.length == 2) {
if (text[1] == '+') value += plusValue; // grade with '+'
else if (text[1] == '-') value -= minusValue; // grade with '-'
}
return value;
};

title = html.getAttribute("title");
titleArray = title.split("<br>");
const title = html.getAttribute("title");
const titleArray = title.split("<br>");

value = parseValue(html.innerText, plusValue, minusValue);
weight = 1;
countToAverage = false;
const value = parseValue(html.innerText, plusValue, minusValue);
let weight = 1;
let countToAverage = false;
for (const text of titleArray) {
if (text.includes("Waga: ")) weight = parseWeight(text);
else if (text.includes("Licz do średniej:")) countToAverage = parseCountToAverage(text, tylkoLiczDoSredniej);
Expand All @@ -41,13 +44,15 @@ function parseGradeFromHtmlObject(html, plusValue, minusValue, tylkoLiczDoSredni


function gradesTdToList(gradesTd, plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades) {
list = [];
let list = [];

let grades = gradesTd.children;
const grades = gradesTd.children;
for (const gradeGroup of grades) {
const gradesInGradeGroup = gradeGroup.children;
if (gradesInGradeGroup[0].tagName == "SPAN") {
if (ignoreCorrectedGrades) list.push(parseGradeFromHtmlObject(gradesInGradeGroup[gradesInGradeGroup.length-1].getElementsByTagName("a")[0]));
if (ignoreCorrectedGrades) {
list.push(parseGradeFromHtmlObject(gradesInGradeGroup[gradesInGradeGroup.length-1].getElementsByTagName("a")[0], plusValue, minusValue, tylkoLiczDoSredniej));
}
else {
for (const grade of gradesInGradeGroup) {
list.push(parseGradeFromHtmlObject(grade.getElementsByTagName("a")[0], plusValue, minusValue, tylkoLiczDoSredniej));
Expand Down
42 changes: 40 additions & 2 deletions extensions/combined/src/przegladaj_oceny/Subject.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
class Subject {
constructor(name, gradesFirstList, gradesSecondList) {
this.name = name;
this.gradesFirst = gradesFirstList;
this.gradesSecond = gradesSecondList;
this._gradesFirst = gradesFirstList;
this._gradesSecond = gradesSecondList;
this.updateAverage();
};

get gradesFirst() {
return this._gradesFirst;
}

set gradesFirst(grades) {
this._gradesFirst = grades;
updateAverage();
}

get gradesSecond() {
return this._gradesSecond;
}

set gradesSecond(grades) {
this._gradesSecond = grades;
updateAverage();

This comment has been minimized.

Copy link
@EnterVPL

EnterVPL Sep 27, 2023

Nie wiem czy w VaniliaJS to jest możliwe, ja bym zrobił dekorator, który dałoby się ustawiać nad setterami. Zadaniem dekoratora byłoby mówienie które settery wywołują konkretną metodę w tym przypadku updateAverage. Jeśli nie ma dekoratorów no to to olej.

This comment has been minimized.

Copy link
@docentYT
}

updateAverage() {
this.averageFirstTerm = Subject.average(this.gradesFirst);
this.averageSecondTerm = Subject.average(this.gradesSecond);
this.averageYear = Subject.average(this.gradesFirst.concat(this.gradesSecond));
}

static average(gradesList) {
let sum = 0;
let counter = 0;
if (gradesList.length == 0) return (0).toFixed(2);
for (const grade of gradesList) {
if (grade.countToAverage && !isNaN(grade.value)) {
sum += (grade.value * grade.weight);
counter += grade.weight;
}
}
return (sum/counter).toFixed(2);
};
};

Expand Down
42 changes: 20 additions & 22 deletions extensions/combined/src/przegladaj_oceny/generateTdsIndexes.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
"use strict";
const Utils = require("../../utils");
import {getTopLevelChildByTagName} from "../../utils"

var wasGrades = false;
function grades() {
if (wasGrades) return "gradesSecondTerm";
wasGrades = true;
return "gradesFirstTerm";
}
const mapColumnHeadersToTdsIndexes = {
"Oceny bieżące": grades,
"Śr.I": "averageFirstTerm",
"(I)": "annualFirstPredirect",
"I": "annualFirst",
"Śr.II": "averageSecondTerm",
"(II)": "annualSecondPredirect",
"II": "annualSecond",
"Śr.R": "averageYear",
"(R)": "annualYearPredirect",
"R": "annualYear"
}

const mapColumnHeadersToTdsIndexes = new Map([
["Oceny bieżące", grades],
["Śr.I", "averageFirstTerm"],
["(I)", "annualFirstPredirect"],
["I", "annualFirst"],
["Śr.II", "averageSecondTerm"],
["(II)", "annualSecondPredirect"],
["II", "annualSecond"],
["Śr.R", "averageYear"],
["(R)", "annualYearPredirect"],
["R", "annualYear"]
])

export function generateTdsIndexes(table) {
let tdsIndexes = {"subjectName": 1};
let tdsIndexes = new Map([["subjectName", 1]]);

let tableHead = Utils.getTopLevelChildByTagName(table, "thead");
let rowWithDescriptions = tableHead.children[1]; // Second row with column descriptions: "oceny bieżące", "Śr. I" etc.
const tableHead = getTopLevelChildByTagName(table, "thead");
const rowWithDescriptions = tableHead.children[1]; // Second row with column descriptions: "oceny bieżące", "Śr. I" etc.
let array = Array.from(rowWithDescriptions.children);

let counter = 2;
for (const column of array) {
let mappedKey = mapColumnHeadersToTdsIndexes[column.innerText];
if (typeof mappedKey === "function") {
tdsIndexes[mappedKey()] = counter;
} else {
tdsIndexes[mappedKey] = counter;
}
const mappedKey = mapColumnHeadersToTdsIndexes.get(column.innerText);
const key = typeof mappedKey === "function" ? mappedKey() : mappedKey;
tdsIndexes.set(key, counter);
counter++;
}

Expand Down
106 changes: 50 additions & 56 deletions extensions/combined/src/przegladaj_oceny/srednia.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";
const Grade = require("./Grade");
const Subject = require("./Subject");
const Utils = require("../../utils");
const GenerateTdsIndexes = require("./generateTdsIndexes");

var tdsIndexes;
import Subject from "./Subject";
import { getTopLevelChildByTagName } from "../../utils";
import { generateTdsIndexes } from "./generateTdsIndexes";

function average(gradesList) {
let sum = 0;
Expand All @@ -19,78 +17,78 @@ function average(gradesList) {
return (sum/counter).toFixed(2);
};

function generateFooter(tfoot, subjects, annuals) {
function generateFooter(tfoot, tdsIndexes, subjects, annuals) {
function generateTd(innerText, title) {
let td = document.createElement("td");
td.setAttribute("colspan", 1);
if (title) td.setAttribute("title", title);
td.innerText = innerText || null;
tr.appendChild(td);
}
var tr = Utils.getTopLevelChildByTagName(tfoot, "tr");
Utils.getTopLevelChildByTagName(tr, "td").remove();
let tr = getTopLevelChildByTagName(tfoot, "tr");
getTopLevelChildByTagName(tr, "td").remove();

const {gradesFirst, gradesSecond} = Grade.gradesFromSubjects(subjects);

generateTd(); // Spacing
generateTd("Średnie", "Średnie wyliczone dzięki wtyczce Librus Średnia wykonanej przez https://docentcompany.com");

if (tdsIndexes.hasOwnProperty("gradesFirstTerm")) generateTd();
if (tdsIndexes.hasOwnProperty("averageFirstTerm")) generateTd(average(gradesFirst), "Średnia ocen z pierwszego semestu ze wszystkich przedmiotów.");
if (tdsIndexes.hasOwnProperty("annualFirstPredirect")) generateTd(annuals["annualFirstPredirect"], "Średnia ocen przewidywanych na pierwszy semestr.");
if (tdsIndexes.hasOwnProperty("annualFirst")) generateTd(annuals["annualFirst"], "Średnia ocen śródrocznych na pierwszy semestr.");
if (tdsIndexes.get("gradesFirstTerm")) generateTd();
if (tdsIndexes.get("averageFirstTerm")) generateTd(average(gradesFirst), "Średnia ocen z pierwszego semestu ze wszystkich przedmiotów.");
if (tdsIndexes.get("annualFirstPredirect")) generateTd(annuals["annualFirstPredirect"], "Średnia ocen przewidywanych na pierwszy semestr.");
if (tdsIndexes.get("annualFirst")) generateTd(annuals["annualFirst"], "Średnia ocen śródrocznych na pierwszy semestr.");

if (tdsIndexes.hasOwnProperty("gradesSecondTerm")) generateTd();
if (tdsIndexes.hasOwnProperty("averageSecondTerm")) generateTd(average(gradesSecond), "Średnia ocen z drugiego semestu ze wszystkich przedmiotów.");
if (tdsIndexes.hasOwnProperty("annualSecondPredirect")) generateTd(annuals["annualSecondPredirect"], "Średnia ocen przewidywanych na drugi semestr.");
if (tdsIndexes.hasOwnProperty("annualSecond")) generateTd(annuals["annualSecond"], "Średnia ocen śródrocznych na drugi semestr.");
if (tdsIndexes.get("gradesSecondTerm")) generateTd();
if (tdsIndexes.get("averageSecondTerm")) generateTd(average(gradesSecond), "Średnia ocen z drugiego semestu ze wszystkich przedmiotów.");
if (tdsIndexes.get("annualSecondPredirect")) generateTd(annuals["annualSecondPredirect"], "Średnia ocen przewidywanych na drugi semestr.");
if (tdsIndexes.get("annualSecond")) generateTd(annuals["annualSecond"], "Średnia ocen śródrocznych na drugi semestr.");

if (tdsIndexes.hasOwnProperty("averageYear")) generateTd(average(gradesFirst.concat(gradesSecond)), "Średnia ocen z pierwszego i drugiego semestu łącznie ze wszystkich przedmiotów.");
if (tdsIndexes.hasOwnProperty("annualYearPredirect")) generateTd(annuals["annualYearPredirect"], "Średnia ocen przewidywanych na koniec roku.");
if (tdsIndexes.hasOwnProperty("annualYear")) generateTd(annuals["annualYear"], "Średnia ocen rocznych.");
if (tdsIndexes.get("averageYear")) generateTd(average(gradesFirst.concat(gradesSecond)), "Średnia ocen z pierwszego i drugiego semestu łącznie ze wszystkich przedmiotów.");
if (tdsIndexes.get("annualYearPredirect")) generateTd(annuals["annualYearPredirect"], "Średnia ocen przewidywanych na koniec roku.");
if (tdsIndexes.get("annualYear")) generateTd(annuals["annualYear"], "Średnia ocen rocznych.");
};

function updateAverage(tds, subject) {
function updadeAverageForAnnual(tdsIndexKey, grades, title) {
if (!tdsIndexes.hasOwnProperty(tdsIndexKey)) return;
const td = tds[tdsIndexes[tdsIndexKey]];
td.textContent = average(grades);
function updateAverage(tds, tdsIndexes, subject) {
function updadeAverageForAnnual(tdsIndexKey, average, title) {
if (!tdsIndexes.get(tdsIndexKey)) return;
let td = tds[tdsIndexes.get(tdsIndexKey)];
td.textContent = average;
td.setAttribute("title", title);
td.setAttribute("class", "center");
}
updadeAverageForAnnual("averageFirstTerm", subject.gradesFirst, "Średnia ocen z pierwszego semestu z jednego przedmiotu.");
updadeAverageForAnnual("averageSecondTerm", subject.gradesSecond, "Średnia ocen z drugiego semestu z jednego przedmiotu.");
updadeAverageForAnnual("averageYear", subject.gradesFirst.concat(subject.gradesSecond), "Średnia ocen z pierwszego i drugiego semesteru łącznie z jednego przedmiotu.");
updadeAverageForAnnual("averageFirstTerm", subject.averageFirstTerm, "Średnia ocen z pierwszego semestu z jednego przedmiotu.");
updadeAverageForAnnual("averageSecondTerm", subject.averageSecondTerm, "Średnia ocen z drugiego semestu z jednego przedmiotu.");
updadeAverageForAnnual("averageYear", subject.averageYear, "Średnia ocen z pierwszego i drugiego semesteru łącznie z jednego przedmiotu.");
};

function generateSubjectListFromGradesTableBody(tbody, plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades) {
function generateSubjectListFromGradesTableBody(tbody, tdsIndexes, plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades) {
let subjectList = [];
for (const subject of tbody.children) {
if (subject.hasAttribute("name")) continue;
let tds = subject.getElementsByTagName("td");
let subjectName = tds[tdsIndexes.subjectName].textContent;
if (subject.hasAttribute("name")) continue; // if it has name it is a table that can be expanded by clicking '+' icon.
const tds = subject.getElementsByTagName("td");
const subjectName = tds[tdsIndexes.get("subjectName")].textContent;
if (subjectName.includes("Zachowanie")) continue;

let gradesFirstList = Grade.gradesTdToList(tds[tdsIndexes.gradesFirstTerm], plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades);
let gradesSecondList = Grade.gradesTdToList(tds[tdsIndexes.gradesSecondTerm], plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades);
let subjectObject = new Subject(subjectName, gradesFirstList, gradesSecondList);
const gradesFirstList = Grade.gradesTdToList(tds[tdsIndexes.get("gradesFirstTerm")], plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades);
const gradesSecondList = Grade.gradesTdToList(tds[tdsIndexes.get("gradesSecondTerm")], plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades);
const subjectObject = new Subject(subjectName, gradesFirstList, gradesSecondList);

updateAverage(tds, subjectObject);
updateAverage(tds, tdsIndexes, subjectObject);

subjectList.push(new Subject(subjectName, gradesFirstList, gradesSecondList));
};
return subjectList;
};

function annualAssements(tbody) {
function annualAssements(tbody, tdsIndexes) {
let annuals = {};

function parseAnnual(tds, tdsIndex) {
let grade = tds[tdsIndex];
if (!grade) return null;
grade = Utils.getTopLevelChildByTagName(grade, "span");
grade = getTopLevelChildByTagName(grade, "span");
if (!grade) return null;
grade = Utils.getTopLevelChildByTagName(grade, "a");
grade = getTopLevelChildByTagName(grade, "a");
if (!grade || !grade.textContent) return null;
return parseInt(grade.textContent);
}
Expand All @@ -110,7 +108,7 @@ function annualAssements(tbody) {
for (const subject of tbody.children) {
if (subject.hasAttribute("name")) continue;
let tds = subject.getElementsByTagName("td");
let subjectName = tds[tdsIndexes.subjectName].textContent;
let subjectName = tds[tdsIndexes.get("subjectName")].textContent;
if (subjectName.includes("Zachowanie")) continue;

let annualFirstPredirectGrade = parseAnnual(tds, tdsIndexes.annualFirstPredirect);
Expand Down Expand Up @@ -160,23 +158,19 @@ function annualAssements(tbody) {
}

async function main() {
let table = document.getElementsByClassName("decorated stretch")[1];
let tbody = Utils.getTopLevelChildByTagName(table, "tbody");

tdsIndexes = GenerateTdsIndexes.generateTdsIndexes(table);

let plusValue;
let minusValue;
let tylkoLiczDoSredniej;
let ignoreCorrectedGrades;

await chrome.storage.sync.get(["plus"]).then((result) => {plusValue = result.plus ?? 0.5});
await chrome.storage.sync.get(["minus"]).then((result) => {minusValue = result.minus ?? 0.25});
await chrome.storage.sync.get(["tylkoLiczDoSredniej"]).then((result) => {tylkoLiczDoSredniej = result.tylkoLiczDoSredniej ?? true});
await chrome.storage.sync.get(["ignoreCorrectedGrades"]).then((result) => {ignoreCorrectedGrades = result.ignoreCorrectedGrades ?? true})
let subjectList = await generateSubjectListFromGradesTableBody(tbody, plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades);
let annuals = annualAssements(tbody);
generateFooter(Utils.getTopLevelChildByTagName(table, "tfoot"), subjectList, annuals);
const table = document.getElementsByClassName("decorated stretch")[1];
const tbody = getTopLevelChildByTagName(table, "tbody");

const tdsIndexes = generateTdsIndexes(table);

const plusValue = await chrome.runtime.sendMessage({ "message": "getSetting", "key": "plus" });
const minusValue = await chrome.runtime.sendMessage({ "message": "getSetting", "key": "minus" });
const tylkoLiczDoSredniej = await chrome.runtime.sendMessage({ "message": "getSetting", "key": "tylkoLiczDoSredniej" });
const ignoreCorrectedGrades = await chrome.runtime.sendMessage({ "message": "getSetting", "key": "ignoreCorrectedGrades" });

let subjectList = generateSubjectListFromGradesTableBody(tbody, tdsIndexes, plusValue, minusValue, tylkoLiczDoSredniej, ignoreCorrectedGrades);
let annuals = annualAssements(tbody, tdsIndexes);
generateFooter(getTopLevelChildByTagName(table, "tfoot"), tdsIndexes, subjectList, annuals);
};

main();

0 comments on commit 7ffe55a

Please sign in to comment.