|
| 1 | +import { definePatch } from "@/utils/definePatch"; |
| 2 | +import { waitForRender, waitForReplacement } from "@/utils/DomObservers"; |
| 3 | +import { route } from "@/utils/route"; |
| 4 | + |
| 5 | +import css from "./style.css?inline"; |
| 6 | + |
| 7 | +async function hideEmptyColumns(signal: AbortSignal) { |
| 8 | + await waitForRender( |
| 9 | + () => |
| 10 | + document.querySelector( |
| 11 | + ".p-datatable-table .details-btn--appearance", |
| 12 | + ), |
| 13 | + document.body, |
| 14 | + signal, |
| 15 | + ); |
| 16 | + |
| 17 | + const headers = document.querySelectorAll(".p-datatable-table th"); |
| 18 | + |
| 19 | + for (const [index] of headers.entries()) { |
| 20 | + const cells = [ |
| 21 | + ...document.querySelectorAll( |
| 22 | + "tbody tr td:nth-child(" + (index + 1) + ")", |
| 23 | + ), |
| 24 | + ]; |
| 25 | + const check = cells.some((cell) => cell.textContent.trim().length > 0); |
| 26 | + |
| 27 | + const columnCells = document.querySelectorAll<HTMLElement>( |
| 28 | + "tr th:nth-child(" |
| 29 | + + (index + 1) |
| 30 | + + "), tr td:nth-child(" |
| 31 | + + (index + 1) |
| 32 | + + ")", |
| 33 | + ); |
| 34 | + for (const cell of columnCells) { |
| 35 | + cell.style.display = check ? "" : "none"; |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +async function prep(signal: AbortSignal) { |
| 41 | + if (window.innerWidth > 1024) { |
| 42 | + await waitForRender( |
| 43 | + () => document.querySelector(".MuiTabs-flexContainer > button"), |
| 44 | + document.body, |
| 45 | + signal, |
| 46 | + ); |
| 47 | + |
| 48 | + await hideEmptyColumns(signal); |
| 49 | + for (const tabButton of document.querySelectorAll( |
| 50 | + ".MuiTabs-flexContainer > button", |
| 51 | + )) { |
| 52 | + tabButton.addEventListener( |
| 53 | + "click", |
| 54 | + async () => { |
| 55 | + await waitForReplacement( |
| 56 | + () => |
| 57 | + document.querySelector( |
| 58 | + ".p-datatable-table .details-btn--appearance", |
| 59 | + ), |
| 60 | + document.body, |
| 61 | + signal, |
| 62 | + ); |
| 63 | + await hideEmptyColumns(signal); |
| 64 | + }, |
| 65 | + { signal }, |
| 66 | + ); |
| 67 | + } |
| 68 | + } else { |
| 69 | + await waitForRender( |
| 70 | + () => |
| 71 | + document.querySelector( |
| 72 | + ".MuiAccordionDetails-root.accordion__full-width__content > .mobile__frame .grades__box", |
| 73 | + ), |
| 74 | + document.body, |
| 75 | + signal, |
| 76 | + ); |
| 77 | + const mobileFrames = document.querySelectorAll( |
| 78 | + ".MuiAccordionDetails-root.accordion__full-width__content > .mobile__frame", |
| 79 | + ); |
| 80 | + for (const semester of mobileFrames) { |
| 81 | + await waitForRender( |
| 82 | + () => |
| 83 | + semester.querySelector( |
| 84 | + ".MuiAccordionDetails-root .grades__box .info-row .info-text > span", |
| 85 | + ), |
| 86 | + semester as HTMLElement, |
| 87 | + signal, |
| 88 | + ); |
| 89 | + for (const infoRow of semester.querySelectorAll(".info-row")) { |
| 90 | + const textContent = |
| 91 | + infoRow |
| 92 | + .querySelector(".info-text > span") |
| 93 | + ?.textContent.trim() ?? ""; |
| 94 | + if (textContent === "" || textContent === "0") { |
| 95 | + infoRow.remove(); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export default definePatch({ |
| 103 | + css, |
| 104 | + init: (_, signal) => prep(signal), |
| 105 | + meta: { |
| 106 | + description: |
| 107 | + "Hides subjects without any grades and columns without content.", |
| 108 | + id: "cleanup-grades", |
| 109 | + matches: [route("*/oceny")], |
| 110 | + name: "Cleanup grades table", |
| 111 | + }, |
| 112 | +}); |
0 commit comments