schoolbox web extension :)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(plugins/scrollPeriod): hot reload

willow 094ff682 88407623

+36 -28
+36 -28
src/entrypoints/plugins/scrollPeriod.ts
··· 1 1 import { getCurrentPeriod } from "@/utils/periodUtils"; 2 2 import { definePlugin } from "@/utils/plugin"; 3 3 4 + let controller = new AbortController(); 5 + let interval: number | null = null; 6 + 4 7 export default function init() { 5 8 definePlugin( 6 9 "scrollPeriod", 7 10 async (settings) => { 8 11 const timetable = document.querySelector("[data-timetable-container] div.scrollable"); 9 12 10 - if (window.location.pathname === "/" && document.getElementsByClassName("timetable")[0]) { 11 - updateScrollbar(); 13 + if (window.location.pathname === "/" && timetable) { 14 + updateScrollbar(timetable); 12 15 13 16 const cooldownDuration = settings?.slider.cooldownDuration; 14 17 const resetCooldownOnMouseMove = settings?.toggle.resetCooldownOnMouseMove; 15 18 16 - let interval: string | number | NodeJS.Timeout | undefined; 17 - function start() { 19 + const setUpdateInterval = () => { 18 20 interval = setInterval(updateScrollbar, (cooldownDuration?.value || 10) * 1000); 19 - } 20 - function reset() { 21 - if (interval) { 22 - clearInterval(interval); 23 - start(); 24 - } 25 - } 21 + }; 26 22 27 - start(); 23 + setUpdateInterval(); 28 24 29 25 if (resetCooldownOnMouseMove === true) { 30 - document.addEventListener("mousemove", reset); 31 - } 32 - } 33 - 34 - function updateScrollbar() { 35 - const currentPeriod = getCurrentPeriod(); 36 - if (currentPeriod && currentPeriod.index && timetable) { 37 - const period = document.querySelector( 38 - `.timetable thead tr th:nth-child(${currentPeriod.index})`, 39 - ) as HTMLElement; 40 - if (period) { 41 - timetable.scroll({ 42 - left: period.offsetLeft - 55, // adjusted for alignment 43 - behavior: "smooth", // or 'auto' for instant scroll 44 - }); 45 - } 26 + document.addEventListener( 27 + "mousemove", 28 + () => { 29 + if (interval) { 30 + clearInterval(interval); 31 + setUpdateInterval(); 32 + } 33 + }, 34 + { signal: controller.signal }, 35 + ); 46 36 } 47 37 } 48 38 }, 39 + () => { 40 + controller.abort(); 41 + if (interval) clearInterval(interval); 42 + interval = null; 43 + }, 49 44 [".timetable"], 50 45 ); 51 46 } 47 + 48 + function updateScrollbar(timetable: Element) { 49 + const currentPeriod = getCurrentPeriod(); 50 + if (currentPeriod && currentPeriod.index && timetable) { 51 + const period = document.querySelector(`.timetable thead tr th:nth-child(${currentPeriod.index})`) as HTMLElement; 52 + if (period) { 53 + timetable.scroll({ 54 + left: period.offsetLeft - 55, // adjusted for alignment 55 + behavior: "smooth", // or 'auto' for instant scroll 56 + }); 57 + } 58 + } 59 + }