Skip to content

Commit

Permalink
Merge pull request #2 from Catalyst4222/main
Browse files Browse the repository at this point in the history
lint: re-enable and fix linting
  • Loading branch information
SiriusBYT authored Sep 21, 2023
2 parents 1c02b1d + 9ac69c5 commit e369444
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 70 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

File renamed without changes.
2 changes: 0 additions & 2 deletions .prettierignore

This file was deleted.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
125 changes: 60 additions & 65 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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 <html> 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 <html> 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);
}

0 comments on commit e369444

Please sign in to comment.