-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
301 lines (259 loc) · 8.59 KB
/
contentScript.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
class ContentFilter {
constructor() {
this.categories = {};
this.debug = true;
this.isProcessing = false;
this.initialized = false;
this.initializeFilter();
}
async initializeFilter() {
try {
console.log('Starting Content Filter initialization...');
await this.loadSettings();
this.setupScrollObserver();
this.setupMutationObserver();
this.setupMessageListener();
this.addStyles();
this.initialized = true;
console.log('Content Filter initialized with settings:', this.categories);
// Initial content filtering
this.filterAllContent();
} catch (error) {
console.error('Error initializing Content Filter:', error);
}
}
addStyles() {
const style = document.createElement('style');
style.textContent = `
.filtered-content {
display: none !important;
}
.category-label {
position: absolute;
top: 4px;
right: 4px;
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 2px 6px;
border-radius: 4px;
font-size: 12px;
z-index: 2;
pointer-events: none;
}
/* Hide Shorts section and button */
ytd-reel-shelf-renderer,
ytd-guide-entry-renderer[title="Shorts"],
ytd-mini-guide-entry-renderer[aria-label="Shorts"],
ytd-guide-entry-renderer a[title="Shorts"],
ytd-mini-guide-entry-renderer a[aria-label="Shorts"],
ytd-rich-shelf-renderer[is-shorts] {
display: none !important;
}
/* Everything mode styles */
body.everything-mode ytd-browse[page-subtype="home"] #contents,
body.everything-mode ytd-browse[page-subtype="home"] ytd-rich-grid-renderer,
body.everything-mode ytd-browse[page-subtype="home"] ytd-shelf-renderer,
body.everything-mode ytd-watch-next-secondary-results-renderer {
display: none !important;
}
`;
document.head.appendChild(style);
}
setupMessageListener() {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'ping') {
sendResponse({ status: 'ready' });
return;
}
if (message.type === 'settingsUpdated') {
console.log('Received settings update');
this.handleSettingsUpdate()
.then(() => sendResponse({ status: 'success' }))
.catch(error => {
console.error('Settings update failed:', error);
sendResponse({ status: 'error', error: error.message });
});
return true;
}
});
}
async loadSettings() {
try {
const result = await chrome.storage.sync.get(['categories']);
this.categories = result.categories || {};
console.log('Loaded filter settings:', this.categories);
if (this.categories.everything?.enabled) {
this.handleEverythingMode(true);
}
} catch (error) {
console.error('Error loading settings:', error);
this.categories = {};
}
}
setupScrollObserver() {
this.intersectionObserver = new IntersectionObserver((entries) => {
if (this.isProcessing || (this.categories.everything?.enabled && window.location.pathname === '/')) return;
const hasNewContent = entries.some(entry => entry.isIntersecting);
if (hasNewContent) {
this.filterNewContent();
}
}, {
root: null,
rootMargin: '100px',
threshold: 0.1
});
const containers = document.querySelectorAll('#contents, ytd-rich-grid-renderer');
containers.forEach(container => {
if (container) this.intersectionObserver.observe(container);
});
}
setupMutationObserver() {
this.mutationObserver = new MutationObserver(this.debounce(() => {
if (this.isProcessing) return;
this.removeShorts();
this.filterNewContent();
}, 100));
this.mutationObserver.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['src', 'href']
});
}
removeShorts() {
const shortsElements = document.querySelectorAll(`
ytd-reel-shelf-renderer,
ytd-guide-entry-renderer[title="Shorts"],
ytd-mini-guide-entry-renderer[aria-label="Shorts"],
ytd-guide-entry-renderer a[title="Shorts"],
ytd-mini-guide-entry-renderer a[aria-label="Shorts"],
ytd-rich-shelf-renderer[is-shorts]
`);
shortsElements.forEach(el => el.remove());
}
handleEverythingMode(enabled) {
if (enabled) {
document.body.classList.add('everything-mode');
} else {
document.body.classList.remove('everything-mode');
}
}
async handleSettingsUpdate() {
console.log('Handling settings update...');
await this.loadSettings();
await this.refilterContent();
console.log('Settings update complete');
}
filterAllContent() {
this.isProcessing = true;
try {
console.log('Filtering all content...');
this.removeShorts();
if (this.categories.everything?.enabled) {
this.handleEverythingMode(true);
// Only process videos if not on homepage
if (window.location.pathname !== '/') {
this.processVideos(this.getAllVideos());
}
} else {
this.handleEverythingMode(false);
this.processVideos(this.getAllVideos());
}
} catch (error) {
console.error('Error during filtering:', error);
} finally {
this.isProcessing = false;
}
}
getAllVideos() {
return Array.from(document.querySelectorAll(`
ytd-rich-item-renderer:not([is-shorts]),
ytd-video-renderer:not([is-shorts]),
ytd-compact-video-renderer:not([is-shorts]),
ytd-grid-video-renderer:not([is-shorts])
`));
}
filterNewContent() {
if (!this.initialized) return;
// Don't process new content in everything mode if on homepage
if (this.categories.everything?.enabled && window.location.pathname === '/') return;
this.isProcessing = true;
try {
this.removeShorts();
const newVideos = document.querySelectorAll(`
ytd-rich-item-renderer:not([data-filtered]):not([is-shorts]),
ytd-video-renderer:not([data-filtered]):not([is-shorts]),
ytd-compact-video-renderer:not([data-filtered]):not([is-shorts]),
ytd-grid-video-renderer:not([data-filtered]):not([is-shorts])
`);
if (newVideos.length > 0) {
console.log(`Processing ${newVideos.length} new videos`);
this.processVideos(Array.from(newVideos));
}
} catch (error) {
console.error('Error processing new content:', error);
} finally {
this.isProcessing = false;
}
}
processVideos(videos) {
if (this.categories.everything?.enabled && window.location.pathname === '/') return;
videos.forEach(video => {
try {
if (video.hasAttribute('data-filtered')) return;
const titleElement = video.querySelector('#video-title, .title');
const channelElement = video.querySelector('#channel-name, .channel-name');
if (!titleElement || !channelElement) return;
const title = titleElement.textContent?.trim() || '';
const channelName = channelElement.textContent?.trim() || '';
let shouldHide = false;
if (this.categories.distraction?.enabled) {
shouldHide = this.checkForDistraction(title+" "+channelName);
}
if (shouldHide) {
this.hideElement(video);
} else {
this.showElement(video);
}
video.setAttribute('data-filtered', 'true');
} catch (error) {
console.error('Error processing video:', error);
}
});
}
checkForDistraction(text) {
const keywords = this.categories.distraction?.keywords || [];
const textLower = text.toLowerCase();
return keywords.some(keyword => textLower.includes(keyword.toLowerCase()));
}
hideElement(element) {
if (!element) return;
element.classList.add('filtered-content');
}
showElement(element) {
if (!element) return;
element.classList.remove('filtered-content');
}
resetFiltering() {
console.log('Resetting all filtered content...');
this.handleEverythingMode(false);
document.querySelectorAll('[data-filtered]').forEach(el => {
el.removeAttribute('data-filtered');
this.showElement(el);
});
}
async refilterContent() {
console.log('Refiltering all content...');
this.resetFiltering();
await this.filterAllContent();
}
debounce(func, wait) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
}
// Initialize filter
const contentFilter = new ContentFilter();