-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
193 lines (165 loc) · 6.36 KB
/
Copy pathcontent.js
File metadata and controls
193 lines (165 loc) · 6.36 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
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
// Global variables
let removedVideosCount = 0;
let viewThreshold = 1000; // Default threshold
// Get the threshold from storage
function loadThreshold() {
if (chrome.storage && chrome.storage.local) {
chrome.storage.local.get(['viewThreshold'], function(result) {
if (result.viewThreshold) {
viewThreshold = parseInt(result.viewThreshold);
}
});
}
}
// Debug function to help troubleshoot
function debugLog(message, data) {
if (localStorage.getItem('ytViewFilterDebug') === 'true') {
console.log(`[YT View Filter] ${message}`, data);
}
}
// Function to hide videos with fewer than the threshold views
function hideVideosWithLowViews() {
// Reset counter for this run
let currentRunRemoved = 0;
// Select all video items on the home page - use more comprehensive selectors
const videoItems = document.querySelectorAll('ytd-rich-item-renderer, ytd-video-renderer, ytd-grid-video-renderer, ytd-compact-video-renderer');
debugLog(`Processing ${videoItems.length} video items`);
videoItems.forEach(item => {
// Skip already processed items
if (item.dataset.viewsProcessed === 'true') {
return;
}
// Find the view count element - try different selectors based on YouTube's layout
let viewCountElement = item.querySelector('#metadata-line span:nth-child(1)');
// If not found, try alternative selectors
if (!viewCountElement) {
// Try other common YouTube selectors
const possibleSelectors = [
'span.inline-metadata-item.style-scope.ytd-video-meta-block',
'span.style-scope.ytd-video-meta-block',
'div.metadata-snippet-container span',
'span[aria-label*="view"]',
'#metadata-line span',
'span.ytd-video-meta-block'
];
for (const selector of possibleSelectors) {
const elements = item.querySelectorAll(selector);
for (const el of elements) {
if (el.textContent.includes('view')) {
viewCountElement = el;
break;
}
}
if (viewCountElement) break;
}
}
if (viewCountElement) {
const viewText = viewCountElement.textContent.trim();
// Extract the view count number
let viewCount = 0;
if (viewText.includes('view')) {
// Parse the view count - handle different formats
const viewMatch = viewText.match(/(\d+(?:,\d+)*(?:\.\d+)?)\s*([KMB]?)/i);
if (viewMatch) {
// Remove commas from numbers like "1,234"
const numStr = viewMatch[1].replace(/,/g, '');
const num = parseFloat(numStr);
const multiplier = viewMatch[2].toUpperCase();
// Convert to actual number
if (multiplier === 'K') {
viewCount = num * 1000;
} else if (multiplier === 'M') {
viewCount = num * 1000000;
} else if (multiplier === 'B') {
viewCount = num * 1000000000;
} else {
viewCount = num;
}
debugLog(`Found video with ${viewCount} views`, {
text: viewText,
threshold: viewThreshold
});
// Hide videos with fewer than threshold views
if (viewCount < viewThreshold) {
item.style.display = 'none';
currentRunRemoved++;
removedVideosCount++;
debugLog(`Removed video with ${viewCount} views (below threshold ${viewThreshold})`);
}
}
}
}
// Mark as processed
item.dataset.viewsProcessed = 'true';
});
// Update the count in storage
if (chrome.storage && chrome.storage.local) {
chrome.storage.local.set({removedCount: removedVideosCount});
}
return currentRunRemoved;
}
// Listen for messages from popup
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.action === "getCount") {
sendResponse({count: removedVideosCount});
} else if (request.action === "updateThreshold") {
viewThreshold = request.threshold;
// Store the new threshold
if (chrome.storage && chrome.storage.local) {
chrome.storage.local.set({viewThreshold: viewThreshold});
}
// Re-run filtering with new threshold
document.querySelectorAll('ytd-rich-item-renderer, ytd-video-renderer').forEach(item => {
item.dataset.viewsProcessed = 'false';
item.style.display = ''; // Reset display
});
const newlyRemoved = hideVideosWithLowViews();
sendResponse({count: removedVideosCount, newlyRemoved: newlyRemoved});
}
return true;
}
);
// Load saved threshold
loadThreshold();
// Add debug toggle to window object for easier troubleshooting
window.toggleYTViewFilterDebug = function() {
const current = localStorage.getItem('ytViewFilterDebug') === 'true';
localStorage.setItem('ytViewFilterDebug', (!current).toString());
console.log(`[YT View Filter] Debug mode ${!current ? 'enabled' : 'disabled'}`);
return `Debug mode ${!current ? 'enabled' : 'disabled'}`;
};
// Run the function when the page loads
setTimeout(() => {
debugLog('Initial run of hideVideosWithLowViews');
hideVideosWithLowViews();
}, 1500);
// Set up a mutation observer to handle dynamically loaded content
const observer = new MutationObserver((mutations) => {
let shouldProcess = false;
for (const mutation of mutations) {
if (mutation.addedNodes.length) {
// Check if any added nodes are video items or contain video items
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.querySelector('ytd-rich-item-renderer, ytd-video-renderer, ytd-grid-video-renderer, ytd-compact-video-renderer') ||
node.matches('ytd-rich-item-renderer, ytd-video-renderer, ytd-grid-video-renderer, ytd-compact-video-renderer')) {
shouldProcess = true;
break;
}
}
}
}
if (shouldProcess) break;
}
if (shouldProcess) {
debugLog('New content detected, running filter');
hideVideosWithLowViews();
}
});
// Start observing the document with the configured parameters
observer.observe(document.body, { childList: true, subtree: true });
// Add delays to catch videos that load after initial page load
setTimeout(hideVideosWithLowViews, 2000);
setTimeout(hideVideosWithLowViews, 5000);
setTimeout(hideVideosWithLowViews, 10000);