-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube-toggle-videos-buttons.user.js
450 lines (373 loc) · 13.1 KB
/
youtube-toggle-videos-buttons.user.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// ==UserScript==
// @name YouTube - Toggle videos buttons
// @description Adds buttons to filter out videos by type and/or status. The toggles can be hidden/shown at any time by pressing the button added to the header.
// @version 2023.06.13.17.21
// @author MetalTxus
// @namespace https://github.com/jesuscc1993
// @icon https://www.youtube.com/favicon.ico
// @match *://*.youtube.com/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @grant GM.getValue
// @grant GM.setValue
// ==/UserScript==
/* globals jQuery */
(async () => {
'use strict';
const enableDebug = false;
let currentUrl;
let videosTotal;
let buttonsContainer;
let toggleButtonsButton;
let toggleLiveButton;
let toggleShortsButton;
let toggleUpcomingButton;
let toggleUploadsButton;
let toggleWatchedButton;
let buttonsHidden = await GM.getValue('buttonsHidden', false);
let liveHidden = await GM.getValue('liveHidden', false);
let shortsHidden = await GM.getValue('shortsHidden', false);
let upcomingHidden = await GM.getValue('upcomingHidden', false);
let uploadsHidden = await GM.getValue('uploadsHidden', false);
let watchedHidden = await GM.getValue('watchedHidden', false);
const shouldRenderButton = () => {
return location.href.match(urlPattern) !== null;
};
const shouldFilterByTypeButton = () => {
return location.href.match(urlWithTypesPattern) !== null;
};
const shouldFilterByStatus = () => {
return true;
};
const shouldRunScript = () => {
const oldUrl = currentUrl;
currentUrl = location.href.split('?')[0];
const oldVideosTotal = videosTotal;
videosTotal = jQuery(videosSelector).length;
const locationChanged = !!oldUrl && oldUrl !== currentUrl;
const videosCountChanged = oldVideosTotal !== videosTotal;
const videosShouldBeHidden =
(liveHidden ||
shortsHidden ||
upcomingHidden ||
uploadsHidden ||
watchedHidden) &&
!!document.querySelectorAll(unprocessedVideosSelectors).length;
const videosShouldBeShown =
!(
liveHidden &&
shortsHidden &&
upcomingHidden &&
uploadsHidden &&
watchedHidden
) && !!document.querySelectorAll(processedVideosSelectors).length;
const shouldIt =
shouldRenderButton() &&
(locationChanged ||
videosCountChanged ||
videosShouldBeHidden ||
videosShouldBeShown);
if (shouldIt) {
debug(`Videos should be processed
locationChanged: ${locationChanged}
videosCountChanged: ${videosCountChanged}
videosShouldBeHidden: ${videosShouldBeHidden}
videosShouldBeShown: ${videosShouldBeShown}`);
}
return shouldIt;
};
const runButtonTask = () => {
if (shouldRenderButton()) {
const buttonsDestinationContainer = jQuery(
buttonDestinationContainerSelector
);
if (
buttonsDestinationContainer.length &&
!buttonsDestinationContainer.find(buttonsContainer).length
) {
insertButtons(buttonsDestinationContainer);
}
} else {
buttonsContainer.remove();
toggleButtonsButton.remove();
}
};
const runVideosTask = () => {
if (shouldRunScript()) {
setTimeout(processAllVideos, 150);
}
};
const insertButtons = (buttonDestinationContainer) => {
toggleLiveButton.off('click').on('click', toggleLiveVideos);
toggleShortsButton.off('click').on('click', toggleShortsVideos);
toggleUpcomingButton.off('click').on('click', toggleUpcomingVideos);
toggleUploadsButton.off('click').on('click', toggleUploadsVideos);
toggleWatchedButton.off('click').on('click', toggleWatchedVideos);
setButtonState(toggleLiveButton, liveHidden);
setButtonState(toggleShortsButton, shortsHidden);
setButtonState(toggleUpcomingButton, upcomingHidden);
setButtonState(toggleUploadsButton, uploadsHidden);
setButtonState(toggleWatchedButton, watchedHidden);
buttonDestinationContainer.prepend(buttonsContainer);
toggleButtonsButton.off('click').on('click', toggleButtons);
jQuery(buttonsToggleDestinationSelector).prepend(toggleButtonsButton);
};
const processAllVideos = () => {
debug(`Processing videos...`);
if (liveHidden) processLiveVideos();
if (shortsHidden) processShortsVideos();
if (upcomingHidden) processUpcomingVideos();
if (uploadsHidden) processUploadsVideos();
if (watchedHidden) processWatchedVideos();
debug(`All videos processed`);
};
const toggleLiveVideos = () => {
liveHidden = !liveHidden;
GM.setValue('liveHidden', liveHidden);
processLiveVideos();
};
const toggleShortsVideos = () => {
shortsHidden = !shortsHidden;
GM.setValue('shortsHidden', shortsHidden);
processShortsVideos();
};
const toggleUpcomingVideos = () => {
upcomingHidden = !upcomingHidden;
GM.setValue('upcomingHidden', upcomingHidden);
processUpcomingVideos();
};
const toggleUploadsVideos = () => {
uploadsHidden = !uploadsHidden;
GM.setValue('uploadsHidden', uploadsHidden);
processUploadsVideos();
};
const toggleWatchedVideos = () => {
watchedHidden = !watchedHidden;
GM.setValue('watchedHidden', watchedHidden);
processWatchedVideos();
};
const toggleButtons = (newValue) => {
buttonsHidden = typeof newValue == 'boolean' ? newValue : !buttonsHidden;
GM.setValue('buttonsHidden', buttonsHidden);
buttonsHidden
? buttonsContainer.addClass('hide-buttons')
: buttonsContainer.removeClass('hide-buttons');
};
const processLiveVideos = () => {
if (shouldFilterByTypeButton()) {
processVideos(toggleLiveButton, liveHidden, liveVideosSelector);
}
};
const processShortsVideos = () => {
if (shouldFilterByTypeButton()) {
processVideos(toggleShortsButton, shortsHidden, shortsVideosSelector);
}
};
const processUpcomingVideos = () => {
if (shouldFilterByStatus()) {
processVideos(
toggleUpcomingButton,
upcomingHidden,
upcomingVideosSelector
);
}
};
const processUploadsVideos = () => {
if (shouldFilterByTypeButton()) {
processVideos(toggleUploadsButton, uploadsHidden, uploadsVideosSelector);
}
};
const processWatchedVideos = () => {
if (shouldFilterByStatus()) {
processVideos(toggleWatchedButton, watchedHidden, watchedVideosSelector);
}
};
const processVideos = (button, hidden, matchingSelector) => {
const matchingVideos = jQuery(matchingSelector).parents(videosSelector);
matchingVideos.toggleClass('mt-hidden', hidden);
setButtonState(button, hidden);
};
const setButtonState = (button, hidden) => {
button.toggleClass('on', !hidden);
};
const debug = enableDebug
? (message) => console.debug(`${scriptPrefix} ${message}`)
: () => {};
const initialize = () => {
jQuery('head').append(baseStyle);
toggleLiveButton = jQuery(toggleVideosButtonTemplate)
.addClass(`${i18n.live} type`)
.text(i18n.live);
toggleShortsButton = jQuery(toggleVideosButtonTemplate)
.addClass(`${i18n.shorts} type`)
.text(i18n.shorts);
toggleUpcomingButton = jQuery(toggleVideosButtonTemplate)
.addClass(`${i18n.upcoming} status`)
.text(i18n.upcoming);
toggleUploadsButton = jQuery(toggleVideosButtonTemplate)
.addClass(`${i18n.uploads} type`)
.text(i18n.uploads);
toggleWatchedButton = jQuery(toggleVideosButtonTemplate)
.addClass(`${i18n.watched} status`)
.text(i18n.watched);
buttonsContainer = jQuery(buttonsContainerTemplate);
buttonsContainer.append(toggleUpcomingButton);
buttonsContainer.append(toggleLiveButton);
buttonsContainer.append(toggleUploadsButton);
buttonsContainer.append(toggleShortsButton);
buttonsContainer.append(toggleWatchedButton);
toggleButtonsButton = jQuery(toggleButtonsButtonTemplate);
toggleButtons(buttonsHidden);
setInterval(runButtonTask, 150);
setInterval(runVideosTask, 1000);
console.info(`${scriptPrefix} Script initialized.`);
};
const scriptPrefix = `[Toggle videos buttons]`;
const urlPattern =
/youtube.com(\/?$|\/((channel\/|c\/|@)(\w*)(\/(featured|videos|shorts|streams)|\/?$)|feed\/subscriptions|results|playlist))/;
const urlWithTypesPattern =
/youtube.com(\/?$|\/((channel\/|c\/|@)(\w*)(\/(featured)|\/?$)|feed\/subscriptions|results|playlist))/;
// texts
const i18n = {
toggleButtons: 'Toggle video filter buttons',
live: 'live',
shorts: 'shorts',
upcoming: 'upcoming',
uploads: 'videos',
watched: 'watched',
};
// selectors
const liveVideosSelector = `
[role="main"] .badge-style-type-live-now-alternate
`;
const shortsVideosSelector = `
[role="main"] ytd-thumbnail-overlay-time-status-renderer[overlay-style="SHORTS"],
[role="main"] .ytd-thumbnail[href^="/shorts/"]
`;
const upcomingVideosSelector = `
[role="main"] ytd-thumbnail-overlay-time-status-renderer[overlay-style="UPCOMING"]
`;
const uploadsVideosSelector = `
[role="main"] ytd-thumbnail-overlay-time-status-renderer:not([overlay-style="SHORTS"])
`;
const watchedVideosSelector = `
[role="main"] [id="progress"]
`;
const buttonDestinationContainerSelector = `
[page-subtype="channels"][role="main"] #primary > ytd-section-list-renderer,
[page-subtype="channels"][role="main"] ytd-rich-grid-renderer,
[page-subtype="home"][role="main"] #primary > ytd-rich-grid-renderer,
[page-subtype="playlist"][role="main"] ytd-item-section-renderer,
[page-subtype="subscriptions"][role="main"] ytd-shelf-renderer,
ytd-search[role="main"] ytd-section-list-renderer
`;
const buttonsToggleDestinationSelector = `#masthead #end`;
const videosSelector = `
[role="main"] ytd-grid-video-renderer,
[role="main"] ytd-playlist-video-renderer,
[role="main"] ytd-rich-item-renderer,
[role="main"] ytd-video-renderer,
[role="main"] .ytd-rich-section-renderer[is-shorts],
[role="main"] ytd-reel-shelf-renderer,
[role="main"] ytd-reel-item-renderer
`;
const unprocessedVideosSelectors = videosSelector
.replace(/\n\s*/g, '')
.split(',')
.map(
(selector) =>
`${selector}:not(.mt-hidden) ${watchedVideosSelector}, ${selector}:not(.mt-hidden) ${upcomingVideosSelector}`
)
.join(',');
const processedVideosSelectors = videosSelector
.replace(/\n\s*/g, '')
.split(',')
.map(
(selector) =>
`${selector}.mt-hidden ${watchedVideosSelector}, ${selector}.mt-hidden ${upcomingVideosSelector}`
)
.join(',');
// templates
const toggleVideosButtonTemplate = `
<tp-yt-paper-button class="ytd-subscribe-button-renderer mt-button mt-toggle-videos-button" />
`;
const toggleButtonsButtonTemplate = `
<tp-yt-paper-button class="mt-button mt-toggle-buttons-button">
<svg viewBox="0 0 24 24">
<g>
<path fill="#FFF" d="M20,7H4V6h16V7z M22,9v12H2V9H22z M15,15l-5-3v6L15,15z M17,3H7v1h10V3z"></path>
</g>
</svg>
<tp-yt-paper-tooltip class="ytd-topbar-menu-button-renderer">
${i18n.toggleButtons}
</tp-yt-paper-tooltip>
</tp-yt-paper-button>
`;
const buttonsContainerTemplate = `
<div class="mt-toggle-videos-container"></div>
`;
// style
const baseStyle = `
<style>
.mt-toggle-videos-container {
display: flex;
justify-content: center;
margin: 0 auto;
}
.mt-toggle-videos-container.hide-buttons {
display: none;
}
.mt-button {
border-radius: 20px !important;
}
.mt-toggle-videos-button {
border-radius: 0 !important;
margin: 0 !important;
text-align: center;
min-width: 112px;
background: var(--yt-spec-additive-background) !important;
}
.mt-toggle-videos-button.on {
background: var(--yt-spec-10-percent-layer) !important;
}
.mt-toggle-videos-button:first-child {
border-radius: 20px 0 0 20px !important;
}
.mt-toggle-videos-button:last-child {
border-radius: 0 20px 20px 0 !important;
}
.mt-toggle-buttons-button {
background: transparent !important;
height: 40px;
margin: 0 8px 0 0;
min-width: 40px;
padding: 0 !important;
}
.mt-toggle-buttons-button:hover {
background: var(--yt-spec-10-percent-layer) !important;
}
.mt-toggle-buttons-button svg {
width: 24px;
}
.mt-hidden {
display: none !important;
}
[page-subtype="channels"] .mt-toggle-videos-container {
margin-top: 24px;
}
[page-subtype="channels"] ytd-rich-grid-renderer .mt-button.type,
ytd-rich-grid-renderer[is-shorts-grid] .mt-button {
background: transparent !important;
opacity: .1;
pointer-events: none;
}
[page-subtype="playlist"] .mt-toggle-videos-container {
box-sizing: border-box;
padding: 0 24px;
}
.ytd-search ytd-section-list-renderer .mt-toggle-videos-container {
margin: 12px 0;
}
</style>
`;
initialize();
})();