-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
122 lines (104 loc) · 3.53 KB
/
Copy pathcontent.js
File metadata and controls
122 lines (104 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
let filterMode = 'hide';
let hiddenChips = [];
let blockedKeywords = [];
let whitelistKeywords = [];
function loadAndApplySettings() {
chrome.storage.sync.get(['filterMode', 'hiddenChips', 'blockedKeywords', 'whitelistKeywords'], (result) => {
filterMode = result.filterMode || 'hide';
hiddenChips = result.hiddenChips || [];
blockedKeywords = result.blockedKeywords ? result.blockedKeywords.split(',').map(k => k.trim().toLowerCase()).filter(k => k) : [];
whitelistKeywords = result.whitelistKeywords ? result.whitelistKeywords.split(',').map(k => k.trim().toLowerCase()).filter(k => k) : [];
applyChipFilters();
applyVideoFilters();
});
}
function applyChipFilters() {
const chips = document.querySelectorAll('ytd-chip-cloud-renderer chip-button');
chips.forEach(chip => {
const chipText = chip.textContent.trim();
if (filterMode === 'hide') {
// Hide mode: hide chips in the list
if (hiddenChips.includes(chipText)) {
chip.style.display = 'none';
} else {
chip.style.display = '';
}
} else if (filterMode === 'showOnly') {
// Show only mode: hide chips NOT in the list
if (hiddenChips.length > 0 && !hiddenChips.includes(chipText)) {
chip.style.display = 'none';
} else {
chip.style.display = '';
}
}
});
}
function applyVideoFilters() {
const videos = document.querySelectorAll('ytd-rich-item-renderer, ytd-grid-video-renderer, ytd-compact-video-renderer, ytd-video-renderer');
videos.forEach(video => {
const titleElement = video.querySelector('#video-title');
if (titleElement) {
const title = titleElement.textContent.toLowerCase();
const channelElement = video.querySelector('#channel-name #text');
const channel = channelElement ? channelElement.textContent.toLowerCase() : '';
let shouldHide = false;
let reason = '';
// Check blocked keywords
if (blockedKeywords.some(keyword => title.includes(keyword))) {
shouldHide = true;
reason = 'blocked';
}
// Check whitelist (if enabled)
if (whitelistKeywords.length > 0 && !shouldHide) {
const matchesWhitelist = whitelistKeywords.some(keyword =>
title.includes(keyword) || channel.includes(keyword)
);
if (!matchesWhitelist) {
shouldHide = true;
reason = 'not in whitelist';
}
}
if (shouldHide) {
video.style.display = 'none';
video.setAttribute('data-tubefilter-hidden', reason);
} else {
video.style.display = '';
video.removeAttribute('data-tubefilter-hidden');
}
}
});
}
// MutationObserver for dynamic content
const observer = new MutationObserver((mutations) => {
let shouldApply = false;
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
shouldApply = true;
}
});
if (shouldApply) {
applyChipFilters();
applyVideoFilters();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Listen for messages from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "refreshFilters") {
loadAndApplySettings();
}
});
// Initial run
loadAndApplySettings();
// Also run on navigation (YouTube SPA)
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
setTimeout(loadAndApplySettings, 500);
}
}).observe(document, {subtree: true, childList: true});