From 1869aa805a858ac7dabaec003dc4edaba57511c2 Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:32:50 -0400 Subject: [PATCH 01/10] refactor: switch to replugged's logger --- src/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 67563c4..bb18d5e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ const inject = new Injector(); const logger = Logger.plugin("ThemeHooker"); export async function start(): Promise { - console.log(`[ThemeHooker] ThemeHooker has now started.`) + logger.log(`ThemeHooker has now started.`) const html = document.documentElement; const {body} = document; @@ -34,7 +34,7 @@ export async function start(): Promise { ] html.setAttribute("theme-hooker", "theme-null"); - console.log(`[ThemeHooker] Added "theme-hooker" to the tag.`); + logger.log(`Added "theme-hooker" to the tag.`); detectTheme(); @@ -42,7 +42,7 @@ export async function start(): Promise { 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] }".`) + logger.log(`Detected Theme: "${ ThemeNames[ThemeNumber] }".`) html.setAttribute("theme-hooker", `theme-${ ThemeNames[ThemeNumber]}`); body.setAttribute("theme-hooker", `theme-${ ThemeNames[ThemeNumber]}`); break @@ -61,13 +61,13 @@ export async function start(): Promise { const TrackerReaction = (mutationList, Tracking) => { for (const mutation of mutationList) { if (mutation.type === "childList") { - console.log("[ThemeHooker] Child List Modification detected, trying to detect the current theme."); + logger.log("Child List Modification detected, trying to detect the current theme."); detectTheme(); } else if (mutation.type === "attributes") { - console.log(`[ThemeHooker] Attribute Modification detected, trying to detect the current theme.`); + logger.log(`Attribute Modification detected, trying to detect the current theme.`); detectTheme(); } else if (mutation.type === "subtree") { - console.log(`[ThemeHooker] Subtree Modification detected, trying to detect the current theme.`); + logger.log(`Subtree Modification detected, trying to detect the current theme.`); detectTheme(); } } From 547e5590f09db88870c858ba0202f244b5f0c7ee Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:34:23 -0400 Subject: [PATCH 02/10] refactor: switch to for...of loops --- src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index bb18d5e..8c95bd3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,12 +39,12 @@ export async function start(): Promise { detectTheme(); function detectTheme() { - for (let ThemeNumber in ThemeNames) { + for (const Theme of ThemeNames) { try { - if(document.querySelector('style[data-client-themes="true"]').textContent.includes(ThemeNames[ThemeNumber])) { - logger.log(`Detected Theme: "${ ThemeNames[ThemeNumber] }".`) - html.setAttribute("theme-hooker", `theme-${ ThemeNames[ThemeNumber]}`); - body.setAttribute("theme-hooker", `theme-${ ThemeNames[ThemeNumber]}`); + 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 } } From 9706b5482fb4a2ea4b0f5b437a45b378e4e93da4 Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:35:04 -0400 Subject: [PATCH 03/10] lint: don't hide eslint issues --- .eslintignore | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index afe8be7..0000000 --- a/.eslintignore +++ /dev/null @@ -1,2 +0,0 @@ -*.js -*.ts From 6a32360c0f5d95d523feaab95a3dcd44718f366f Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:39:45 -0400 Subject: [PATCH 04/10] refactor: extract static variables/functions --- src/index.ts | 93 +++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8c95bd3..7fc82f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,57 +3,60 @@ import { Injector, Logger, webpack } from "replugged"; const inject = new Injector(); const logger = Logger.plugin("ThemeHooker"); +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 +] + +function setTheme() { + const {body, documentElement: html} = document + + 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 async function start(): Promise { logger.log(`ThemeHooker has now started.`) 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"); logger.log(`Added "theme-hooker" to the tag.`); - detectTheme(); + setTheme(); - function detectTheme() { - 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`); - } - } - } const TrackedMutation = document.head; const MutationConfig = { attributes: true, childList: true, subtree: true }; @@ -62,13 +65,13 @@ export async function start(): Promise { for (const mutation of mutationList) { if (mutation.type === "childList") { logger.log("Child List Modification detected, trying to detect the current theme."); - detectTheme(); + setTheme(); } else if (mutation.type === "attributes") { logger.log(`Attribute Modification detected, trying to detect the current theme.`); - detectTheme(); + setTheme(); } else if (mutation.type === "subtree") { logger.log(`Subtree Modification detected, trying to detect the current theme.`); - detectTheme(); + setTheme(); } } } From 28718c21c8ea8914aa0891b467b7e747bdbc0f14 Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:42:23 -0400 Subject: [PATCH 05/10] refactor: condense the MutationObserver code --- src/index.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7fc82f1..3fc6f05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,10 +58,9 @@ export async function start(): Promise { setTheme(); - const TrackedMutation = document.head; const MutationConfig = { attributes: true, childList: true, subtree: true }; - const TrackerReaction = (mutationList, Tracking) => { + const Tracking = new MutationObserver((mutationList, Tracking) => { for (const mutation of mutationList) { if (mutation.type === "childList") { logger.log("Child List Modification detected, trying to detect the current theme."); @@ -74,10 +73,9 @@ export async function start(): Promise { setTheme(); } } - } + }); - const Tracking = new MutationObserver(TrackerReaction); - Tracking.observe(TrackedMutation, MutationConfig); + Tracking.observe(document.head, MutationConfig); } From f91ef01445ecbb1ecbd1bc837531beb78289784c Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:44:01 -0400 Subject: [PATCH 06/10] refactor: remove unused variables --- src/index.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3fc6f05..405a37b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ -import { Injector, Logger, webpack } from "replugged"; +import { Logger } from "replugged"; -const inject = new Injector(); const logger = Logger.plugin("ThemeHooker"); const ThemeNames: string[] = [ @@ -47,7 +46,7 @@ function setTheme() { } } -export async function start(): Promise { +export function start(): void { logger.log(`ThemeHooker has now started.`) const html = document.documentElement; @@ -60,7 +59,7 @@ export async function start(): Promise { const MutationConfig = { attributes: true, childList: true, subtree: true }; - const Tracking = new MutationObserver((mutationList, Tracking) => { + const Tracking = new MutationObserver((mutationList) => { for (const mutation of mutationList) { if (mutation.type === "childList") { logger.log("Child List Modification detected, trying to detect the current theme."); @@ -79,7 +78,3 @@ export async function start(): Promise { } -export function stop(): void { - inject.uninjectAll(); -} - From de5a774ff6e757f3b26a78aae95c8e0707a65d98 Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:50:31 -0400 Subject: [PATCH 07/10] types: fix up types --- src/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 405a37b..2ae7e7f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,12 +27,12 @@ const ThemeNames: string[] = [ "easter-egg" //Memory Lane ] -function setTheme() { +function setTheme(): void { const {body, documentElement: html} = document for (const Theme of ThemeNames) { try { - if(document.querySelector('style[data-client-themes="true"]').textContent.includes(Theme)) { + 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}`); @@ -67,6 +67,7 @@ export function start(): void { } else if (mutation.type === "attributes") { 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") { logger.log(`Subtree Modification detected, trying to detect the current theme.`); setTheme(); From 494d7018a63fbdafd99bd4721cee9a84a817a4de Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:52:42 -0400 Subject: [PATCH 08/10] scripts: add back in the missing linting scripts --- package.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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": "", From 0e5e91097cca18fd4624534298c024eb60b8473c Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 13:54:51 -0400 Subject: [PATCH 09/10] scripts: re-enable the linting workflow --- .github/workflows/{lint.yml.bak => lint.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{lint.yml.bak => lint.yml} (100%) 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 From 9ac69c540b132cf913d4ae78d6746419844b9391 Mon Sep 17 00:00:00 2001 From: Catalyst4 Date: Wed, 20 Sep 2023 14:06:20 -0400 Subject: [PATCH 10/10] lint: re-enable prettier --- .prettierignore | 2 -- src/index.ts | 22 ++++++++++------------ 2 files changed, 10 insertions(+), 14 deletions(-) delete mode 100644 .prettierignore 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/src/index.ts b/src/index.ts index 2ae7e7f..27e4257 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,22 +24,23 @@ const ThemeNames: string[] = [ "strawberry-lemonade", "aurora", "sepia", - "easter-egg" //Memory Lane -] + "easter-egg", //Memory Lane +]; function setTheme(): void { - const {body, documentElement: html} = document + const { body, documentElement: html } = document; for (const Theme of ThemeNames) { try { - if(document.querySelector?.('style[data-client-themes="true"]')?.textContent?.includes?.(Theme)) { - logger.log(`Detected Theme: "${Theme}".`) + 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 + break; } - } - catch { + } catch { html.setAttribute("theme-hooker", `theme-null`); body.setAttribute("theme-hooker", `theme-null`); } @@ -47,7 +48,7 @@ function setTheme(): void { } export function start(): void { - logger.log(`ThemeHooker has now started.`) + logger.log(`ThemeHooker has now started.`); const html = document.documentElement; @@ -56,7 +57,6 @@ export function start(): void { setTheme(); - const MutationConfig = { attributes: true, childList: true, subtree: true }; const Tracking = new MutationObserver((mutationList) => { @@ -76,6 +76,4 @@ export function start(): void { }); Tracking.observe(document.head, MutationConfig); - } -