This repository has been archived on 2022-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
tghandbook/src/scripts/index.ts

96 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-06-26 12:05:17 +00:00
import { chemistryScript, processChemistry } from "./pages/chemistry";
import { processVirology, virologyScript } from "./pages/virology";
2020-08-25 13:28:42 +00:00
import { processFood, foodScript } from "./pages/food";
2021-08-20 14:33:54 +00:00
import { processDrinks, drinkScript } from "./pages/drinks";
2020-06-26 12:05:17 +00:00
import { genericScript } from "./pages/generic";
import { processGlobal } from "./pages/global";
2020-06-28 15:44:59 +00:00
import { welcomeScript } from "./pages/welcome";
2020-06-16 10:51:24 +00:00
2020-06-21 17:16:01 +00:00
// This is used for cache busting when userscript changes significantly.
// Only change it when you made changes to the processHTML part!
export const PAGE_VERSIONS = {
Infections: "fcebeda2fddb46d924f4538cd9c0daeb55aa4c9b",
2020-08-25 13:28:42 +00:00
Guide_to_food_and_drinks: "131e010df66ed689d31df53c3ca17ad16635a827",
2021-08-20 14:33:54 +00:00
Guide_to_chemistry: "5074d6180fc8b283bac00b99c6aa2325b797da6b",
$DEFAULT: "5074d6180fc8b283bac00b99c6aa2325b797da6b",
};
2020-06-21 17:16:01 +00:00
2020-06-26 11:59:13 +00:00
const MAX_WIDTH = 440;
2020-06-21 17:16:01 +00:00
export function processHTML(root: HTMLElement, docname: string): void {
2020-06-26 12:05:17 +00:00
processGlobal(root, docname);
2020-06-16 16:21:12 +00:00
2020-06-21 17:16:01 +00:00
switch (docname) {
case "Guide_to_chemistry":
2020-06-26 12:05:17 +00:00
processChemistry(root);
2020-06-21 17:16:01 +00:00
break;
case "Infections":
processVirology(root);
break;
2021-08-20 14:33:54 +00:00
case "Guide_to_food":
2020-08-25 13:28:42 +00:00
processFood(root);
break;
2021-08-20 14:33:54 +00:00
case "Guide_to_drinks":
processDrinks(root);
break;
2020-06-21 17:16:01 +00:00
default:
}
}
2020-06-16 10:51:24 +00:00
2020-06-26 11:59:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function postProcessHTML(root: HTMLElement, docname: string): void {
// This should be noop unless we're testing changes before committing them to processHTML
document.querySelectorAll("img[width]").forEach((img) => {
const width = img.getAttribute("width");
2020-06-17 00:43:10 +00:00
2020-06-26 11:59:13 +00:00
// Don't care if they are not absolutely sized
if (width.includes("%")) {
return;
}
2020-06-21 17:16:01 +00:00
2020-06-26 11:59:13 +00:00
const widthI = parseInt(width, 10);
if (widthI > MAX_WIDTH) {
img.setAttribute("width", "100%");
img.removeAttribute("height"); // Remove any height so we don't have to deal with the crazy math
2020-06-21 17:16:01 +00:00
}
});
2020-06-26 11:59:13 +00:00
switch (docname) {
default:
}
2020-06-21 17:16:01 +00:00
}
2020-06-16 10:51:24 +00:00
2020-06-21 17:16:01 +00:00
export function bindFunctions(root: HTMLElement, docname: string): void {
switch (docname) {
case "Guide_to_chemistry":
genericScript(root, docname);
2020-06-21 17:16:01 +00:00
chemistryScript(root);
break;
case "Infections":
genericScript(root, docname);
virologyScript(root);
break;
2020-06-28 15:44:59 +00:00
case "$Welcome":
welcomeScript(root);
break;
2021-08-20 14:33:54 +00:00
case "Guide_to_food":
2020-08-25 13:28:42 +00:00
genericScript(root, docname);
foodScript(root);
break;
2021-08-20 14:33:54 +00:00
case "Guide_to_drinks":
genericScript(root, docname);
drinkScript(root);
break;
default:
2020-06-24 15:12:11 +00:00
genericScript(root, docname);
break;
}
2020-06-16 10:51:24 +00:00
}
2020-06-21 17:16:01 +00:00
export default {
PAGE_VERSIONS,
2020-06-26 11:59:13 +00:00
postProcessHTML,
2020-06-21 17:16:01 +00:00
processHTML,
bindFunctions,
};