Kepler Script

Kepler için notlar

当前为 2025-01-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Kepler Script
  3. // @author KazuroAkashi
  4. // @match https://obs.itu.edu.tr/ogrenci/
  5. // @license MIT
  6. // @version 0.0.1.20250107074256
  7. // @namespace https://greasyfork.org/users/1419483
  8. // @description Kepler için notlar
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. async function getJWT() {
  15. return new Promise((resolve, reject) => {
  16. const xhr = new XMLHttpRequest();
  17. xhr.open("GET", "https://obs.itu.edu.tr/ogrenci/auth/jwt");
  18.  
  19. xhr.onload = () => {
  20. if (xhr.readyState == 4 && xhr.status == 200) {
  21. resolve(xhr.responseText);
  22. } else {
  23. reject(xhr.status);
  24. }
  25. };
  26. xhr.send();
  27. })
  28. }
  29.  
  30. async function getDonemListesi(jwt) {
  31. return new Promise((resolve, reject) => {
  32. const xhr = new XMLHttpRequest();
  33. xhr.open("GET", "https://obs.itu.edu.tr/api/ogrenci/DonemListesi");
  34. xhr.setRequestHeader("Authorization", "Bearer " + jwt);
  35.  
  36. xhr.onload = () => {
  37. if (xhr.readyState == 4 && xhr.status == 200) {
  38. resolve(JSON.parse(xhr.response));
  39. } else {
  40. reject(xhr.status);
  41. }
  42. };
  43. xhr.send();
  44. })
  45. }
  46.  
  47. async function getSinifListesi(jwt, donemId) {
  48. return new Promise((resolve, reject) => {
  49. const xhr = new XMLHttpRequest();
  50. xhr.open("GET", "https://obs.itu.edu.tr/api/ogrenci/sinif/KayitliSinifListesi/" + donemId);
  51. xhr.setRequestHeader("Authorization", "Bearer " + jwt);
  52.  
  53. xhr.onload = () => {
  54. if (xhr.readyState == 4 && xhr.status == 200) {
  55. resolve(JSON.parse(xhr.response));
  56. } else {
  57. reject(xhr.status);
  58. }
  59. };
  60. xhr.send();
  61. })
  62. }
  63.  
  64. async function getHarfNotuListesi(jwt, donemId) {
  65. return new Promise((resolve, reject) => {
  66. const xhr = new XMLHttpRequest();
  67. xhr.open("GET", "https://obs.itu.edu.tr/api/ogrenci/Sinif/SinifHarfNotuListesi/" + donemId);
  68. xhr.setRequestHeader("Authorization", "Bearer " + jwt);
  69.  
  70. xhr.onload = () => {
  71. if (xhr.readyState == 4 && xhr.status == 200) {
  72. const arr = JSON.parse(xhr.response).sinifHarfNotuResultList;
  73. const obj = {};
  74.  
  75. for (const not of arr) {
  76. obj[not.crn] = not.harfNotu;
  77. }
  78.  
  79. resolve(obj);
  80. } else {
  81. reject(xhr.status);
  82. }
  83. };
  84. xhr.send();
  85. })
  86. }
  87.  
  88. async function getNotListesi(jwt, sinifId) {
  89. return new Promise((resolve, reject) => {
  90. const xhr = new XMLHttpRequest();
  91. xhr.open("GET", "https://obs.itu.edu.tr/api/ogrenci/Sinif/SinifDonemIciNotListesi/" + sinifId);
  92. xhr.setRequestHeader("Authorization", "Bearer " + jwt);
  93.  
  94. xhr.onload = () => {
  95. if (xhr.readyState == 4 && xhr.status == 200) {
  96. resolve(JSON.parse(xhr.response));
  97. } else {
  98. reject(xhr.status);
  99. }
  100. };
  101. xhr.send();
  102. })
  103. }
  104.  
  105. String.prototype.formatStr = String.prototype.formatStr ||
  106. function () {
  107. "use strict";
  108. var str = this.toString();
  109. if (arguments.length) {
  110. var t = typeof arguments[0];
  111. var key;
  112. var args = ("string" === t || "number" === t) ?
  113. Array.prototype.slice.call(arguments)
  114. : arguments[0];
  115.  
  116. for (key in args) {
  117. str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), args[key]);
  118. }
  119. }
  120.  
  121. return str;
  122. };
  123.  
  124. const htmlParser = new DOMParser();
  125. function createHTMLElement(str) {
  126. const doc = htmlParser.parseFromString(str, "text/html");
  127. return doc.body.firstChild;
  128. }
  129.  
  130. function insertBeforeHTMLElement(str, el) {
  131. const insert = createHTMLElement(str);
  132. el.parentElement.insertBefore(insert, el);
  133. }
  134.  
  135. const newOptionTemplate = `
  136. <option value="{id}">{name}</option>
  137. `;
  138.  
  139. const newCardTemplate = `
  140. <div class="row">
  141. <div class="col-md-12 mb-5">
  142. <div class="card info-graphic info-graphic--service">
  143. <div class="card-body">
  144. <h2>Notlar</h2>
  145. <select id="notscript-donemlist" class="form-control" style="margin: 10px">
  146. {1}
  147. </select>
  148. <div class="row" style="justify-content: center; align-items: center;">
  149. <input id="notscript-hidetrivial" type="checkbox">
  150. <label for="notscript-hidetrivial" style="padding-left: 10px; margin: 0; user-select: none;">Hiç not girilmemiş dersleri gizle</label>
  151. </div>
  152. {0}
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. `;
  158.  
  159. const newDonemTemplate = `
  160. <div style="display: none" class="notscript-donem" data-id={id}>{classes}</div>
  161. `;
  162.  
  163. const newClassTemplateTable = `
  164. <div class="col-lg-12 mb-3 notscript-class" data-crn="{crn}">
  165. <h4 style="font-weight: 600">{name}</h4>
  166. <div class="table-vertical table-vertical--unheight">
  167. <table class="table table-striped table-bordered" style="table-layout: fixed">
  168. <tbody>
  169. {notes}
  170. <tr>
  171. <th class="title" style="width: 40%">Ağırlıklı Ortalama</th>
  172. <td>{average}</td>
  173. </tr>
  174. <tr style="display: {harfnotudisp}">
  175. <th class="title" style="width: 40%">Harf Notu</th>
  176. <td>{harfnotu}</td>
  177. </tr>
  178. </tbody>
  179. </table>
  180. </div>
  181. </div>
  182. `;
  183.  
  184. const newNoteTemplateTable = `
  185. <tr>
  186. <th class="title" style="width: 40%">{name} (%{perc})</th>
  187. <td>{note}</td>
  188. </tr>
  189. `;
  190.  
  191. const classLineTemplate = `
  192. <div id="notscript-line-{crn}" style="width: 100%; height: 1px; background: #358aed; margin-bottom: 15px; margin-top: 13px;"></div>
  193. `;
  194.  
  195. // TODO: Option to hide ignorable classes
  196. // TODO: Term Selection
  197. async function printNotlar() {
  198. const jwt = await getJWT();
  199. const donemListesi = (await getDonemListesi(jwt)).ogrenciDonemListesi;
  200. const sonDonem = donemListesi[donemListesi.length - 1];
  201. const sonDonemId = sonDonem.akademikDonemId;
  202.  
  203. const addBefore = document.querySelectorAll(".obs > .container-fluid > div > .row")[1];
  204.  
  205. const hidecrns = [];
  206.  
  207. let donemOptList = "";
  208. let donems = "";
  209. for (let i = donemListesi.length - 1; i >= 0; i--) {
  210. const donem = donemListesi[i];
  211. donemOptList += newOptionTemplate.formatStr({ id: donem.akademikDonemId, name: donem.akademikDonemAdi });
  212.  
  213. const disp = donem.akademikDonemId === sonDonemId ? "block" : "none";
  214.  
  215. const sinifListesi = (await getSinifListesi(jwt, donem.akademikDonemId)).kayitSinifResultList;
  216. const harfNotuListesi = (await getHarfNotuListesi(jwt, donem.akademikDonemId));
  217.  
  218. let classesEl = "";
  219. for (const sinif of sinifListesi) {
  220. if (classesEl !== "") classesEl += classLineTemplate.formatStr({ crn: sinif.crn });
  221.  
  222. const sinifNameEn = sinif.bransKodu + sinif.dersKodu + " - " + sinif.dersAdiEN + " (CRN: " + sinif.crn + ")";
  223. const sinifNameTr = sinif.bransKodu + sinif.dersKodu + " - " + sinif.dersAdiTR + " (CRN: " + sinif.crn + ")";
  224. const sinifId = sinif.sinifId;
  225.  
  226. const notListesiObj = (await getNotListesi(jwt, sinifId));
  227. const notListesi = notListesiObj.sinifDonemIciNotListesi;
  228. const ortalama = notListesiObj.ortalama;
  229.  
  230. let notesEl = "";
  231. for (const not of notListesi) {
  232. const notName = not.degerlendirmeOlcutuAdi;
  233. const notPerc = not.degerlendirmeKatkisi;
  234. const notValue = not.not;
  235.  
  236. notesEl += newNoteTemplateTable.formatStr({ name: notName, perc: notPerc, note: notValue });
  237. }
  238.  
  239. if (notListesi.length === 0 && !harfNotuListesi[sinif.crn]) {
  240. hidecrns.push(sinif.crn);
  241. }
  242.  
  243. let harfnotudisp = "none";
  244. let harfnotu = "";
  245. if (harfNotuListesi[sinif.crn]) {
  246. harfnotudisp = "table-row";
  247. harfnotu = harfNotuListesi[sinif.crn];
  248. }
  249.  
  250. classesEl += newClassTemplateTable.formatStr({ crn: sinif.crn, name: sinifNameTr, notes: notesEl, average: ortalama, harfnotudisp, harfnotu });
  251. }
  252.  
  253. donems += newDonemTemplate.formatStr({ id: donem.akademikDonemId, classes: classesEl });
  254. }
  255.  
  256. const cardEl = newCardTemplate.formatStr(donems, donemOptList);
  257. const cardEll = createHTMLElement(cardEl);
  258. addBefore.parentElement.insertBefore(cardEll, addBefore);
  259.  
  260. const donemlistEl = cardEll.querySelector("#notscript-donemlist");
  261. const donemElList = cardEll.querySelectorAll(".notscript-donem");
  262. donemlistEl.onchange = (e) => {
  263. for (const donem of donemElList) {
  264. if (donem.dataset.id === donemlistEl.value) donem.style.display = "block";
  265. else donem.style.display = "none";
  266. }
  267. }
  268. donemlistEl.onchange();
  269.  
  270. const hidetrivialEl = cardEll.querySelector("#notscript-hidetrivial");
  271. hidetrivialEl.checked = window.localStorage.getItem("hide_trivial_classes");
  272.  
  273. const classes = cardEll.querySelectorAll(".notscript-class");
  274.  
  275. const trivialClasses = classes.values().filter(cl => hidecrns.includes(cl.dataset.crn)).toArray();
  276.  
  277. hidetrivialEl.onchange = (e) => {
  278. const disp = hidetrivialEl.checked ? "none" : "block";
  279. window.localStorage.setItem("hide_trivial_classes", hidetrivialEl.checked);
  280. for (const cl of trivialClasses) {
  281. cl.style.display = disp;
  282.  
  283. const line = cardEll.querySelector("#notscript-line-" + cl.dataset.crn);
  284. line.style.display = disp;
  285. }
  286. }
  287.  
  288. hidetrivialEl.onchange();
  289. }
  290.  
  291. printNotlar();
  292.  
  293.  
  294. })();