Schoolbook Tools

Mess around with schoolbook.ge

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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.3.1
// @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
// ==/UserScript==

(function () {
  "use strict";
  // do not change anything here if you don't know what you are doing!
  const settings = {
    // @ts-ignore
    changeGrade: GM_getValue("changeGrade"),
    // @ts-ignore
    changeAttendance: GM_getValue("changeAttendance"),
    // @ts-ignore
    liveGradeUpdate: GM_getValue("liveGradeUpdate"),
    // @ts-ignore
    gradeViewer: GM_getValue("gradeViewer"),
    // @ts-ignore
    hideComments: GM_getValue("hideComments"),
    // @ts-ignore
    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">(CLIENT SIDE) Change Grade:</label>
        <input type="text" id="changeGrade" value="${
          settings.changeGrade ?? ""
        }">
        <br>
        <label for="changeAttendance">(CLIENT SIDE) Change Attendance:</label>
        <input type="text" id="changeAttendance" value="${
          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">(CLIENT SIDE) Hide Comments:</label>
        <input type="checkbox" id="hideComments" ${
          settings.hideComments ? "checked" : ""
        }>
        <br>
        <a href='https://github.com/Naviamold1/schoolbook-filterlist'>Block Ads</a>
        <br>
        <br>
        <button data-close-modal id="saveSettingsButton">Save Settings</button>
        <button data-close-modal id="cancelSettingsButton">Cancel</button>
      </dialog>`);
  const sanitizeInput = (query) => {
    const inp = document.querySelector(query);
    if (inp && inp.value.trim() !== "") {
      return inp.value.trim();
    }
    return "";
  };
  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", () => {
      // @ts-ignore
      GM_setValue("changeGrade", sanitizeInput("#changeGrade"));
      // @ts-ignore
      GM_setValue("changeAttendance", sanitizeInput("#changeAttendance"));
      // @ts-ignore
      GM_setValue(
        "liveGradeUpdate",
        document.querySelector("#liveGradeUpdate").checked
      );
      // @ts-ignore
      GM_setValue(
        "gradeViewer",
        document.querySelector("#gradeViewer").checked
      );
      // @ts-ignore
      GM_setValue(
        "attendanceViewer",
        document.querySelector("#attendanceViewer").checked
      );
      // @ts-ignore
      GM_setValue(
        "hideComments",
        document.querySelector("#hideComments").checked
      );
      document.querySelector("#schoolbook_tools_settings_dialog").close();
      window.location.reload();
    });
  // Logic Functions
  const totalAvgGradePath = "#saertosashualo > span, .otherRow > span";
  const totalAvgAttendPath = ".sec span";
  function newGrade(grade) {
    let grades = document.querySelectorAll(`.avg_value, ${totalAvgGradePath}`);
    grades.forEach((val) => (val.textContent = grade));
  }
  function newAttendance(attendance) {
    let attendances = document.querySelectorAll(`.prc, ${totalAvgAttendPath}`);
    attendances.forEach((val) => (val.textContent = 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 && totalGrade.click();
    setTimeout(() => {
      // @ts-ignore
      closeGradeDialog();
      let numbers = [];
      document
        .querySelectorAll(
          "#cnt > div.div_container_grades > table > tbody > tr > td:nth-child(4)"
        )
        .forEach((el) => {
          let text = el.innerHTML || 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.textContent = avg.toString();
    }, 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;
      // @ts-ignore
      val && gradeclick(val, -1);
    };
    document.querySelector("#gmview").addEventListener("click", getter);
  }
  function attendanceSpier() {
    $("#pageBody")?.prepend(`
        <form id="gmSomeID2">
          <input placeholder="Attendance Viewer - User" list='datalistOptions' id="gminput2">
          <input placeholder="Subject ID" list='datalistOptionsAttend' id="gminput3">
          <button id="gmview2">View</button>
          <datalist id='datalistOptionsAttend'>
        </form>
        `);
    const getter = (e) => {
      e.preventDefault();
      let val = document.querySelector("#gminput2").value;
      let val2 = document.querySelector("#gminput3").value;
      // @ts-ignore
      val && val2 && attendanceclick(val, val2);
    };
    document.querySelector("#gmview2").addEventListener("click", getter);
  }
  const currentPage = window.location.pathname;
  if (
    currentPage === "/Parent/Index" ||
    currentPage === "/Parent/AllSubjects"
  ) {
    if (settings.changeGrade) {
      newGrade(settings.changeGrade);
    }
    if (settings.changeAttendance) {
      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}">`
            );
          });
          document.querySelectorAll(".sagani, more").forEach((val) => {
            $("#datalistOptionsAttend")?.append(
              `<option value="${val.id.replace("sagani_", "")}" label="${
                val.querySelector(".subject_name").innerText
              }">`
            );
          });
        } catch (error) {
          console.error(error);
        }
      };
      main();
      if (settings.gradeViewer) {
        gradeSpier();
      }
      if (settings.attendanceViewer) {
        attendanceSpier();
      }
    }
  }
  if (currentPage === "/Parent/Messages" && settings.hideComments) {
    removeComments();
  }
})();