schoolbox web extension :)
0
fork

Configure Feed

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

refactor(utils): remove query selector helpers

willow 0cbf0236 91872937

+17 -33
+10 -6
src/entrypoints/plugins/progressBar/index.ts
··· 1 - import { injectInlineStyles, schooltapeQuerySelectorAll, uninjectInlineStyles } from "@/utils"; 1 + import { dataAttr, injectInlineStyles, uninjectInlineStyles } from "@/utils"; 2 2 import type { Period } from "@/utils/periodUtils"; 3 3 import { getListOfPeriods } from "@/utils/periodUtils"; 4 4 import { definePlugin } from "@/utils/plugin"; ··· 30 30 ); 31 31 } 32 32 33 - function getProgressBars() { 34 - return schooltapeQuerySelectorAll(PLUGIN_ID); 35 - } 33 + const getProgressBars = () => document.querySelectorAll(dataAttr(PLUGIN_ID)); 36 34 37 35 function injectProgressBars(periodList: Period[], container: HTMLElement) { 38 - if (getProgressBars().length > 0) return; 36 + const progressBars = getProgressBars(); 37 + if (progressBars && progressBars.length > 0) return; 39 38 40 39 periodList.forEach((period) => { 41 40 const td = document.createElement("td"); ··· 62 61 } 63 62 64 63 function uninjectProgressBars() { 65 - getProgressBars().forEach((progressBar) => document.removeChild(progressBar)); 64 + const progressBars = getProgressBars(); 65 + if (progressBars && progressBars.length === 0) return; 66 + 67 + for (const bar of progressBars) { 68 + document.removeChild(bar); 69 + } 66 70 }
+7 -27
src/utils/index.ts
··· 9 9 el.dataset.schooltape = id; 10 10 } 11 11 12 - export function schooltapeQuerySelector(id: string) { 13 - // TODO: deprecate 14 - return document.querySelector(dataAttr(id)); 15 - } 16 - export function schooltapeQuerySelectorAll(id: string) { 17 - // TODO: deprecate 18 - return document.querySelectorAll(dataAttr(id)); 19 - } 20 - 21 12 export function injectInlineStyles(styleText: string, id: string) { 22 13 logger.info(`injecting styles with id ${id}`); 23 14 const style = document.createElement("style"); ··· 29 20 30 21 export function uninjectInlineStyles(id: string) { 31 22 logger.info(`uninjecting styles with id ${id}`); 32 - const style = schooltapeQuerySelector(`inline-${id}`); 33 - if (style) { 34 - document.head.removeChild(style); 35 - // logger.info(`uninjected styles with id ${id}`); 36 - } else { 37 - // logger.warn(`styles with id ${id} not found, aborting`) 38 - } 23 + const style = document.querySelector(dataAttr(`inline-${id}`)); 24 + if (style) document.head.removeChild(style); 39 25 } 40 26 41 27 export function injectCatppuccin() { ··· 109 95 // eslint-disable-next-line @typescript-eslint/no-explicit-any 110 96 export function injectStylesheet(url: any, id: string) { 111 97 // check if stylesheet has already been injected 112 - const existingLink = schooltapeQuerySelector(`stylesheet-${id}`); 113 - if (existingLink) { 114 - logger.info(`stylesheet with id ${id} already injected, aborting`); 115 - return; 116 - } 98 + const existingLink = document.querySelector(dataAttr(`stylesheet-${id}`)); 99 + if (existingLink) return; 117 100 118 101 // inject stylesheet 119 102 logger.info(`injecting stylesheet with id ${id}: ${url}`); ··· 126 109 127 110 export function uninjectStylesheet(id: string) { 128 111 logger.info(`uninjecting stylesheet with id ${id}`); 129 - const link = schooltapeQuerySelector(`stylesheet-${id}`); 130 - if (link) { 131 - document.head.removeChild(link); 132 - } else { 133 - // logger.warn(`stylesheet with id ${id} not found, aborting`); 134 - } 112 + 113 + const link = document.querySelector(dataAttr(`stylesheet-${id}`)); 114 + if (link) document.head.removeChild(link); 135 115 } 136 116 137 117 export function injectUserSnippet(id: string) {