Files
reddit-tweaks-ffe/content/tweaks/cards.js
cottongin 00507fe87c refactor: decompose CSS, unify dark mode, extract shared modules
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>
2026-08-28 01:39:25 -04:00

83 lines
2.2 KiB
JavaScript

(function () {
"use strict";
function processPost(post) {
if (post.classList.contains("rt-card")) return;
post.classList.add("rt-card");
var rank = post.querySelector(":scope > .rank");
if (rank) {
rank.classList.add("rt-rank-badge");
post.appendChild(rank);
}
}
function processPosts() {
var posts = document.querySelectorAll("div.thing.link:not(.rt-card)");
for (var i = 0; i < posts.length; i++) {
processPost(posts[i]);
}
}
function applyBackground(settings) {
if (settings.cardBackground === "shadcn") {
document.documentElement.classList.add("rt-card-bg-shadcn");
} else {
document.documentElement.classList.remove("rt-card-bg-shadcn");
}
}
function applyGap(settings) {
var siteTable = document.getElementById("siteTable");
if (siteTable) {
siteTable.style.setProperty("--rt-card-gap", settings.cardGap + "px");
}
}
var DENSITY_CLASSES = ["rt-density-cozy", "rt-density-modern", "rt-density-extra"];
function applyDensity(settings) {
var html = document.documentElement;
for (var i = 0; i < DENSITY_CLASSES.length; i++) {
html.classList.remove(DENSITY_CLASSES[i]);
}
var cls = "rt-density-" + (settings.cardDensity || "modern");
html.classList.add(cls);
}
function initSelection() {
document.addEventListener("mousedown", function (e) {
var clickedCard = e.target.closest(".rt-card");
var prev = document.querySelectorAll(".rt-card.rt-selected");
for (var i = 0; i < prev.length; i++) {
prev[i].classList.remove("rt-selected");
}
if (clickedCard) {
clickedCard.classList.add("rt-selected");
}
});
}
function apply(settings) {
document.documentElement.classList.add("rt-card-enabled");
applyBackground(settings);
applyDensity(settings);
applyGap(settings);
processPosts();
initSelection();
}
function updateSettings(settings) {
applyBackground(settings);
applyDensity(settings);
applyGap(settings);
}
window.RedditTweaks = window.RedditTweaks || {};
window.RedditTweaks.cardTweak = {
apply: apply,
updateSettings: updateSettings,
processNewPosts: processPosts,
};
})();