Schoolbook Tools

Mess around with schoolbook.ge

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

// ==UserScript==
// @name         Schoolbook Tools
// @description  Mess around with schoolbook.ge
// @icon         https://eservices.schoolbook.ge/Images/sb-logo-blue.png
// @author       Naviamold
// @license      MIT
// @version      2.1.2
// @namespace    https://github.com/naviamold1
// @homepage     https://greasyfork.org/en/scripts/459858-schoolbook-tools
// @match        *://*.schoolbook.ge/*
// @exclude      *://schoolbook.ge/*
// @grant        GM_getValue
// @grant        GM_setValue
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require      https://greasyfork.org/scripts/476008-waitforkeyelements-gist-port/code/waitforkeyelements%20gist%20port+.js
// ==/UserScript==

(function () {
  "use strict";

  // do not change anything here if you don't know what you are doing!

  const settings = {
    changeGrade: GM_getValue("changeGrade"),
    changeAttendance: GM_getValue("changeAttendance"),
    liveGradeUpdate: GM_getValue("liveGradeUpdate"),
    gradeViewer: GM_getValue("gradeViewer"),
    hideComments: GM_getValue("hideComments"),
    attendanceViewer: GM_getValue("attendanceViewer"),
  };

  // Add a button to open the settings dialog
  $("#pageBody").prepend(
    `<button data-open-modal id="openSettingsButton">Open Settings</button>
  
      <dialog data-modal id="schoolbook_tools_settings_dialog">
        <h2>Script Settings</h2>
        <label for="changeGrade">Change Grade:</label>
        <input type="text" id="changeGrade" value="${
          settings.changeGrade === "null" || "undefined"
            ? null
            : settings.changeGrade
        }">
        <br>
        <label for="changeAttendance">Change Attendance:</label>
        <input type="text" id="changeAttendance" value="${
          settings.changeAttendance === "null" || "undefined"
            ? null
            : settings.changeAttendance
        }">
        <br>
        <label for="liveGradeUpdate">Live Grade Update:</label>
        <input type="checkbox" id="liveGradeUpdate" ${
          settings.liveGradeUpdate ? "checked" : ""
        }>
        <br>
        <label for="gradeViewer">Grade Viewer:</label>
        <input type="checkbox" id="gradeViewer" ${
          settings.gradeViewer ? "checked" : ""
        }>
        <br>
        <label for="attendanceViewer">Attendance Viewer:</label>
        <input type="checkbox" id="attendanceViewer" ${
          settings.attendanceViewer ? "checked" : ""
        }>
        <br>
        <label for="hideComments">Hide Comments:</label>
        <input type="checkbox" id="hideComments" ${
          settings.hideComments ? "checked" : ""
        }>
        <br>
        <button data-close-modal id="saveSettingsButton">Save Settings</button>
        <button data-close-modal id="cancelSettingsButton">Cancel</button>
      </dialog>`
  );

  const dialog = document.querySelector("#schoolbook_tools_settings_dialog");
  document
    .querySelector("#openSettingsButton")
    .addEventListener("click", () => dialog.showModal());

  document
    .querySelector("#cancelSettingsButton")
    .addEventListener("click", () => dialog.close());

  document
    .querySelector("#saveSettingsButton")
    .addEventListener("click", () => {
      GM_setValue(
        "changeGrade",
        document.querySelector("#changeGrade").value === "null" || "undefined"
          ? null
          : document.querySelector("#changeGrade").value
      );
      GM_setValue(
        "changeAttendance",
        document.querySelector("#changeAttendance").value === "null" ||
          "undefined"
          ? null
          : document.querySelector("#changeAttendance").value
      );
      GM_setValue(
        "liveGradeUpdate",
        document.querySelector("#liveGradeUpdate").checked
      );
      GM_setValue(
        "gradeViewer",
        document.querySelector("#gradeViewer").checked
      );
      GM_setValue(
        "attendanceViewer",
        document.querySelector("#attendanceViewer").checked
      );
      GM_setValue(
        "hideComments",
        document.querySelector("#hideComments").checked
      );
      document.querySelector("#schoolbook_tools_settings_dialog").close();
      window.location.reload();
    });

  // Logic Functions

  const totalAvgGradePath = "#leftUnderPicture > div:nth-child(9) > span";
  const totalAvgAttendPath = ".sec span";

  function newGrade(grade) {
    let grades = document.querySelectorAll($`.avg_value, ${totalAvgGradePath}`);
    grades.forEach((val) => (val.innerHTML = grade));
  }

  function newAttendance(attendance) {
    let attendances = document.querySelectorAll(`.prc, ${totalAvgAttendPath}`);
    attendances.forEach((val) => (val.innerHTML = attendance));
  }

  function removeComments() {
    let comments = document.querySelectorAll(
      ".notificationsList, .notificationsListalter, .homeworkContent"
    );
    comments.forEach((mes) => (mes.style.display = "none"));
  }

  function liveUpdate() {
    let totalGrade = document.querySelector("#saertosashualo span");
    totalGrade.click();
    setTimeout(() => {
      closeGradeDialog();
      let numbers = [];
      document
        .querySelectorAll(
          "#cnt > div.div_container_grades > table > tbody > tr > td:nth-child(4)"
        )
        .forEach((el) => {
          let text = el.innerText || el.textContent;
          let number = parseInt(text.replace(/\D/g, ""));
          if (!isNaN(number)) {
            numbers.push(number);
          }
        });

      let sum = numbers.reduce((acc, val) => acc + val, 0);

      let avg = 0;
      if (numbers.length > 0) {
        avg = sum / numbers.length;
      }

      avg = Math.round(avg * 100) / 100;

      totalGrade.innerHTML = avg;
    }, 2000);
  }

  function gradeSpier() {
    $("#pageBody").prepend(`
        <form id="gmSomeID">
          <input placeholder="Grade Viewer - User" list='datalistOptions' id="gminput">
          <button id="gmview">View</button>
          <datalist id='datalistOptions'>
        </form>
        `);
    const getter = (e) => {
      e.preventDefault();
      let val = document.querySelector("#gminput").value;
      gradeclick(val, -1);
    };
    $("#gmview").click(getter);
  }

  function attendanceSpier() {
    $("#pageBody").prepend(`
        <form id="gmSomeID2">
          <input placeholder="Attendance Viewer - User" list='datalistOptions' id="gminput2">
          <input placeholder="Subject ID" id="gminput3">
          <button id="gmview2">View</button>
        </form>
        `);

    const getter = (e) => {
      e.preventDefault();
      let val = document.querySelector("#gminput2").value;
      let val2 = document.querySelector("#gminput3").value;
      attendanceclick(val, val2);
    };
    $("#gmview2").click(getter);
  }

  const currentPage = window.location.pathname;
  if (
    currentPage === "/Parent/Index" ||
    currentPage === "/Parent/AllSubjects"
  ) {
    if (settings.changeGrade) {
      waitForKeyElements(totalAvgGradePath, () =>
        newGrade(settings.changeGrade)
      );
    }

    if (settings.changeAttendance) {
      waitForKeyElements(totalAvgAttendPath, () =>
        newAttendance(settings.changeAttendance)
      );
    }

    if (settings.liveGradeUpdate) {
      liveUpdate();
    }

    if (settings.attendanceViewer || settings.gradeViewer) {
      const main = async () => {
        try {
          const options = {
            method: "POST",
            headers: {
              accept: "application/json, text/javascript, */*; q=0.01",
              "content-type": "application/json",
            },
            body: '{"pageSize":1000,"filter":"","initValue":null}',
          };

          const req = await fetch(
            "https://eservices.schoolbook.ge/SchoolBook/SchoolPersonsList",
            options
          );
          const res = await req.json();
          res["mas"].forEach((val) => {
            $("#datalistOptions").append(
              `<option value="${val.value}" label="${val.text}">`
            );
          });
        } catch (error) {
          console.error(error);
        }
      };
      main();

      if (settings.gradeViewer) {
        gradeSpier();
      }

      if (settings.attendanceViewer) {
        attendanceSpier();
      }
    }
  }

  if (currentPage === "/Parent/Messages" && settings.hideComments) {
    removeComments();
  }
})();