average vulcan

calculate average grades in vulcan

  1. // ==UserScript==
  2. // @name average vulcan
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description calculate average grades in vulcan
  6. // @author bewu
  7. // @license MIT
  8. // @match https://uonetplus-uczen.vulcan.net.pl/*
  9. // @icon https://cdn-icons-png.flaticon.com/512/5084/5084428.png
  10. // @grant none
  11. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. console.info("average vulcan by bewu");
  17.  
  18. var subjectsDiv = $("#ext-element-110");
  19. var waitGrades = setInterval(function() {
  20. if (subjectsDiv.length != 0 && subjectsDiv[0].childElementCount != 0) {
  21. console.info("loaded grades!");
  22. clearInterval(waitGrades);
  23.  
  24. start(subjectsDiv[0]);
  25. }
  26. else {
  27. subjectsDiv = $("#ext-element-110");
  28. }
  29. }, 100);
  30. })();
  31.  
  32. function start(subjects) {
  33. console.info(subjects);
  34. for (var i = 0; i < subjects.childElementCount; i++) {
  35. avg(subjects.children[i], subjects.getElementsByClassName("x-label-el")[i]);
  36. }
  37. }
  38.  
  39. function avg(s, sName) {
  40. s = s.querySelectorAll("span[aria-label]");
  41.  
  42. if (s.length != 0) { // if there are any grades
  43. console.info(sName.innerText);
  44. var gSum = 0.0;
  45. var weightSum = 0;
  46.  
  47. for (var i = 0; i < s.length; i++) {
  48. var tempG = s[i].innerText.replace(" (%)", "") // temp string to hold grade
  49. if (tempG.length > 1) { // if grade is not only + or -
  50. tempG = tempG.replace("+", ".5");
  51.  
  52. if (tempG.includes("-")) { // handling grades with a -
  53. tempG = tempG.replace("-", ".0");
  54. if (!isNaN(tempG)) {
  55. tempG = (parseInt(tempG) - 0.25).toString();
  56. }
  57. }
  58. }
  59.  
  60. if (!isNaN(tempG)) { // if the grade is numeric
  61. var tempW = s[i].getAttribute("aria-label").split("waga: ")[1].split(",")[0]; // temp string to hold grade's weight
  62.  
  63. if (tempW != "0") { // if weight is not 0
  64. gSum += parseInt(tempW) * parseFloat(tempG);
  65. weightSum += parseInt(tempW);
  66.  
  67. console.info(tempG + " " + tempW);
  68. }
  69. }
  70. }
  71.  
  72. var sAverage;
  73. if (weightSum > 0) { // to avoid dividing by 0
  74. sAverage = Math.round(gSum / weightSum * 100) / 100; // calculating the average
  75.  
  76. console.info(sName.innerText + " " + sAverage);
  77. sName.innerHTML = sName.innerText + "<i> (" + sAverage + ")</i>";
  78. }
  79. }
  80. }