Block Youtube Users

Hide videos of blacklisted users/channels and comments

< Feedback on Block Youtube Users

Review: قالتىس - قوليازما ئىشلەيدۇ

§
يوللانغان ۋاقتى: 2025-08-23

Hi, I made a minor improvement, please add it to the original you distribute, thanks.


// ==UserScript==
// @name Block Youtube Users
// @namespace https://codeberg.org/schegge
// @description Hide videos of blacklisted users/channels and comments
// @version 2.6.2.1
// @author Schegge
// @match https://www.youtube.com/*
// @exclude *://*.youtube.com/embed/*
// @exclude *://*.youtube.com/live_chat*
// @run-at document-end
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.notification
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @icon https://cdn-icons-png.flaticon.com/512/1384/1384060.png

// ==/UserScript==

// source of update https://greasyforks.org/en/scripts/11057-block-youtube-users

// fix trusted-types
if (typeof window != "undefined" && ('trustedTypes' in window) && ('createPolicy' in window.trustedTypes) &&
(typeof window.trustedTypes.createPolicy == "function") && !window.trustedTypes.defaultPolicy) {
window.trustedTypes.createPolicy('default', {createScriptURL: s => s, createScript: s => s, createHTML: s => s});
}

(async function() {

/* VALUES */

const Values = {
storageVer: '1',
storageSep: ',',
storageTimer: 1000,
storageComment: '',
storageVideo: '',
storageAdd: '',
storageHideShorts: '',
storageVideoOwner: '',
storageBlacklist: [],
storageWhitelist: [],
menuOpen: false,
menuPause: false
};

// get saved values
Values.storageVer = await GM.getValue('byuver', '1');
Values.storageSep = await GM.getValue('sep', ',');
Values.storageTimer = await GM.getValue('timer', 1000);
Values.storageComment = await GM.getValue('hidecomments', '');
Values.storageVideo = await GM.getValue('enablepause', '');
Values.storageAdd = await GM.getValue('enableadd', '');
Values.storageHideShorts = await GM.getValue('hideshorts', '');
Values.storageVideoOwner = await GM.getValue('videoowner', '');
Values.storageBlacklist = getArray(await GM.getValue('savedblocks', ''));
Values.storageWhitelist = getArray(await GM.getValue('savedwhites', ''));

// get array from string
function getArray(string) {
if (!string) return [];
return string.split(Values.storageSep).map(v => v.trim()).filter(v => v.length);
}

const Where = {
renderer: `ytd-rich-item-renderer,
ytd-video-renderer,
ytd-channel-renderer,
ytd-playlist-renderer,
ytd-playlist-video-renderer,
ytd-playlist-panel-video-renderer,
ytd-movie-renderer,
.ytp-videowall-still.ytp-suggestion-set.ytp-videowall-still-round-medium,
ytd-compact-video-renderer,
ytd-compact-movie-renderer,
ytd-compact-radio-renderer,
ytd-compact-autoplay-renderer,
ytd-compact-playlist-renderer,
ytd-grid-video-renderer,
ytd-grid-playlist-renderer,
ytd-secondary-search-container-renderer,
yt-lockup-view-model
${Values.storageVideoOwner ? ', ytd-video-owner-renderer' : ''}
${Values.storageComment ? ', ytd-comment-view-model.ytd-comment-replies-renderer, ytd-comment-view-model.ytd-comment-thread-renderer' : ''}`,

// home, related and page playlist: #metadata > :not([hidden]) #text.ytd-channel-name
// ^ not hidden because of search video
// search video: #channel-info #text.ytd-channel-name
// search channel: #channel-title.ytd-channel-renderer span.ytd-channel-renderer, #info #text.ytd-channel-name, #metadata #subscribers.ytd-channel-renderer
// video playlist: #byline.ytd-playlist-panel-video-renderer
// playlists: .yt-content-metadata-view-model-wiz__metadata-row span.yt-core-attributed-string a[href^="/@"], .yt-content-metadata-view-model-wiz__metadata-row span.yt-core-attributed-string a[href^="/channel/"]
// user video: #owner #upload-info #channel-name #text.ytd-channel-name
// comment: #author-text span.ytd-comment-view-model, #name #text.ytd-channel-name
user: `#metadata > :not([hidden]) #text.ytd-channel-name,
#channel-info #text.ytd-channel-name,
#channel-title.ytd-channel-renderer span.ytd-channel-renderer,
#info #text.ytd-channel-name,
#metadata #subscribers.ytd-channel-renderer,
#byline.ytd-playlist-panel-video-renderer,
yt-simple-endpoint[href^="/@"],
yt-formatted-string#text[title],
.yt-content-metadata-view-model-wiz__metadata-row span.yt-core-attributed-string a[href^="/@"],
.yt-content-metadata-view-model-wiz__metadata-row span.yt-core-attributed-string a[href^="/channel/"],
.yt-content-metadata-view-model-wiz__metadata-text:last-of-type
${Values.storageVideoOwner ? ', #owner #upload-info #channel-name #text.ytd-channel-name' : ''}
${Values.storageComment ? ', #author-text span.ytd-comment-view-model, #name #text.ytd-channel-name' : ''}`,


// if the above aren't found
userFailSafe: 'a[href^="/@"], a[href^="/channel/"], .ytd-channel-name a, .yt-content-metadata-view-model-wiz__metadata-row:first-child span.yt-core-attributed-string',

shorts: 'a[href^="/shorts/"]',

videoPage: {
parentId: 'owner',
channel: '#owner #upload-info #channel-name #text.ytd-channel-name',
video: '#player video.video-stream.html5-main-video'
},

masthead: {
parent: '#container.ytd-masthead',
buttonsId: 'buttons'
}
};

/* INTERVAL FOR BLACKLISTING */

search();
setInterval(search, Values.storageTimer);

if (Values.storageHideShorts) hideShortsCSS();

/* CSS */

document.head.insertAdjacentHTML('beforeend', `

`);

/* VIDEO FIRST PAGE */

if (Values.storageVideo && /\/watch/.test(window.location.href)) {
let waitUserVideo = setInterval(() => {
if (document.querySelector(Where.videoPage.channel)) {
clearInterval(waitUserVideo);

let username = document.querySelector(Where.videoPage.channel).textContent.trim();
if (ifMatch(username.toLowerCase())) {
let video = document.querySelector(Where.videoPage.video);
video.pause();
video.currentTime = 0;

let divBlack = document.createElement('div');
divBlack.id = 'byu-video-page-black';
divBlack.title = username;
divBlack.textContent = 'BLACKLISTED';
document.getElementById(Where.videoPage.parentId).insertAdjacentElement('afterbegin', divBlack);
}
}
}, 1000);
}

/* BLACKLIST MENU */

document.body.insertAdjacentHTML('beforeend', `

SAVE
pause
blacklist
${Values.storageBlacklist.join(`${Values.storageSep} `)}
whitelist
${Values.storageWhitelist.join(`${Values.storageSep} `)}

separator

timer

show buttons

video owner

pause video

comments

hide all shorts

Users in list: ${Values.storageBlacklist.length}

Currently hidden: 0

`);


// for the B wait till the masthead buttons are added
const buttonB = document.createElement('div');
buttonB.id = 'byu-icon';
buttonB.innerHTML = 'B';

let waitButton = setInterval(() => {
if (document.getElementById(Where.masthead.buttonsId)) {
clearInterval(waitButton);
document.getElementById(Where.masthead.buttonsId).insertAdjacentElement('beforebegin', buttonB);
document.head.insertAdjacentHTML('beforeend', `

`);
}
}, 1000);

// open / close menu
buttonB.addEventListener('click', openMenu);
document.addEventListener('keydown', function(e) {
if (e.ctrlKey && e.altKey && e.key == 'b') {
openMenu();
}
});

function openMenu() {
let byuOpts = document.getElementById('byu-options');
byuOpts.style = byuOpts.style.display === 'none' ? '' : 'display: none;';
buttonB.style.fontWeight = buttonB.style.fontWeight === '500' ? '' : '500';

Values.menuOpen = !Values.menuOpen;
if (Values.storageAdd) return;

if (Values.menuOpen) {
search();
} else {
document.querySelectorAll('.byu-add').forEach(el => el.parentElement.removeChild(el));
}
}

// save changes
document.getElementById('byu-save').addEventListener('click', function() {
if (/[*"]|^$/.test(document.getElementById('byu-sep').value)) {
this.text('ERROR! separator');
} else {
Values.storageSep = document.getElementById('byu-sep').value;
Values.storageTimer = Math.max(parseInt(document.getElementById('byu-timer').value, 10), 500) || 1000;
Values.storageComment = document.getElementById('byu-hidecomments').checked ? 'yes' : '';
Values.storageVideo = document.getElementById('byu-enablepause').checked ? 'yes' : '';
Values.storageAdd = document.getElementById('byu-enableadd').checked ? 'yes' : '';
Values.storageHideShorts = document.getElementById('byu-hideshorts').checked ? 'yes' : '';
Values.storageVideoOwner = document.getElementById('byu-videoowner').checked ? 'yes' : '';
Values.storageBlacklist = getArray(document.getElementById('byu-blacklist').value.trim());
Values.storageWhitelist = getArray(document.getElementById('byu-whitelist').value.trim());
GM.setValue('sep', Values.storageSep);
GM.setValue('timer', Values.storageTimer);
GM.setValue('hidecomments', Values.storageComment);
GM.setValue('enablepause', Values.storageVideo);
GM.setValue('enableadd', Values.storageAdd);
GM.setValue('hideshorts', Values.storageHideShorts);
GM.setValue('videoowner', Values.storageVideoOwner);
GM.setValue('savedblocks', Values.storageBlacklist.join(`${Values.storageSep} `));
GM.setValue('savedwhites', Values.storageWhitelist.join(`${Values.storageSep} `));
this.textContent = 'SAVED';
search(true);
if (Values.storageHideShorts) {
hideShortsCSS();
} else if (document.getElementById('byu-hideshorts-css')) {
document.getElementById('byu-hideshorts-css').textContent = '';
}
}
setTimeout(() => this.textContent = 'SAVE', 2000);
});

// pause
document.getElementById('byu-pause').addEventListener('click', function() {
Values.menuPause = !Values.menuPause;
if (Values.menuPause) {
document.querySelectorAll('[data-block="yes"]').forEach(el => el.dataset.block = '');
document.getElementById('byu-hideshorts-css').textContent = '';
this.textContent = 'paused';
} else {
search(true);
hideShortsCSS();
this.textContent = 'pause';
}
});

/* BLACKLISTING FUNCTIONS */

// global search
function search(blacklistChanged = false) {
for (let el of document.querySelectorAll(Where.renderer)) {
// check if blacklisted
findMatch(el, blacklistChanged);
// Update current hidden count
document.getElementById('hidden-count-value').textContent = document.querySelectorAll('[data-block="yes"]').length;
}
}

// check if blacklisted, get username, add "x" buttons
function findMatch(el, blacklistChanged) {
let addButton = true;

// retrieve current username
let userEl = el.querySelector(Where.user);
let username = userEl?.textContent?.trim();
if (!username) {
// to try to not make the script completely break if yt changes
// search with broader classes but don't add "x" buttons
username = el.querySelector(Where.userFailSafe)?.textContent?.trim();
if (!username) return;
addButton = false;
}
username = username.toLowerCase();

// add "x" when menu is open or always add selected
if (addButton &&
(Values.menuOpen || Values.storageAdd) &&
!el.querySelector('.byu-add')) {

let addBtn = document.createElement('div');
addBtn.className = 'byu-add';
addBtn.textContent = 'x';
addBtn.addEventListener('click', addToBlacklist);
userEl.insertAdjacentElement('beforebegin', addBtn);
}

// if blacklist is paused, stop
if (Values.menuPause) return;

// if blacklist or content are changed
if (blacklistChanged || el.dataset.username !== username) {
el.dataset.username = username;

// hide if match
if (ifMatch(username)) {
el.dataset.block = 'yes';
// show if it was hidden with another username or deleted username from blacklist
} else if (el.dataset.block) {
el.dataset.block = '';
}
}
}

// check if it needs to be blacklisted
function ifMatch(u) {
return (
!Values.storageWhitelist.some(w => u === w.toLowerCase()) &&
Values.storageBlacklist.some(b => {
b = b.toLowerCase();
if (b.startsWith('*')) {
b = b.replace('*', '');
return b && u.includes(b);
} else {
return u === b;
}
})
);
}

// add usernames to blacklist
function addToBlacklist(e) {
e.preventDefault();
e.stopPropagation();

let username = this.nextElementSibling.textContent.trim().toLowerCase();

if (!Values.storageBlacklist.includes(username)) {
Values.storageBlacklist.push(username);
let blacks = Values.storageBlacklist.join(`${Values.storageSep} `);
document.getElementById('byu-blacklist').value = blacks;
GM.setValue('savedblocks', blacks);
search(true);

// Aktualizace počtu blokovaných uživatelů
document.getElementById('blocked-count-value').textContent = Values.storageBlacklist.length;
}
}

// css to hide shorts
function hideShortsCSS() {
let css = `ytd-reel-shelf-renderer,
ytd-rich-section-renderer:has(ytd-rich-shelf-renderer[is-shorts]),
ytd-rich-item-renderer:has(a[href^="/shorts/"]),
ytd-video-renderer:has(a.yt-simple-endpoint[href*="shorts"]),
a[title="Shorts"] {
display: none !important;
}`;

if (!document.getElementById('byu-hideshorts-css')) {
document.head.insertAdjacentHTML('beforeend', `

`);
} else {
document.getElementById('byu-hideshorts-css').textContent = css;
}
}

/* NEW VERSION NOTIFICATION */

if (Values.storageVer !== '2.6.2.1') {
Values.storageVer = '2.6.2.1';
GM.setValue('byuver', Values.storageVer);

/*GM.notification({
text: '\nFor more details, see the script description on Greasy Fork镜像.',
title: 'BLOCK YOUTUBE USERS [2.6.2]'
});*/
}

})();

جاۋاب قايتۇرۇش

جاۋاب قايتۇرۇش ئۈچۈن كىرىش.

长期地址
遇到问题?请前往 GitHub 提 Issues。