Enhance popup and content script: add debug toggle, reset defaults button, and improve settings loading
This commit is contained in:
parent
351116c85a
commit
debdc67ed6
79
content.js
79
content.js
@ -1,7 +1,5 @@
|
|||||||
console.log("[MemberFilter] content script loaded");
|
|
||||||
|
|
||||||
const seen = new WeakSet();
|
|
||||||
let whitelist = [];
|
let whitelist = [];
|
||||||
|
let debugEnabled = false;
|
||||||
|
|
||||||
/* ---------------- CSS injection ---------------- */
|
/* ---------------- CSS injection ---------------- */
|
||||||
|
|
||||||
@ -22,56 +20,73 @@ injectStyleOnce();
|
|||||||
|
|
||||||
/* ---------------- load whitelist ---------------- */
|
/* ---------------- load whitelist ---------------- */
|
||||||
|
|
||||||
function loadWhitelist() {
|
function debugLog(...args) {
|
||||||
chrome.storage.local.get({ whitelist: [] }, data => {
|
if (!debugEnabled) return;
|
||||||
|
console.log("[MemberFilter]", ...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadSettings() {
|
||||||
|
chrome.storage.local.get({ whitelist: [], debug: false }, data => {
|
||||||
whitelist = data.whitelist.map(n => n.toLowerCase());
|
whitelist = data.whitelist.map(n => n.toLowerCase());
|
||||||
console.log("[MemberFilter] whitelist loaded:", whitelist);
|
debugEnabled = Boolean(data.debug);
|
||||||
|
debugLog("settings loaded:", { whitelist, debugEnabled });
|
||||||
|
process();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
loadWhitelist();
|
loadSettings();
|
||||||
|
|
||||||
// Reload whitelist if popup changes it
|
// Reload settings if popup changes them
|
||||||
chrome.storage.onChanged.addListener(loadWhitelist);
|
chrome.storage.onChanged.addListener(loadSettings);
|
||||||
|
|
||||||
/* ---------------- detection logic ---------------- */
|
/* ---------------- detection logic ---------------- */
|
||||||
|
|
||||||
|
function isMemberOnly(video) {
|
||||||
|
const badges = video.querySelectorAll("badge-shape");
|
||||||
|
for (const badge of badges) {
|
||||||
|
if (badge.textContent?.includes("Nur für Kanalmitglieder")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
function process(root = document) {
|
function process(root = document) {
|
||||||
const badges = root.querySelectorAll("badge-shape");
|
const selector =
|
||||||
|
"ytd-rich-item-renderer, ytd-video-renderer, ytd-grid-video-renderer";
|
||||||
|
const videos = root.querySelectorAll(selector);
|
||||||
|
const rootIsVideo =
|
||||||
|
root instanceof HTMLElement && root.matches && root.matches(selector);
|
||||||
|
|
||||||
badges.forEach(badge => {
|
const allVideos = rootIsVideo ? [root, ...videos] : Array.from(videos);
|
||||||
if (!badge.textContent?.includes("Nur für Kanalmitglieder")) return;
|
|
||||||
|
|
||||||
const video = badge.closest(
|
|
||||||
"ytd-rich-item-renderer, ytd-video-renderer, ytd-grid-video-renderer"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!video) return;
|
|
||||||
if (seen.has(video)) return;
|
|
||||||
|
|
||||||
seen.add(video);
|
|
||||||
|
|
||||||
|
allVideos.forEach(video => {
|
||||||
const titleEl = video.querySelector("#video-title");
|
const titleEl = video.querySelector("#video-title");
|
||||||
const channelEl =
|
const channelEl =
|
||||||
video.querySelector("ytd-channel-name a") ||
|
video.querySelector("ytd-channel-name a") ||
|
||||||
video.querySelector(".ytd-channel-name a");
|
video.querySelector(".ytd-channel-name a");
|
||||||
|
|
||||||
const title = titleEl?.textContent?.trim() ?? "(no title)";
|
const channel = channelEl?.textContent?.trim() ?? "";
|
||||||
const url = titleEl?.href ?? "(no url)";
|
|
||||||
const channel = channelEl?.textContent?.trim() ?? "(no channel)";
|
|
||||||
const channelKey = channel.toLowerCase();
|
const channelKey = channel.toLowerCase();
|
||||||
|
|
||||||
const whitelisted = whitelist.includes(channelKey);
|
const whitelisted = whitelist.includes(channelKey);
|
||||||
|
const memberOnly = isMemberOnly(video);
|
||||||
|
|
||||||
console.group("[MemberFilter]");
|
if (debugEnabled) {
|
||||||
console.log("Title :", title);
|
const title = titleEl?.textContent?.trim() ?? "(no title)";
|
||||||
console.log("Channel:", channel);
|
const url = titleEl?.href ?? "(no url)";
|
||||||
console.log("URL :", url);
|
console.group("[MemberFilter]");
|
||||||
console.log("Whitelisted:", whitelisted);
|
console.log("Title :", title);
|
||||||
console.groupEnd();
|
console.log("Channel:", channel || "(no channel)");
|
||||||
|
console.log("URL :", url);
|
||||||
|
console.log("MemberOnly:", memberOnly);
|
||||||
|
console.log("Whitelisted:", whitelisted);
|
||||||
|
console.groupEnd();
|
||||||
|
}
|
||||||
|
|
||||||
if (!whitelisted) {
|
if (memberOnly && !whitelisted) {
|
||||||
video.setAttribute("data-member-filter-hidden", "true");
|
video.setAttribute("data-member-filter-hidden", "true");
|
||||||
|
} else {
|
||||||
|
video.removeAttribute("data-member-filter-hidden");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
75
popup.css
75
popup.css
@ -1,14 +1,85 @@
|
|||||||
body {
|
body {
|
||||||
font-family: sans-serif;
|
font-family: "Trebuchet MS", "Verdana", sans-serif;
|
||||||
min-width: 220px;
|
min-width: 260px;
|
||||||
|
margin: 0;
|
||||||
|
padding: 12px;
|
||||||
|
background: linear-gradient(180deg, #f7f3e8, #efe6d2);
|
||||||
|
color: #2b2014;
|
||||||
|
}
|
||||||
|
|
||||||
|
h3 {
|
||||||
|
margin: 0 0 10px;
|
||||||
|
letter-spacing: 0.4px;
|
||||||
|
text-transform: uppercase;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row {
|
||||||
|
display: flex;
|
||||||
|
gap: 6px;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row.settings {
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|
||||||
|
input {
|
||||||
|
flex: 1;
|
||||||
|
padding: 6px 8px;
|
||||||
|
border: 1px solid #cdbf9e;
|
||||||
|
border-radius: 6px;
|
||||||
|
background: #fffaf0;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
padding: 6px 10px;
|
||||||
|
border: 1px solid #b2985b;
|
||||||
|
border-radius: 6px;
|
||||||
|
background: #d9c18c;
|
||||||
|
color: #2b2014;
|
||||||
|
font-size: 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.ghost {
|
||||||
|
background: transparent;
|
||||||
|
border-color: #cdbf9e;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
filter: brightness(0.95);
|
||||||
}
|
}
|
||||||
|
|
||||||
ul {
|
ul {
|
||||||
padding-left: 0;
|
padding-left: 0;
|
||||||
|
margin: 0;
|
||||||
|
list-style: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
li {
|
li {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
margin: 4px 0;
|
margin: 4px 0;
|
||||||
|
padding: 6px 8px;
|
||||||
|
background: #fffaf0;
|
||||||
|
border: 1px solid #e2d7be;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
li button {
|
||||||
|
padding: 2px 8px;
|
||||||
|
border-radius: 999px;
|
||||||
|
border-color: #c95a3a;
|
||||||
|
background: #f2a285;
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle {
|
||||||
|
display: flex;
|
||||||
|
gap: 6px;
|
||||||
|
align-items: center;
|
||||||
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|||||||
14
popup.html
14
popup.html
@ -8,8 +8,18 @@
|
|||||||
<body>
|
<body>
|
||||||
<h3>Channel Whitelist</h3>
|
<h3>Channel Whitelist</h3>
|
||||||
|
|
||||||
<input id="channelInput" placeholder="Channel name" />
|
<div class="row">
|
||||||
<button id="addBtn">Add</button>
|
<input id="channelInput" placeholder="Channel name" />
|
||||||
|
<button id="addBtn">Add</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row settings">
|
||||||
|
<label class="toggle">
|
||||||
|
<input id="debugToggle" type="checkbox" />
|
||||||
|
<span>Debug</span>
|
||||||
|
</label>
|
||||||
|
<button id="resetBtn" class="ghost">Reset defaults</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<ul id="channelList"></ul>
|
<ul id="channelList"></ul>
|
||||||
|
|
||||||
|
|||||||
24
popup.js
24
popup.js
@ -1,10 +1,18 @@
|
|||||||
const input = document.getElementById("channelInput");
|
const input = document.getElementById("channelInput");
|
||||||
const addBtn = document.getElementById("addBtn");
|
const addBtn = document.getElementById("addBtn");
|
||||||
const list = document.getElementById("channelList");
|
const list = document.getElementById("channelList");
|
||||||
|
const debugToggle = document.getElementById("debugToggle");
|
||||||
|
const resetBtn = document.getElementById("resetBtn");
|
||||||
|
|
||||||
|
const DEFAULTS = {
|
||||||
|
whitelist: [],
|
||||||
|
debug: false
|
||||||
|
};
|
||||||
|
|
||||||
function loadChannels() {
|
function loadChannels() {
|
||||||
chrome.storage.local.get({ whitelist: [] }, ({ whitelist }) => {
|
chrome.storage.local.get(DEFAULTS, ({ whitelist, debug }) => {
|
||||||
list.innerHTML = "";
|
list.innerHTML = "";
|
||||||
|
debugToggle.checked = Boolean(debug);
|
||||||
whitelist.forEach((name, index) => {
|
whitelist.forEach((name, index) => {
|
||||||
const li = document.createElement("li");
|
const li = document.createElement("li");
|
||||||
li.textContent = name;
|
li.textContent = name;
|
||||||
@ -23,7 +31,7 @@ function addChannel() {
|
|||||||
const name = input.value.trim();
|
const name = input.value.trim();
|
||||||
if (!name) return;
|
if (!name) return;
|
||||||
|
|
||||||
chrome.storage.local.get({ whitelist: [] }, ({ whitelist }) => {
|
chrome.storage.local.get(DEFAULTS, ({ whitelist }) => {
|
||||||
whitelist.push(name);
|
whitelist.push(name);
|
||||||
chrome.storage.local.set({ whitelist }, loadChannels);
|
chrome.storage.local.set({ whitelist }, loadChannels);
|
||||||
});
|
});
|
||||||
@ -32,11 +40,21 @@ function addChannel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function removeChannel(index) {
|
function removeChannel(index) {
|
||||||
chrome.storage.local.get({ whitelist: [] }, ({ whitelist }) => {
|
chrome.storage.local.get(DEFAULTS, ({ whitelist }) => {
|
||||||
whitelist.splice(index, 1);
|
whitelist.splice(index, 1);
|
||||||
chrome.storage.local.set({ whitelist }, loadChannels);
|
chrome.storage.local.set({ whitelist }, loadChannels);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setDebug(enabled) {
|
||||||
|
chrome.storage.local.set({ debug: Boolean(enabled) });
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetDefaults() {
|
||||||
|
chrome.storage.local.set(DEFAULTS, loadChannels);
|
||||||
|
}
|
||||||
|
|
||||||
addBtn.onclick = addChannel;
|
addBtn.onclick = addChannel;
|
||||||
|
debugToggle.onchange = e => setDebug(e.target.checked);
|
||||||
|
resetBtn.onclick = resetDefaults;
|
||||||
loadChannels();
|
loadChannels();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user