2026-08-26 15:02:46 -04:00
|
|
|
(function () {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2026-08-26 17:16:00 -04:00
|
|
|
var picker = null;
|
|
|
|
|
var activeSwatchEl = null;
|
|
|
|
|
|
|
|
|
|
var COLOR_KEYS = [
|
2026-08-26 15:32:33 -04:00
|
|
|
"light.subredditBgColor",
|
2026-08-26 15:02:46 -04:00
|
|
|
"light.subredditBorderColor",
|
|
|
|
|
"light.subredditTextColor",
|
|
|
|
|
"light.commentsBgColor",
|
|
|
|
|
"light.commentsBorderColor",
|
|
|
|
|
"light.commentsTextColor",
|
2026-08-26 15:32:33 -04:00
|
|
|
"dark.subredditBgColor",
|
2026-08-26 15:02:46 -04:00
|
|
|
"dark.subredditBorderColor",
|
|
|
|
|
"dark.subredditTextColor",
|
|
|
|
|
"dark.commentsBgColor",
|
|
|
|
|
"dark.commentsBorderColor",
|
|
|
|
|
"dark.commentsTextColor",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function colorToHex(val) {
|
2026-08-26 15:32:33 -04:00
|
|
|
if (!val) return "#ffffff";
|
2026-08-26 15:02:46 -04:00
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 17:16:00 -04:00
|
|
|
function getColorValue(el) {
|
|
|
|
|
return el.dataset.value || "#ffffff";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setColorValue(el, val) {
|
|
|
|
|
el.dataset.value = val;
|
|
|
|
|
el.style.backgroundColor = val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openPickerForSwatch(swatchEl) {
|
|
|
|
|
if (!picker) {
|
|
|
|
|
picker = window.RedditTweaks.colorPicker.create();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (picker.isOpen() && activeSwatchEl === swatchEl) {
|
|
|
|
|
closePicker();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
closePicker();
|
|
|
|
|
|
|
|
|
|
activeSwatchEl = swatchEl;
|
|
|
|
|
var parent = swatchEl.closest("label") || swatchEl.parentNode;
|
|
|
|
|
parent.parentNode.insertBefore(picker.el, parent.nextSibling);
|
|
|
|
|
|
|
|
|
|
picker.open(getColorValue(swatchEl), function (hex) {
|
|
|
|
|
setColorValue(swatchEl, hex);
|
|
|
|
|
saveFromControls();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closePicker() {
|
|
|
|
|
if (picker && picker.isOpen()) picker.close();
|
|
|
|
|
activeSwatchEl = null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 15:02:46 -04:00
|
|
|
function populateControls(settings) {
|
|
|
|
|
document.getElementById("enabled").checked = settings.enabled;
|
|
|
|
|
document.getElementById("darkMode").value = settings.darkMode;
|
|
|
|
|
|
|
|
|
|
COLOR_KEYS.forEach(function (key) {
|
2026-08-26 17:16:00 -04:00
|
|
|
var el = document.getElementById(key);
|
|
|
|
|
if (el) setColorValue(el, colorToHex(settings[key]));
|
2026-08-26 15:02:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
renderSubredditOverrides(settings.subredditColors || {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderSubredditOverrides(overrides) {
|
2026-08-26 16:07:07 -04:00
|
|
|
var list = document.getElementById("subredditList");
|
2026-08-26 15:02:46 -04:00
|
|
|
list.innerHTML = "";
|
|
|
|
|
|
2026-08-26 16:07:07 -04:00
|
|
|
Object.entries(overrides).forEach(function (pair) {
|
|
|
|
|
addSubredditEntry(pair[0], pair[1]);
|
2026-08-26 15:02:46 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 16:07:07 -04:00
|
|
|
function addSubredditEntry(name, modeColors) {
|
|
|
|
|
var list = document.getElementById("subredditList");
|
|
|
|
|
var entry = document.createElement("div");
|
2026-08-26 15:02:46 -04:00
|
|
|
entry.className = "subreddit-entry";
|
|
|
|
|
|
2026-08-26 16:07:07 -04:00
|
|
|
var header = document.createElement("div");
|
|
|
|
|
header.className = "subreddit-entry-header";
|
|
|
|
|
|
|
|
|
|
var nameInput = document.createElement("input");
|
2026-08-26 15:02:46 -04:00
|
|
|
nameInput.type = "text";
|
|
|
|
|
nameInput.placeholder = "subreddit";
|
|
|
|
|
nameInput.value = name || "";
|
|
|
|
|
|
2026-08-26 16:07:07 -04:00
|
|
|
var removeBtn = document.createElement("button");
|
|
|
|
|
removeBtn.className = "btn-remove";
|
|
|
|
|
removeBtn.textContent = "\u00d7";
|
|
|
|
|
removeBtn.addEventListener("click", function () {
|
2026-08-26 17:16:00 -04:00
|
|
|
closePicker();
|
2026-08-26 16:07:07 -04:00
|
|
|
entry.remove();
|
|
|
|
|
saveFromControls();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
header.appendChild(nameInput);
|
|
|
|
|
header.appendChild(removeBtn);
|
|
|
|
|
|
|
|
|
|
var modeRow = document.createElement("div");
|
|
|
|
|
modeRow.className = "subreddit-entry-mode";
|
|
|
|
|
|
|
|
|
|
var modeSelect = document.createElement("select");
|
|
|
|
|
modeSelect.innerHTML = '<option value="light">Light</option><option value="dark">Dark</option>';
|
|
|
|
|
modeSelect.value = "light";
|
|
|
|
|
modeRow.appendChild(modeSelect);
|
|
|
|
|
|
|
|
|
|
var colorsDiv = document.createElement("div");
|
|
|
|
|
colorsDiv.className = "subreddit-entry-colors";
|
|
|
|
|
|
|
|
|
|
var lightColors = modeColors && modeColors.light ? modeColors.light : {};
|
|
|
|
|
var darkColors = modeColors && modeColors.dark ? modeColors.dark : {};
|
|
|
|
|
|
2026-08-26 17:16:00 -04:00
|
|
|
var bgSwatch = document.createElement("div");
|
|
|
|
|
bgSwatch.className = "rt-color-swatch";
|
|
|
|
|
setColorValue(bgSwatch, colorToHex(lightColors.bg));
|
|
|
|
|
bgSwatch.title = "Background";
|
2026-08-26 15:02:46 -04:00
|
|
|
|
2026-08-26 17:16:00 -04:00
|
|
|
var borderSwatch = document.createElement("div");
|
|
|
|
|
borderSwatch.className = "rt-color-swatch";
|
|
|
|
|
setColorValue(borderSwatch, colorToHex(lightColors.border));
|
|
|
|
|
borderSwatch.title = "Border";
|
2026-08-26 15:02:46 -04:00
|
|
|
|
2026-08-26 17:16:00 -04:00
|
|
|
var textSwatch = document.createElement("div");
|
|
|
|
|
textSwatch.className = "rt-color-swatch";
|
|
|
|
|
setColorValue(textSwatch, colorToHex(lightColors.text));
|
|
|
|
|
textSwatch.title = "Text";
|
2026-08-26 15:02:46 -04:00
|
|
|
|
2026-08-26 17:16:00 -04:00
|
|
|
colorsDiv.appendChild(bgSwatch);
|
|
|
|
|
colorsDiv.appendChild(borderSwatch);
|
|
|
|
|
colorsDiv.appendChild(textSwatch);
|
2026-08-26 16:07:07 -04:00
|
|
|
|
|
|
|
|
entry._colorState = {
|
2026-08-26 17:16:00 -04:00
|
|
|
light: { bg: getColorValue(bgSwatch), border: getColorValue(borderSwatch), text: getColorValue(textSwatch) },
|
2026-08-26 16:07:07 -04:00
|
|
|
dark: { bg: colorToHex(darkColors.bg), border: colorToHex(darkColors.border), text: colorToHex(darkColors.text) },
|
|
|
|
|
};
|
2026-08-26 16:11:24 -04:00
|
|
|
entry._hadOverride = {
|
|
|
|
|
light: !!(modeColors && modeColors.light),
|
|
|
|
|
dark: !!(modeColors && modeColors.dark),
|
|
|
|
|
};
|
|
|
|
|
entry._dirtyModes = { light: false, dark: false };
|
2026-08-26 16:07:07 -04:00
|
|
|
|
|
|
|
|
modeSelect.addEventListener("change", function () {
|
2026-08-26 17:16:00 -04:00
|
|
|
closePicker();
|
2026-08-26 16:07:07 -04:00
|
|
|
var prev = modeSelect.value === "dark" ? "light" : "dark";
|
2026-08-26 16:11:24 -04:00
|
|
|
entry._dirtyModes[prev] = true;
|
2026-08-26 17:16:00 -04:00
|
|
|
entry._colorState[prev] = { bg: getColorValue(bgSwatch), border: getColorValue(borderSwatch), text: getColorValue(textSwatch) };
|
2026-08-26 16:07:07 -04:00
|
|
|
var next = entry._colorState[modeSelect.value];
|
2026-08-26 17:16:00 -04:00
|
|
|
setColorValue(bgSwatch, next.bg);
|
|
|
|
|
setColorValue(borderSwatch, next.border);
|
|
|
|
|
setColorValue(textSwatch, next.text);
|
2026-08-26 15:02:46 -04:00
|
|
|
});
|
|
|
|
|
|
2026-08-26 17:16:00 -04:00
|
|
|
[bgSwatch, borderSwatch, textSwatch].forEach(function (swatch) {
|
|
|
|
|
swatch.addEventListener("click", function () {
|
|
|
|
|
openPickerForSwatch(swatch);
|
2026-08-26 16:11:24 -04:00
|
|
|
});
|
2026-08-26 15:02:46 -04:00
|
|
|
});
|
|
|
|
|
|
2026-08-26 17:16:00 -04:00
|
|
|
nameInput.addEventListener("change", function () {
|
|
|
|
|
saveFromControls();
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-26 16:07:07 -04:00
|
|
|
entry.appendChild(header);
|
|
|
|
|
entry.appendChild(modeRow);
|
|
|
|
|
entry.appendChild(colorsDiv);
|
|
|
|
|
|
|
|
|
|
entry._nameInput = nameInput;
|
2026-08-26 17:16:00 -04:00
|
|
|
entry._bgSwatch = bgSwatch;
|
|
|
|
|
entry._borderSwatch = borderSwatch;
|
|
|
|
|
entry._textSwatch = textSwatch;
|
2026-08-26 16:07:07 -04:00
|
|
|
entry._modeSelect = modeSelect;
|
|
|
|
|
|
2026-08-26 15:02:46 -04:00
|
|
|
list.appendChild(entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function collectSubredditOverrides() {
|
2026-08-26 16:07:07 -04:00
|
|
|
var overrides = {};
|
|
|
|
|
var entries = document.querySelectorAll(".subreddit-entry");
|
2026-08-26 15:02:46 -04:00
|
|
|
entries.forEach(function (entry) {
|
2026-08-26 16:07:07 -04:00
|
|
|
var name = entry._nameInput.value.trim().replace(/^r\//, "");
|
|
|
|
|
if (!name) return;
|
|
|
|
|
|
|
|
|
|
var currentMode = entry._modeSelect.value;
|
|
|
|
|
entry._colorState[currentMode] = {
|
2026-08-26 17:16:00 -04:00
|
|
|
bg: getColorValue(entry._bgSwatch),
|
|
|
|
|
border: getColorValue(entry._borderSwatch),
|
|
|
|
|
text: getColorValue(entry._textSwatch),
|
2026-08-26 16:07:07 -04:00
|
|
|
};
|
|
|
|
|
|
2026-08-26 16:11:24 -04:00
|
|
|
var entryOverrides = {};
|
|
|
|
|
if (entry._hadOverride.light || entry._dirtyModes.light) {
|
|
|
|
|
entryOverrides.light = entry._colorState.light;
|
|
|
|
|
}
|
|
|
|
|
if (entry._hadOverride.dark || entry._dirtyModes.dark) {
|
|
|
|
|
entryOverrides.dark = entry._colorState.dark;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entryOverrides.light || entryOverrides.dark) {
|
|
|
|
|
overrides[name] = entryOverrides;
|
|
|
|
|
}
|
2026-08-26 15:02:46 -04:00
|
|
|
});
|
|
|
|
|
return overrides;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 15:05:48 -04:00
|
|
|
async function saveFromControls() {
|
2026-08-26 17:16:00 -04:00
|
|
|
var settings = {
|
2026-08-26 15:02:46 -04:00
|
|
|
enabled: document.getElementById("enabled").checked,
|
|
|
|
|
darkMode: document.getElementById("darkMode").value,
|
|
|
|
|
subredditColors: collectSubredditOverrides(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
COLOR_KEYS.forEach(function (key) {
|
2026-08-26 17:16:00 -04:00
|
|
|
var el = document.getElementById(key);
|
|
|
|
|
if (el) settings[key] = getColorValue(el);
|
2026-08-26 15:02:46 -04:00
|
|
|
});
|
|
|
|
|
|
2026-08-26 15:05:48 -04:00
|
|
|
try {
|
|
|
|
|
await window.RedditTweaks.saveSettings(settings);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Reddit Tweaks: failed to save settings", err);
|
|
|
|
|
}
|
2026-08-26 15:02:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function init() {
|
2026-08-26 17:16:00 -04:00
|
|
|
var settings = await window.RedditTweaks.loadSettings();
|
2026-08-26 15:02:46 -04:00
|
|
|
populateControls(settings);
|
|
|
|
|
|
|
|
|
|
document.getElementById("enabled").addEventListener("change", saveFromControls);
|
|
|
|
|
document.getElementById("darkMode").addEventListener("change", saveFromControls);
|
|
|
|
|
|
|
|
|
|
COLOR_KEYS.forEach(function (key) {
|
2026-08-26 17:16:00 -04:00
|
|
|
var el = document.getElementById(key);
|
|
|
|
|
if (el) {
|
|
|
|
|
el.addEventListener("click", function () {
|
|
|
|
|
openPickerForSwatch(el);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-08-26 15:02:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
document.getElementById("addSubreddit").addEventListener("click", function () {
|
2026-08-26 17:16:00 -04:00
|
|
|
closePicker();
|
2026-08-26 15:02:46 -04:00
|
|
|
addSubredditEntry("", {});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
document.getElementById("resetLight").addEventListener("click", async function () {
|
2026-08-26 17:16:00 -04:00
|
|
|
closePicker();
|
|
|
|
|
var settings = await window.RedditTweaks.loadSettings();
|
|
|
|
|
var DEFAULTS = window.RedditTweaks.DEFAULTS;
|
2026-08-26 15:02:46 -04:00
|
|
|
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 () {
|
2026-08-26 17:16:00 -04:00
|
|
|
closePicker();
|
|
|
|
|
var settings = await window.RedditTweaks.loadSettings();
|
|
|
|
|
var DEFAULTS = window.RedditTweaks.DEFAULTS;
|
2026-08-26 15:02:46 -04:00
|
|
|
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?")) {
|
2026-08-26 17:16:00 -04:00
|
|
|
closePicker();
|
2026-08-26 15:02:46 -04:00
|
|
|
await window.RedditTweaks.resetSettings();
|
2026-08-26 17:16:00 -04:00
|
|
|
var settings = await window.RedditTweaks.loadSettings();
|
2026-08-26 15:02:46 -04:00
|
|
|
populateControls(settings);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
})();
|