-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.user.js
313 lines (274 loc) · 9.76 KB
/
main.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
// ==UserScript==
// @name Focused YouTube
// @version 2024-05-02
// @author Kervyn
// @namespace https://raw.githubusercontent.com/KervynH/Focused-YouTube/main/main.user.js
// @description Remove ads, shorts, and algorithmic suggestions on YouTube
// @match *://*.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @run-at document-start
// @grant GM.addStyle
// ==/UserScript==
/* Credit: https://github.com/lawrencehook/remove-youtube-suggestions */
'use strict';
// Config custom settings here
const SETTINGS = {
/// homepage redirect ///
// redirectHomepage Options: 'wl', 'subs', 'lib', false
redirectHomepage: 'wl',
hideHomepageButton: true,
/// homepage suggestions ///
hideAllSuggestions: false,
hideAllButOneRow: true,
hideInfiniteScroll: true,
/// video player ///
skipAds: true,
hideLiveChat: true,
hideRelatedVideos: true,
hidePlayNextButton: true,
hidePlayPreviousButton: true,
hideMiniPlayerButton: true,
disablePlaylistAutoPlay: true,
/// shorts ///
hideShorts: true,
redirectShortsPlayer: true,
/// misc ///
hideSearchButton: false,
};
// Mark settings in HTML
const HTML = document.documentElement;
Object.keys(SETTINGS).forEach(key => {
HTML.setAttribute(key, SETTINGS[key]);
});
// Add css to remove unnecessary elements
const DESKTOP_BLOCK_LIST = [
// Ads
'#masthead-ad',
'ytd-mealbar-promo-renderer',
'ytd-carousel-ad-renderer',
'.ytd-display-ad-renderer',
'ytd-ad-slot-renderer',
'div.ytp-ad-overlay-image',
'.iv-branding.annotation-type-custom.annotation',
// Shorts
'html[hideShorts="true"] ytd-rich-section-renderer',
'html[hideShorts="true"] a[title="Shorts"]',
'html[hideShorts="true"] ytd-reel-shelf-renderer',
// Left Bar Navigation
'a[href="/feed/trending"]',
'a[href="/feed/explore"]',
'html[hideShorts="true"] a[title="Shorts"]',
'html[hideShorts="true"] ytd-mini-guide-entry-renderer[aria-label="Shorts"]',
'ytd-guide-section-renderer.ytd-guide-renderer.style-scope:nth-of-type(4)',
'ytd-guide-section-renderer.ytd-guide-renderer.style-scope:nth-of-type(3)',
// Homepage
'html[hideHomepageButton="true"] a:not(#logo)[href="/"]',
'html[hideAllSuggestions="true"] ytd-browse[page-subtype="home"]',
'html[hideAllButOneRow="true"] ytd-browse[page-subtype="home"] ytd-rich-grid-renderer>#contents>ytd-rich-grid-row:nth-child(n+2)',
'html[hideAllButOneRow="true"] ytd-browse[page-subtype="home"] #header',
'html[hideInfiniteScroll="true"] ytd-browse[page-subtype="home"] ytd-rich-grid-renderer>#contents>ytd-continuation-item-renderer',
// Video Player
'html[hideRelatedVideos="true"] #secondary>div.circle',
'html[hideRelatedVideos="true"] #related',
'html[hideRelatedVideos="true"] .html5-endscreen',
'html[hidePlayNextButton="true"] a.ytp-next-button.ytp-button',
'html[hidePlayPreviousButton="true"] a.ytp-prev-button.ytp-button',
'html[hideChat="true"] #chat',
'html[hideMiniPlayerButton="true"] .ytp-button.ytp-miniplayer-button',
// '#movie_player button.ytp-button.ytp-share-button',
// '#movie_player button.ytp-button.ytp-watch-later-button',
'.ytd-download-button-renderer.style-scope',
// Search
'div.sbdd_a',
'#container.ytd-search ytd-search-pyv-renderer',
'#container.ytd-search ytd-reel-shelf-renderer',
'#container.ytd-search ytd-shelf-renderer',
'html[hideSearchButton="true"] div.ytd-masthead>ytd-searchbox',
'html[hideSearchButton="true"] div.ytd-masthead>#voice-search-button',
];
const MOBILE_BLOCK_LIST = [
// Ads
'ytm-companion-ad-renderer',
'ytm-promoted-sparkles-web-renderer',
// Homepage
'html[hideHomepageButton="true"] div[tab-identifier="FEwhat_to_watch"]',
'html[hideSearchButton="true"] #header-bar > header > div > button',
'html[hideSearchButton="true"] #center.style-scope.ytd-masthead',
// Video Player
'html[hideRelatedVideos="true"] ytm-item-section-renderer[section-identifier="related-items"]>lazy-list',
'html[hidePlayPreviousButton="true"] .player-controls-middle-core-buttons>button:nth-child(1)',
'html[hidePlayNextButton="true"] .player-controls-middle-core-buttons>button:nth-child(5)',
// Navigation Bar
'html[hideHomepageButton="true"] ytm-pivot-bar-item-renderer:nth-child(1)',
'html[hideShorts="true"] ytm-pivot-bar-item-renderer:nth-child(2)',
'ytm-chip-cloud-chip-renderer[chip-style="STYLE_EXPLORE_LAUNCHER_CHIP"]',
];
if (location.hostname.startsWith('www.')) {
DESKTOP_BLOCK_LIST.forEach(e => GM.addStyle(`${e} {display: none !important}`));
}
if (location.hostname.startsWith('m.')) {
MOBILE_BLOCK_LIST.forEach(e => GM.addStyle(`${e} {display: none !important}`));
}
// Global variables for running dynamic settings
const TIMEOUT = 1000;
let path = undefined;
let isRunning = false;
let frameRequested = false;
handleNewPage();
/***** Main Functions *****/
function handleNewPage() {
// check whether url has changed
if (path == location.pathname) return;
path = location.pathname;
// Static settings run only once
runStaticSettings();
// Dyamic settings run periodically
requestRunDynamicSettings();
}
function runStaticSettings() {
if (SETTINGS.redirectHomepage) redirectHomepage();
if (SETTINGS.redirectShortsPlayer) redirectShortsPlayer();
}
function runDynamicSettings() {
if (isRunning) return;
isRunning = true;
handleNewPage();
cleanSearchResults();
if (SETTINGS.hideShorts) hideShortsVideos();
if (SETTINGS.skipAds) skipVideoAds();
if (SETTINGS.hideRelatedVideos) disableRelatedAutoPlay();
if (SETTINGS.disablePlaylistAutoPlay) disablePlaylistAutoPlay();
frameRequested = false;
isRunning = false;
requestRunDynamicSettings();
}
function requestRunDynamicSettings() {
if (isRunning || frameRequested) return;
frameRequested = true;
setTimeout(runDynamicSettings, TIMEOUT);
}
/***** Features *****/
function redirectHomepage() {
if (path == '/') {
if (SETTINGS.redirectHomepage == 'wl') {
location.replace('/playlist/?list=WL');
}
if (SETTINGS.redirectHomepage == 'subs') {
location.replace('/feed/subscriptions');
}
if (SETTINGS.redirectHomepage == 'lib') {
location.replace('/feed/library');
}
}
}
function redirectShortsPlayer() {
if (path.startsWith('/shorts')) {
const redirPath = path.replace('shorts', 'watch');
location.replace(redirPath);
}
}
function disableRelatedAutoPlay() {
// turn off auto play button
const autoplayButton = document.querySelectorAll('.ytp-autonav-toggle-button[aria-checked=true]');
autoplayButton?.forEach(e => {
if (e && e.offsetParent) {
e.click();
}
});
// turn off auto play button on mobile
const mAutoplayButton = document.querySelectorAll('.ytm-autonav-toggle-button-container[aria-pressed=true]');
mAutoplayButton?.forEach(e => {
if (e && e.offsetParent) {
e.click();
}
});
}
function disablePlaylistAutoPlay() {
if (path.startsWith('/watch')) {
var script = document.createElement("script");
script.id = "npafy-script";
script.type = "text/javascript";
script.innerText = [
"(function() {",
" var ypm;",
" function noAutoAdvance() {",
" if (!ypm) {",
" ypm = document.getElementsByTagName('yt-playlist-manager')[0];",
" }",
" if (ypm) {",
" ypm.canAutoAdvance_ = false;",
" }",
" }",
" noAutoAdvance();",
" setInterval(noAutoAdvance, 100);",
"})();"
].join("\n");
document.body.appendChild(script);
}
}
function hideShortsVideos() {
const shortsLinks = document.querySelectorAll('a[href^="/shorts"]');
if (path == '/feed/subscriptions') {
shortsLinks.forEach(link => {
// For desktop
link.closest('ytd-rich-item-renderer')?.remove();
// For mobile
link.closest('ytm-item-section-renderer')?.remove();
});
}
// Hide shorts on the results page
if (path.startsWith('/results')) {
shortsLinks.forEach(link => {
// For desktop
link.closest('ytd-video-renderer')?.remove();
link.closest('ytd-reel-shelf-renderer')?.remove();
// For mobile
link.closest('ytm-reel-shelf-renderer')?.remove();
link.closest('ytm-media-item')?.remove();
});
}
// Hide shorts on channel page
if (path.startsWith('/@')) {
// remove shorts tab
document.querySelectorAll('div.tab-content')?.forEach(tab => {
if (tab.innerText == "SHORTS") tab.parentElement.remove(); // desktop
});
document.querySelectorAll('a[role="tab"]')?.forEach(tab => {
if (tab.innerText == 'SHORTS') tab.remove(); // mobile
});
// remove shorts shelf
document.querySelectorAll('a[href^="/shorts"]')?.forEach(link => {
link.closest('ytm-reel-shelf-renderer.item')?.remove(); // mobile
link.closest('ytd-reel-shelf-renderer')?.remove(); // desktop
});
}
}
function skipVideoAds() {
if (path.startsWith('/watch')) {
// click "skip ad" button if it exists
// during the first 5s, th button is not clickable in UI, but it's clickable in console
const adSkipButton = document.querySelector(".ytp-ad-skip-button-slot button,.ytp-ad-overlay-close-button");
adSkipButton?.click();
// skip ad video
const adVideo = document.querySelector('.ad-showing');
if (adVideo) {
const video = document.querySelector('.html5-main-video');
if (video && !isNaN(video?.duration)) {
video.play();
video.currentTime = video?.duration;
}
}
}
}
function cleanSearchResults() {
if (path.startsWith('/results')) {
// Mobile
const badges = document.querySelectorAll('ytm-badge');
badges.forEach(badge => {
// Only support Chinese and English now
if (badge.innerText == '相關影片' || badge.innerText == '相关视频' || badge.innerText == 'Related') {
badge.closest('ytm-video-with-context-renderer')?.remove();
}
});
}
}