Skip to content

Commit

Permalink
use class hash map
Browse files Browse the repository at this point in the history
  • Loading branch information
beebls committed Apr 1, 2024
1 parent 7f048fa commit c58937f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/deckyPatches/UnminifyMode.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import { unminifyClass } from "decky-frontend-lib";
import { classMap } from "decky-frontend-lib";
import { getRootElements } from "./SteamTabElementsFinder";

const classHashMap = new Map<string, string>();

function initializeClassHashMap() {
const withoutLocalizationClasses = classMap.filter((module) => Object.keys(module).length < 1000);

const allClasses = withoutLocalizationClasses
.map((module) => {
let filteredModule = {};
Object.entries(module).forEach(([propertyName, value]) => {
// Filter out things that start with a number (eg: Breakpoints like 800px)
// I have confirmed the new classes don't start with numbers
if (isNaN(Number(value.charAt(0)))) {
filteredModule[propertyName] = value;
}
});
return filteredModule;
})
.filter((module) => {
// Some modules will be empty after the filtering, remove those
return Object.keys(module).length > 0;
});

allClasses.forEach((module: Record<string, string>) => {
Object.entries(module).forEach(([propertyName, value]) => {
classHashMap.set(value, propertyName);
});
});
}

export function unminifyElement(element: Element) {
if (element.classList.length === 0) return;

const classList = Array.from(element.classList);
const unminifiedClassList = classList.map((c) => unminifyClass(c) ?? c);
const unminifiedClassList = classList.map((c) => classHashMap.get(c) || c);
element.setAttribute("unminified-class", unminifiedClassList.join(" "));
}

Expand Down Expand Up @@ -52,6 +81,7 @@ export function setUpMutationObserver(rootElement: any) {

export function enableUnminifyMode() {
if (mutationObservers.length > 0) disconnectMutationObservers();
initializeClassHashMap();
const roots = getRootElements();
roots.forEach(initialUnminification);
roots.forEach(setUpMutationObserver);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/PluginSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function PluginSettings() {
<ToggleField
checked={unminifyModeOn}
label="Unminify Mode"
description="Adds unminified classnames to devtools view, resets on steam client restart, VERY VERY LAGGY!"
description="Adds unminified classnames to devtools view, resets on steam client restart"
onChange={setUnminify}
/>
</Focusable>
Expand Down

0 comments on commit c58937f

Please sign in to comment.