Files
reddit-tweaks-ffe/popup/popup.js
cottongin cca7940dd9 fix: subreddit badge fill, tagline 'to' cleanup, and comments li spacing
- Default subreddit badge background from transparent to opaque fill
  (light: #fde8e0, dark: #3d2518) so page highlights don't bleed through
- Remove lingering "to" text node from tagline when subreddit is hidden
- Hide the <li> wrapping the comments link (not just the <a>) to
  eliminate leftover whitespace in the flat-list
- Simplify popup: subreddit bg fields are now color pickers (no longer
  need transparent text-input workaround)

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-26 15:32:33 -04:00

173 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(function () {
"use strict";
const COLOR_KEYS = [
"light.subredditBgColor",
"light.subredditBorderColor",
"light.subredditTextColor",
"light.commentsBgColor",
"light.commentsBorderColor",
"light.commentsTextColor",
"dark.subredditBgColor",
"dark.subredditBorderColor",
"dark.subredditTextColor",
"dark.commentsBgColor",
"dark.commentsBorderColor",
"dark.commentsTextColor",
];
function colorToHex(val) {
if (!val) return "#ffffff";
return val;
}
function populateControls(settings) {
document.getElementById("enabled").checked = settings.enabled;
document.getElementById("darkMode").value = settings.darkMode;
COLOR_KEYS.forEach(function (key) {
const el = document.getElementById(key);
if (el) el.value = colorToHex(settings[key]);
});
renderSubredditOverrides(settings.subredditColors || {});
}
function renderSubredditOverrides(overrides) {
const list = document.getElementById("subredditList");
list.innerHTML = "";
Object.entries(overrides).forEach(function ([name, colors]) {
addSubredditEntry(name, colors);
});
}
function addSubredditEntry(name, colors) {
const list = document.getElementById("subredditList");
const entry = document.createElement("div");
entry.className = "subreddit-entry";
const nameInput = document.createElement("input");
nameInput.type = "text";
nameInput.placeholder = "subreddit";
nameInput.value = name || "";
const bgInput = document.createElement("input");
bgInput.type = "color";
bgInput.value = colorToHex(colors?.bg);
bgInput.title = "Background";
const borderInput = document.createElement("input");
borderInput.type = "color";
borderInput.value = colorToHex(colors?.border);
borderInput.title = "Border";
const textInput = document.createElement("input");
textInput.type = "color";
textInput.value = colorToHex(colors?.text);
textInput.title = "Text";
const removeBtn = document.createElement("button");
removeBtn.className = "btn-remove";
removeBtn.textContent = "×";
removeBtn.addEventListener("click", function () {
entry.remove();
saveFromControls();
});
[nameInput, bgInput, borderInput, textInput].forEach(function (input) {
input.addEventListener("change", saveFromControls);
});
entry.appendChild(nameInput);
entry.appendChild(bgInput);
entry.appendChild(borderInput);
entry.appendChild(textInput);
entry.appendChild(removeBtn);
list.appendChild(entry);
}
function collectSubredditOverrides() {
const overrides = {};
const entries = document.querySelectorAll(".subreddit-entry");
entries.forEach(function (entry) {
const inputs = entry.querySelectorAll("input");
const name = inputs[0].value.trim().replace(/^r\//, "");
if (name) {
overrides[name] = {
bg: inputs[1].value,
border: inputs[2].value,
text: inputs[3].value,
};
}
});
return overrides;
}
async function saveFromControls() {
const settings = {
enabled: document.getElementById("enabled").checked,
darkMode: document.getElementById("darkMode").value,
subredditColors: collectSubredditOverrides(),
};
COLOR_KEYS.forEach(function (key) {
const el = document.getElementById(key);
if (el) settings[key] = el.value;
});
try {
await window.RedditTweaks.saveSettings(settings);
} catch (err) {
console.error("Reddit Tweaks: failed to save settings", err);
}
}
async function init() {
const settings = await window.RedditTweaks.loadSettings();
populateControls(settings);
document.getElementById("enabled").addEventListener("change", saveFromControls);
document.getElementById("darkMode").addEventListener("change", saveFromControls);
COLOR_KEYS.forEach(function (key) {
const el = document.getElementById(key);
if (el) el.addEventListener("input", saveFromControls);
});
document.getElementById("addSubreddit").addEventListener("click", function () {
addSubredditEntry("", {});
});
document.getElementById("resetLight").addEventListener("click", async function () {
const settings = await window.RedditTweaks.loadSettings();
const DEFAULTS = window.RedditTweaks.DEFAULTS;
Object.keys(DEFAULTS).forEach(function (key) {
if (key.startsWith("light.")) settings[key] = DEFAULTS[key];
});
await window.RedditTweaks.saveSettings(settings);
populateControls(settings);
});
document.getElementById("resetDark").addEventListener("click", async function () {
const settings = await window.RedditTweaks.loadSettings();
const DEFAULTS = window.RedditTweaks.DEFAULTS;
Object.keys(DEFAULTS).forEach(function (key) {
if (key.startsWith("dark.")) settings[key] = DEFAULTS[key];
});
await window.RedditTweaks.saveSettings(settings);
populateControls(settings);
});
document.getElementById("resetAll").addEventListener("click", async function () {
if (confirm("Reset all settings to defaults?")) {
await window.RedditTweaks.resetSettings();
const settings = await window.RedditTweaks.loadSettings();
populateControls(settings);
}
});
}
init();
})();