diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index afe8be7..0000000 --- a/.eslintignore +++ /dev/null @@ -1,2 +0,0 @@ -*.js -*.ts diff --git a/.github/workflows/lint.yml.bak b/.github/workflows/lint.yml similarity index 100% rename from .github/workflows/lint.yml.bak rename to .github/workflows/lint.yml diff --git a/.prettierignore b/.prettierignore deleted file mode 100644 index 1fa23be..0000000 --- a/.prettierignore +++ /dev/null @@ -1,2 +0,0 @@ -*.ts -**/src diff --git a/package.json b/package.json index 344d1e8..102a18a 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,14 @@ "build": "replugged build plugin", "watch": "replugged build plugin --watch", "bundle": "replugged bundle plugin", - "release": "replugged release" + "release": "replugged release", + "check": "tsc --noEmit", + "prettier:check": "prettier ./src --check", + "eslint:check": "eslint ./src", + "prettier:fix": "prettier ./src --write", + "eslint:fix": "eslint ./src --fix", + "lint": "pnpm run prettier:check && pnpm run eslint:check && pnpm run check", + "lint:fix": "pnpm run prettier:fix && pnpm run eslint:fix" }, "keywords": [], "author": "", diff --git a/src/index.ts b/src/index.ts index 67563c4..27e4257 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,84 +1,79 @@ -import { Injector, Logger, webpack } from "replugged"; +import { Logger } from "replugged"; -const inject = new Injector(); const logger = Logger.plugin("ThemeHooker"); -export async function start(): Promise { - console.log(`[ThemeHooker] ThemeHooker has now started.`) +const ThemeNames: string[] = [ + "mint-apple", + "citrus-sherbert", + "retro-raincloud", + "hanami", + "sunrise", + "cotton-candy", //Candyfloss + "lofi-vibes", + "desert-khaki", + "sunset", + "chroma-glow", + "forest", + "crimson-moon", + "midnight-burple", + "mars", + "dusk", + "under-the-sea", + "retro-storm", + "neon-lights", + "strawberry-lemonade", + "aurora", + "sepia", + "easter-egg", //Memory Lane +]; - const html = document.documentElement; - const {body} = document; - let ThemeNames: string[] = [ - "mint-apple", - "citrus-sherbert", - "retro-raincloud", - "hanami", - "sunrise", - "cotton-candy", //Candyfloss - "lofi-vibes", - "desert-khaki", - "sunset", - "chroma-glow", - "forest", - "crimson-moon", - "midnight-burple", - "mars", - "dusk", - "under-the-sea", - "retro-storm", - "neon-lights", - "strawberry-lemonade", - "aurora", - "sepia", - "easter-egg" //Memory Lane - ] - - html.setAttribute("theme-hooker", "theme-null"); - console.log(`[ThemeHooker] Added "theme-hooker" to the tag.`); +function setTheme(): void { + const { body, documentElement: html } = document; - detectTheme(); - - function detectTheme() { - for (let ThemeNumber in ThemeNames) { - try { - if(document.querySelector('style[data-client-themes="true"]').textContent.includes(ThemeNames[ThemeNumber])) { - console.log(`[ThemeHooker] Detected Theme: "${ ThemeNames[ThemeNumber] }".`) - html.setAttribute("theme-hooker", `theme-${ ThemeNames[ThemeNumber]}`); - body.setAttribute("theme-hooker", `theme-${ ThemeNames[ThemeNumber]}`); - break - } - } - catch { - html.setAttribute("theme-hooker", `theme-null`); - body.setAttribute("theme-hooker", `theme-null`); + for (const Theme of ThemeNames) { + try { + if ( + document.querySelector?.('style[data-client-themes="true"]')?.textContent?.includes?.(Theme) + ) { + logger.log(`Detected Theme: "${Theme}".`); + html.setAttribute("theme-hooker", `theme-${Theme}`); + body.setAttribute("theme-hooker", `theme-${Theme}`); + break; } + } catch { + html.setAttribute("theme-hooker", `theme-null`); + body.setAttribute("theme-hooker", `theme-null`); } } +} + +export function start(): void { + logger.log(`ThemeHooker has now started.`); + + const html = document.documentElement; + + html.setAttribute("theme-hooker", "theme-null"); + logger.log(`Added "theme-hooker" to the tag.`); + + setTheme(); - const TrackedMutation = document.head; const MutationConfig = { attributes: true, childList: true, subtree: true }; - const TrackerReaction = (mutationList, Tracking) => { + const Tracking = new MutationObserver((mutationList) => { for (const mutation of mutationList) { if (mutation.type === "childList") { - console.log("[ThemeHooker] Child List Modification detected, trying to detect the current theme."); - detectTheme(); + logger.log("Child List Modification detected, trying to detect the current theme."); + setTheme(); } else if (mutation.type === "attributes") { - console.log(`[ThemeHooker] Attribute Modification detected, trying to detect the current theme.`); - detectTheme(); + logger.log(`Attribute Modification detected, trying to detect the current theme.`); + setTheme(); + // @ts-expect-error Probably included for a reason? } else if (mutation.type === "subtree") { - console.log(`[ThemeHooker] Subtree Modification detected, trying to detect the current theme.`); - detectTheme(); + logger.log(`Subtree Modification detected, trying to detect the current theme.`); + setTheme(); } } - } + }); - const Tracking = new MutationObserver(TrackerReaction); - Tracking.observe(TrackedMutation, MutationConfig); - -} - -export function stop(): void { - inject.uninjectAll(); + Tracking.observe(document.head, MutationConfig); } -