Skip to content

Instantly share code, notes, and snippets.

@cm8263
Created June 22, 2023 04:21
Show Gist options
  • Save cm8263/0b96dfc3beb4b8ea97a4ae57692d3f60 to your computer and use it in GitHub Desktop.
Save cm8263/0b96dfc3beb4b8ea97a4ae57692d3f60 to your computer and use it in GitHub Desktop.
Hacky changes to Sentral Education's Sickbay Register Screen. Requires something like https://github.com/Ruud14/Page-Manipulator.
const config = {
cats: false
}
///
let catImage;
let lastResults;
injection = async () => {
const list = document.querySelector("#incursions-list");
if (list === null) {
console.log("No table found, dipping out.");
return;
}
const secondList = document.querySelectorAll("tbody")[1].querySelector("tr");
console.log("Injecting sick bay register custom code");
removeButtons();
document.querySelector("#scan-slip-print-container").remove();
if (config.cats && secondList.style.display !== "none") {
secondList.querySelector("div").textContent += ". Have a cat instead!";
const p = document.createElement("p");
catImage = document.createElement("img");
img.style.width = "25%";
p.appendChild(img);
secondList.querySelector("div").appendChild(p);
}
updateColsAndRows(list);
};
updateColsAndRows = (list) => {
const headersToRemove = [1, 6, 7, 8];
const headerParent = document.querySelector("thead").querySelector("tr");
const headers = headerParent.querySelectorAll("th");
const optionHeader = document.createElement("th");
const quickActionsHeader = document.createElement("th");
headersToRemove.forEach(el => headers[el].remove());
headers[5].textContent = "Times";
headers[10].textContent = "🗑"
optionHeader.textContent = "Notifs";
quickActionsHeader.textContent = "⚡";
headerParent.appendChild(optionHeader);
headerParent.insertBefore(quickActionsHeader, headerParent.childNodes[2]);
for (var i = 0, row; row = list.rows[i]; i++) {
const notifsCell = row.insertCell();
const colsToRemove = [];
const colIndexsToRemove = [1, 6, 7, 8];
let quickActionLinks;
notifsCell.innerHTML = `<td><span class="icon icon-spin icon-spinner searchIndicator"></span></td>`;
for (var j = 0, col; col = row.cells[j]; j++) {
if (colIndexsToRemove.contains(j)) colsToRemove.push(col);
const link = col.querySelector("a");
const button = col.querySelector("button");
const menu = col.querySelector(".dropdown-menu");
if (col.contains(link) && !link.contains(link.querySelector("span")) && link.href.slice(-1) !== "#") {
const searchTerm = "student/";
const student = link.href.slice(link.href.indexOf(searchTerm) + searchTerm.length).split("/")[0];
quickActionLinks = `
<a href="/attendance/administration/student/${student}" target="_blank">📅</a>
<a href="/health/student/${student}/sickbay" target="_blank">📋</a>
`;
}
if (j === 5) {
const start = convertTime12to24(col.textContent.trim());
if (row.cells[6].querySelectorAll("a").length) {
col.textContent = getDuration(start);
} else {
const end = convertTime12to24(row.cells[6].textContent.trim());
col.innerHTML = `<td>${start} - ${end}<p>${getDuration(start, end)}</p></td>`
}
}
if (col.contains(menu)) {
const remove = menu.querySelectorAll("li")[3].querySelector("a");
remove.className = "remove-sickbay-entry-btn pointer btn btn-danger btn-small";
remove.innerHTML = '<span class="icon icon-trash"></span>';
col.querySelector(".btn-group").replaceWith(remove);
}
if (col.contains(button)) {
button.remove();
}
}
colsToRemove.forEach(col => col.remove());
const quickActionsCell = row.insertCell(1);
quickActionsCell.innerHTML = quickActionLinks;
updateRow(row, notifsCell);
}
}
updateRow = async (row, notifsCell) => {
const response = await fetch(row.cells[0].querySelector("a").href.replace("edit", "view"));
const responseData = await response;
if (responseData.status != 200) return;
const parser = new DOMParser();
const body = await responseData.text();
const doc = parser.parseFromString(body, "text/html");
const notiContainer = doc.querySelector("#notification-container");
const notifications = notiContainer.querySelector(".span8").innerHTML.replace(/\s/g, "").split("<br>").filter(n => n);
let notis = [];
notifications.forEach(function (value) {
switch (value) {
case "Phone":
notis.push("📱");
break;
case "PushNotification":
notis.push("🔔");
break;
default:
break;
}
});
if (notis.length > 0) {
notifsCell.innerHTML = `<td>${notis.sort().join("")}</td>`;
} else {
notifsCell.innerHTML = `<td>❌</td>`;
}
}
const removeButtons = () => {
const mainControls = document.querySelector(".content-box-controls");
if (mainControls === null) return;
const mainButton = mainControls.querySelector("button");
if (!mainButton) return;
mainButton.remove();
const controls = document.querySelector(".content-box-footer").querySelector(".controls");
if (controls === null) return;
const button = controls.querySelector("button");
if (!button) return;
button.remove();
}
const getDuration = (start, end = null) => {
const pastDate = new Date();
pastDate.setHours(...start.split(":"));
let tempTime;
if (end !== null) {
const checkOutTime = new Date();
checkOutTime.setHours(...end.split(":"));
tempTime = msToTime(checkOutTime.getTime() - pastDate.getTime());
} else {
tempTime = msToTime(new Date().getTime() - pastDate.getTime());
}
return tempTime;
}
const convertTime12to24 = (time12h) => {
const [time, modifier] = [time12h.substring(0, time12h.length -2), time12h.slice(-2)];
let [hours, minutes] = time.split(":");
if (hours === "12") hours = "00";
if (modifier === "pm") hours = parseInt(hours, 10) + 12;
return `${hours}:${minutes}`;
}
const msToTime = (duration) => {
let seconds = Math.floor((duration / 1000) % 60);
let minutes = Math.floor((duration / (1000 * 60)) % 60);
let hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
if (hours === 0) {
hours = "";
} else {
hours += " hours, ";
}
if (minutes === 0) {
minutes = "";
} else {
minutes += " minutes";
}
if (seconds === 0) {
seconds = "";
} else if (minutes === "") {
seconds += " seconds";
} else {
seconds = `, ${seconds} seconds`;
}
return hours + minutes + seconds;
}
const getCatUrl = () => img.src = "https://cataas.com/c?" + new Date().getTime();
injection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment