-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube-sort-channel-videos.user.js
More file actions
312 lines (257 loc) · 11 KB
/
youtube-sort-channel-videos.user.js
File metadata and controls
312 lines (257 loc) · 11 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
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
302
303
304
305
306
307
308
309
310
311
312
// ==UserScript==
// @name YouTube Channel - Sort Videos by Views
// @namespace http://tampermonkey.net/
// @version 1.0.3
// @description Sort YouTube channel videos by view count (all loaded videos)
// @author dbeley
// @match https://www.youtube.com/*/videos
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
console.log('[YT Sort by Views] Script loaded');
// Parse view count from text (supports multiple languages)
function parseViewCount(text) {
if (!text) return 0;
const cleaned = text.toLowerCase()
.replace(/views?|vues?|visualizaciones?|aufrufe?/gi, '')
.trim();
const match = cleaned.match(/([\d.,]+)\s*([kmb])?/i);
if (!match) return 0;
let num = parseFloat(match[1].replace(',', '.'));
const suffix = match[2]?.toUpperCase();
if (suffix === 'K') num *= 1000;
else if (suffix === 'M') num *= 1000000;
else if (suffix === 'B') num *= 1000000000;
return Math.floor(num);
}
// Extract all videos with their view counts
function extractVideos() {
const videos = [];
const containers = document.querySelectorAll('ytd-rich-item-renderer');
console.log('[YT Sort by Views] Found', containers.length, 'video containers');
containers.forEach((container, index) => {
try {
const metadataLine = container.querySelector('#metadata-line');
if (!metadataLine) return;
const spans = metadataLine.querySelectorAll('span');
if (spans.length < 1) return;
// Check if first span contains "views" - member-only videos don't show view counts
const firstSpanText = spans[0]?.textContent?.trim() || '';
const firstSpanLower = firstSpanText.toLowerCase();
const hasViewCount = firstSpanLower.includes('view') || firstSpanLower.includes('vue') ||
firstSpanLower.includes('visualizacion') || firstSpanLower.includes('aufruf');
const viewText = hasViewCount ? firstSpanText : '';
const views = parseViewCount(viewText);
videos.push({
container: container,
views: views,
viewText: viewText || '(no views - member-only or hidden)'
});
console.log(`[YT Sort by Views] Video ${index}: ${viewText || '(no views)'} = ${views} views`);
} catch (error) {
console.error('[YT Sort by Views] Error parsing video:', error);
}
});
console.log('[YT Sort by Views] Extracted', videos.length, 'videos');
return videos;
}
// Sort and re-render videos
function sortByViews() {
console.log('[YT Sort by Views] Starting sort...');
const videos = extractVideos();
if (videos.length === 0) {
console.log('[YT Sort by Views] No videos found');
alert('No videos found to sort. Make sure you\'re on a channel\'s videos page.');
return;
}
// Sort by view count (descending)
// Videos with 0 views (likely member-only or no view count) go to the end
videos.sort((a, b) => {
// If both have 0 views, maintain original order
if (a.views === 0 && b.views === 0) return 0;
// If a has 0 views, put it after b
if (a.views === 0) return 1;
// If b has 0 views, put it after a
if (b.views === 0) return -1;
// Otherwise, sort by view count descending
return b.views - a.views;
});
console.log('[YT Sort by Views] Top 5 videos after sorting:');
videos.slice(0, 5).forEach((v, i) => {
console.log(` ${i+1}. ${v.views.toLocaleString()} views`);
});
// Find parent container
const gridContainer = videos[0].container.parentElement;
if (!gridContainer) {
console.error('[YT Sort by Views] Could not find grid container');
return;
}
console.log('[YT Sort by Views] Grid container:', gridContainer.tagName);
// Get all children to preserve structure
const allChildren = Array.from(gridContainer.children);
const videoSet = new Set(videos.map(v => v.container));
// Separate videos from other elements
const videoElements = allChildren.filter(child => videoSet.has(child));
const otherElements = allChildren.filter(child => !videoSet.has(child));
console.log('[YT Sort by Views] Videos:', videoElements.length);
console.log('[YT Sort by Views] Other elements:', otherElements.length);
// Clear and rebuild
gridContainer.innerHTML = '';
// Add sorted videos
videos.forEach(v => {
gridContainer.appendChild(v.container);
});
// Add back other elements at the end
otherElements.forEach(el => {
gridContainer.appendChild(el);
});
console.log('[YT Sort by Views] ✓ Videos re-sorted!');
// Show notification
showNotification(`Sorted ${videos.length} videos by view count!`);
}
// Show a temporary notification
function showNotification(message) {
const notification = document.createElement('div');
notification.textContent = message;
notification.style.cssText = `
position: fixed;
top: 80px;
right: 20px;
background: #0f0f0f;
color: #fff;
padding: 16px 24px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.5);
z-index: 10000;
font-size: 14px;
font-family: "Roboto", sans-serif;
animation: slideIn 0.3s ease;
`;
const style = document.createElement('style');
style.textContent = `
@keyframes slideIn {
from { transform: translateX(400px); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
`;
document.head.appendChild(style);
document.body.appendChild(notification);
setTimeout(() => {
notification.style.animation = 'slideIn 0.3s ease reverse';
setTimeout(() => notification.remove(), 300);
}, 3000);
}
// Create the sort button
function createButton() {
const chipBar = document.querySelector('#chips');
if (!chipBar) {
console.log('[YT Sort by Views] Chip bar not found, will retry...');
return false;
}
// Remove existing button if it exists (for SPA navigation)
const existingButton = document.querySelector('#sort-by-views-btn');
if (existingButton) {
console.log('[YT Sort by Views] Removing existing button');
existingButton.remove();
}
const button = document.createElement('button');
button.id = 'sort-by-views-btn';
button.innerHTML = '📊 Sort by Views';
button.style.cssText = `
padding: 8px 16px;
margin-left: 12px;
background: #3ea6ff;
color: white;
border: none;
border-radius: 18px;
font-size: 14px;
font-weight: 500;
font-family: "Roboto", sans-serif;
cursor: pointer;
transition: background 0.2s;
flex-shrink: 0;
`;
button.addEventListener('mouseenter', () => {
button.style.background = '#3091e0';
});
button.addEventListener('mouseleave', () => {
button.style.background = '#3ea6ff';
});
button.addEventListener('click', () => {
button.disabled = true;
button.textContent = '⏳ Sorting...';
setTimeout(() => {
sortByViews();
button.disabled = false;
button.innerHTML = '📊 Sort by Views';
}, 100);
});
// Find the container div that holds left-arrow, scroll-container, and right-arrow
// Insert the button after scroll-container but before right-arrow
const scrollContainer = chipBar.parentElement; // This is #scroll-container
const containerDiv = scrollContainer.parentElement; // This is #container
const rightArrow = containerDiv.querySelector('#right-arrow');
if (rightArrow) {
// Insert button before the right arrow
containerDiv.insertBefore(button, rightArrow);
} else {
// Fallback: append to container
containerDiv.appendChild(button);
}
console.log('[YT Sort by Views] Button created');
return true;
}
// Check if we're on a channel videos page
function isChannelPage() {
return window.location.pathname.includes('/@') ||
window.location.pathname.includes('/channel/') ||
window.location.pathname.includes('/c/') ||
window.location.pathname.includes('/user/');
}
// Initialize
function init() {
if (!isChannelPage()) {
console.log('[YT Sort by Views] Not on a channel page');
return;
}
console.log('[YT Sort by Views] Initializing...');
// Wait for page to load
let attempts = 0;
const maxAttempts = 20;
const interval = setInterval(() => {
attempts++;
if (createButton()) {
clearInterval(interval);
console.log('[YT Sort by Views] ✓ Ready!');
} else if (attempts >= maxAttempts) {
clearInterval(interval);
console.log('[YT Sort by Views] Could not find chip bar after', maxAttempts, 'attempts');
}
}, 500);
}
// Run on load
init();
// Re-run on navigation (YouTube SPA)
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
console.log('[YT Sort by Views] URL changed to:', url);
// Remove old button immediately on navigation
const existingButton = document.querySelector('#sort-by-views-btn');
if (existingButton) {
existingButton.remove();
}
// Re-initialize after a short delay
setTimeout(init, 500);
}
}).observe(document, { subtree: true, childList: true });
// Expose debug function
window.ytSortDebug = {
sort: sortByViews,
extract: extractVideos
};
})();