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>
114 lines
3.4 KiB
JavaScript
114 lines
3.4 KiB
JavaScript
(function () {
|
|
"use strict";
|
|
|
|
function startDarkModeObserver(settings) {
|
|
var observer = new MutationObserver(function () {
|
|
window.RedditTweaks.loadSettings().then(function (updated) {
|
|
if (updated.darkMode === "auto") {
|
|
window.RedditTweaks.applyDarkModeClass(updated);
|
|
if (updated.badgeLayout) {
|
|
window.RedditTweaks.badgeTweak.updateColors(updated);
|
|
}
|
|
if (updated.cardLayout) {
|
|
window.RedditTweaks.cardTweak.updateSettings(updated);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
observer.observe(document.body, {
|
|
attributes: true,
|
|
attributeFilter: ["class"],
|
|
});
|
|
}
|
|
|
|
function startPostObserver(settings) {
|
|
var target =
|
|
document.getElementById("siteTable") ||
|
|
document.querySelector('.content[role="main"]') ||
|
|
document.body;
|
|
|
|
var observer = new MutationObserver(function (mutations) {
|
|
var hasNewPosts = false;
|
|
for (var i = 0; i < mutations.length; i++) {
|
|
var added = mutations[i].addedNodes;
|
|
for (var j = 0; j < added.length; j++) {
|
|
var node = added[j];
|
|
if (node.nodeType !== Node.ELEMENT_NODE) continue;
|
|
if ((node.matches && node.matches("div.thing.link")) ||
|
|
(node.querySelector && node.querySelector("div.thing.link"))) {
|
|
hasNewPosts = true;
|
|
break;
|
|
}
|
|
}
|
|
if (hasNewPosts) break;
|
|
}
|
|
if (hasNewPosts) {
|
|
window.RedditTweaks.loadSettings().then(function (updated) {
|
|
if (updated.badgeLayout) {
|
|
window.RedditTweaks.badgeTweak.processNewPosts(updated);
|
|
}
|
|
if (updated.cardLayout) {
|
|
window.RedditTweaks.cardTweak.processNewPosts(updated);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
observer.observe(target, { childList: true, subtree: true });
|
|
}
|
|
|
|
async function init() {
|
|
try {
|
|
var settings = await window.RedditTweaks.loadSettings();
|
|
if (!settings.enabled) return;
|
|
|
|
window.RedditTweaks.applyDarkModeClass(settings);
|
|
|
|
if (settings.badgeLayout) {
|
|
document.documentElement.classList.add("rt-badges-enabled");
|
|
window.RedditTweaks.badgeTweak.apply(settings);
|
|
}
|
|
|
|
if (settings.fontOverride) {
|
|
document.documentElement.classList.add("rt-fonts-enabled");
|
|
}
|
|
|
|
if (settings.cardLayout) {
|
|
window.RedditTweaks.cardTweak.apply(settings);
|
|
}
|
|
|
|
if (settings.badgeLayout || settings.cardLayout) {
|
|
startPostObserver(settings);
|
|
startDarkModeObserver(settings);
|
|
}
|
|
|
|
var RELOAD_KEYS = ["enabled", "badgeLayout", "fontOverride", "cardLayout"];
|
|
|
|
browser.storage.onChanged.addListener(function (changes) {
|
|
var needsReload = RELOAD_KEYS.some(function (key) {
|
|
return changes[key] && changes[key].newValue !== changes[key].oldValue;
|
|
});
|
|
if (needsReload) {
|
|
location.reload();
|
|
return;
|
|
}
|
|
|
|
window.RedditTweaks.loadSettings().then(function (updated) {
|
|
window.RedditTweaks.applyDarkModeClass(updated);
|
|
if (updated.badgeLayout) {
|
|
window.RedditTweaks.badgeTweak.updateColors(updated);
|
|
}
|
|
if (updated.cardLayout) {
|
|
window.RedditTweaks.cardTweak.updateSettings(updated);
|
|
}
|
|
});
|
|
});
|
|
} catch (err) {
|
|
console.error("RedditTweaks init failed:", err);
|
|
}
|
|
}
|
|
|
|
init();
|
|
})();
|