Structural cleanup based on thermo-nuclear code quality review: - Extract shared color-picker.css and color-popover.css from badges.css (611→85 lines) - Extract shared lib/presets.js for preset CRUD used by popup and popover - Unify dark mode to single applyDarkModeClass on <html>, removing 4 scattered functions - Fix stale closure in startPostObserver, data-driven reload key check - Remove dead exports, getHelpers wrapper, duplicated colorToHex - Replace DOM expando properties with state array in popup.js Net: -318 lines of duplication, no file over 401 lines. Co-authored-by: Cursor <cursoragent@cursor.com>
4.7 KiB
4.7 KiB
Thermo-Nuclear Code Quality Refactor — Reddit Tweaks Extension
Date: 2026-08-28 01:23 Task: Implement all 10 findings from the thermo-nuclear code quality review
Changes Made
1. CSS Deduplication — Extracted shared color-picker.css (NEW FILE)
- Created
content/styles/color-picker.css(259 lines) containing color picker widget styles and preset badge styles - This file is now shared between the content script (via manifest.json) and the popup (via
<link>tag) - Eliminated ~200 lines of identical CSS that was previously duplicated between
badges.cssandpopup.css
2. CSS Decomposition — Extracted color-popover.css (NEW FILE)
- Created
content/styles/color-popover.css(256 lines) with popover chrome, buttons, dark mode - Trimmed
badges.cssfrom 611 lines to 85 lines (badges-only) - Trimmed
popup.cssfrom 489 lines to 220 lines (popup-specific only) - Updated dark mode selectors from
.rt-color-popover.rt-darkto.rt-dark .rt-color-popover(ancestor form)
3. Shared Preset Module — Extracted lib/presets.js (NEW FILE)
- Created
lib/presets.js(57 lines) withrender(),save(),delete()functions - Refactored
popup.jsandcolor-popover.jsto use shared module - Eliminated ~60 lines of duplicated preset rendering and CRUD logic
4. Dark Mode Unification
- Added
applyDarkModeClass(settings)tolib/settings.js - Removed per-container dark class toggling from
badges.jsapplyColorsToContainer() - Removed
applyDarkMode()fromcards.js(was redundant with shared function) - Removed
applyDarkModeToPopover()andrefreshTheme()fromcolor-popover.js main.jsnow callsapplyDarkModeClass()centrally in init, dark-mode observer, and storage listener
5. Stale Closure Fix
- Fixed
startPostObserverinmain.jsto use freshly-loadedupdatedsettings instead of stale capturedsettings
6. Data-Driven Storage Listener
- Replaced 4 verbose
changes.X && changes.X.newValue !== ...conditions withRELOAD_KEYS.some()
7. Dead Export Cleanup
- Removed
isDarkModeandgetColorPrefixre-exports frombadgeTweakobject inbadges.js
8. Deleted getHelpers() Wrapper
- Removed unnecessary indirection in
color-popover.js, replaced with directwindow.RedditTweaks.*calls
9. Inlined colorToHex
- Removed duplicated
colorToHexfunction from bothpopup.jsandcolor-popover.js - Replaced all call sites with inline
val || "#ffffff"
10. DOM Expando Refactor
- Replaced
entry._colorState,entry._nameInput, etc. DOM expando pattern inpopup.js - Introduced
subredditEntriesarray of plain state objects collectSubredditOverrides()now iterates the state array instead of querying the DOM
Files Changed
content/main.js— stale closure fix, data-driven reload keys, central dark mode callscontent/tweaks/badges.js— removed dark class toggle, dead exports, refreshTheme callcontent/tweaks/cards.js— removed applyDarkMode, use shared functioncontent/tweaks/color-popover.js— removed getHelpers, colorToHex, applyDarkModeToPopover, refreshTheme, preset duplicationlib/settings.js— added applyDarkModeClasspopup/popup.js— inlined colorToHex, used shared presets, state array instead of expandospopup/popup.css— trimmed to popup-only stylescontent/styles/badges.css— trimmed to badge-only styles (611 → 85 lines)manifest.json— added new CSS and JS filespopup/popup.html— added links to new CSS and JS files
New Files
content/styles/color-picker.css— shared picker + preset badge stylescontent/styles/color-popover.css— popover chrome styleslib/presets.js— shared preset CRUD module
Net Impact
- Before: 3,180 lines across 12 source files
- After: 2,862 lines across 15 source files
- Deleted: ~318 lines of duplication and dead code
- No file exceeds 401 lines (previously badges.css was 611)
- Dark mode is now managed from a single source instead of 4 scattered functions
Lessons Learned
- CSS duplication across content script and popup contexts is easy to miss because there's no import graph. Shared CSS files referenced by both manifest.json and popup.html
<link>work well. - Dark mode class management is a natural candidate for centralization — scattered per-element toggling creates CSS selector complexity that cascading from a single ancestor eliminates.
- DOM expando properties work but are an anti-pattern that makes code harder to reason about. A separate state array with DOM references is only marginally more code but significantly clearer.
- The
colorToHex"conversion" function was really just null-coalescing — renaming or inlining it makes the intent obvious.