feat: add card layout tweak with unified post observer
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
let contentObserver = null;
|
||||
let darkModeObserver = null;
|
||||
let currentSettings = null;
|
||||
|
||||
function applyColorsToContainer(container, settings, subredditName) {
|
||||
@@ -93,52 +91,6 @@
|
||||
});
|
||||
}
|
||||
|
||||
function getObserverTarget() {
|
||||
return (
|
||||
document.getElementById("siteTable") ||
|
||||
document.querySelector('.content[role="main"]') ||
|
||||
document.body
|
||||
);
|
||||
}
|
||||
|
||||
function startContentObserver(settings) {
|
||||
if (contentObserver) contentObserver.disconnect();
|
||||
|
||||
const target = getObserverTarget();
|
||||
contentObserver = new MutationObserver(function (mutations) {
|
||||
let hasNewPosts = false;
|
||||
for (const mutation of mutations) {
|
||||
for (const node of mutation.addedNodes) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE) {
|
||||
if (node.matches && node.matches("div.thing.link")) {
|
||||
hasNewPosts = true;
|
||||
} else if (node.querySelector && node.querySelector("div.thing.link")) {
|
||||
hasNewPosts = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hasNewPosts) processPosts(currentSettings);
|
||||
});
|
||||
|
||||
contentObserver.observe(target, { childList: true, subtree: true });
|
||||
}
|
||||
|
||||
function startDarkModeObserver() {
|
||||
if (darkModeObserver) darkModeObserver.disconnect();
|
||||
|
||||
darkModeObserver = new MutationObserver(function () {
|
||||
if (currentSettings && currentSettings.darkMode === "auto") {
|
||||
updateColors(currentSettings);
|
||||
}
|
||||
});
|
||||
|
||||
darkModeObserver.observe(document.body, {
|
||||
attributes: true,
|
||||
attributeFilter: ["class"],
|
||||
});
|
||||
}
|
||||
|
||||
function updateColors(settings) {
|
||||
currentSettings = settings;
|
||||
const containers = document.querySelectorAll(".reddit-tweaks-badges");
|
||||
@@ -155,8 +107,6 @@
|
||||
function apply(settings) {
|
||||
currentSettings = settings;
|
||||
processPosts(settings);
|
||||
startContentObserver(settings);
|
||||
startDarkModeObserver();
|
||||
}
|
||||
|
||||
window.RedditTweaks = window.RedditTweaks || {};
|
||||
@@ -165,5 +115,6 @@
|
||||
updateColors: updateColors,
|
||||
isDarkMode: window.RedditTweaks.isDarkMode,
|
||||
getColorPrefix: window.RedditTweaks.getColorPrefix,
|
||||
processNewPosts: processPosts,
|
||||
};
|
||||
})();
|
||||
|
||||
59
content/tweaks/cards.js
Normal file
59
content/tweaks/cards.js
Normal file
@@ -0,0 +1,59 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
function processPost(post) {
|
||||
if (post.classList.contains("rt-card")) return;
|
||||
post.classList.add("rt-card");
|
||||
}
|
||||
|
||||
function processPosts() {
|
||||
var posts = document.querySelectorAll("div.thing.link:not(.rt-card)");
|
||||
for (var i = 0; i < posts.length; i++) {
|
||||
processPost(posts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function applyDarkMode(settings) {
|
||||
if (window.RedditTweaks.isDarkMode(settings)) {
|
||||
document.documentElement.classList.add("rt-dark");
|
||||
} else {
|
||||
document.documentElement.classList.remove("rt-dark");
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
function apply(settings) {
|
||||
document.documentElement.classList.add("rt-card-enabled");
|
||||
applyDarkMode(settings);
|
||||
applyBackground(settings);
|
||||
applyGap(settings);
|
||||
processPosts();
|
||||
}
|
||||
|
||||
function updateSettings(settings) {
|
||||
applyDarkMode(settings);
|
||||
applyBackground(settings);
|
||||
applyGap(settings);
|
||||
}
|
||||
|
||||
window.RedditTweaks = window.RedditTweaks || {};
|
||||
window.RedditTweaks.cardTweak = {
|
||||
apply: apply,
|
||||
updateSettings: updateSettings,
|
||||
processNewPosts: processPosts,
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user