// ==UserScript== // @name Block Youtube Users // @namespace https://github.com/Schegge // @description Hide videos of blacklisted users/channels and comments // @icon https://raw.githubusercontent.com/Schegge/Userscripts/master/images/BYUicon.png // @version 2.5.3.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.getValue // @grant GM.setValue // @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js // ==/UserScript== // gm4 polyfill https://github.com/greasemonkey/gm4-polyfill if (typeof GM == 'undefined') { this.GM = {}; Object.entries({ 'GM_getValue': 'getValue', 'GM_setValue': 'setValue' }).forEach(([oldKey, newKey]) => { let old = this[oldKey]; if (old && (typeof GM[newKey] == 'undefined')) { GM[newKey] = function(...args) { return new Promise((resolve, reject) => { try { resolve(old.apply(this, args)); } catch (e) { reject(e); } }); }; } }); } (async function($) { /* VALUES */ const DEBUGGING = false; const Values = { storageVer: '1', storageSep: ',', storageTimer: 1000, storageComment: '', storageVideo: '', storageAdd: '', 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.storageBlacklist = getArray(await GM.getValue('savedblocks', '')); Values.storageWhitelist = getArray(await GM.getValue('savedwhites', '')); if (DEBUGGING) { console.log('BYU- current blacklist:', Values.storageBlacklist); console.log('BYU- current whitelist:', Values.storageWhitelist); } // 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 = { // home, related and page playlist: #metadata #text.ytd-channel-name // 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 // user video: #meta #upload-info #channel-name #text.ytd-channel-name, #owner #upload-info #channel-name #text.ytd-channel-name // comment: #author-text span.ytd-comment-renderer, #name #text.ytd-channel-name user: `#metadata #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, #meta #upload-info #channel-name #text.ytd-channel-name ${Values.storageComment ? ', #author-text span.ytd-comment-renderer, #name #text.ytd-channel-name' : ''}`, 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, 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 ${Values.storageComment ? ', ytd-comment-renderer.ytd-comment-replies-renderer, ytd-comment-thread-renderer' : ''}`, userVideo: '#meta #upload-info #channel-name #text.ytd-channel-name' }; /* INTERVAL FOR BLACKLISTING */ search(); setInterval(search, Values.storageTimer); /* CSS */ $('head').append(``); /* VIDEO FIRST PAGE */ if (Values.storageVideo && /\/watch/.test(window.location.href)) { let waitUserVideo = setInterval(() => { if ($(Where.userVideo).length) { clearInterval(waitUserVideo); let username = $(Where.userVideo).text().trim(); if (ifMatch(username.toLowerCase())) { let video = $('#player video.video-stream.html5-main-video'); video.get(0).pause(); video.get(0).currentTime = 0; let pausing = setInterval(() => { if (!video.get(0).paused) { video.get(0).pause(); video.get(0).currentTime = 0; } }, 500); $('body').append($(`
${username} is blacklisted
`)); $('body').on('click', '.html5-main-video, .html5-video-player, .ytp-play-button, #secondary', () => clearInterval(pausing)); setTimeout(() => { $('#byu-video-page-black').remove(); clearInterval(pausing); }, 10000); } } }, 1000); } /* BLACKLIST MENU */ $('body').append(``); // for the B wait till the masthead buttons are added let buttonB = $('
B
'); let waitButton = setInterval(() => { if ($('#buttons').length) { clearInterval(waitButton); $('#buttons').before(buttonB); $('head').append(``); } }, 1000); /* NEW VERSION NOTIFICATION */ if (Values.storageVer !== '2.5.3.1') { Values.storageVer = '2.5.3.1'; GM.setValue('byuver', Values.storageVer); /* $('body').append(`
BLOCK YOUTUBE USERS [${Values.storageVer}]

--

close
`); $('#byu-notice-close').on('click', () => $('#byu-notice').remove()); */ } /* BLACKLISTING FUNCTIONS */ // global search function search(newAdd = false) { $(Where.user).each(function() { findMatch($(this), newAdd); }); } // do the thing function findMatch(user, newAdd) { // retrieve current username let username = user.text().trim().toLowerCase(); if (!username) return; // add [x] when menu is open or always add selected if ((Values.menuOpen || Values.storageAdd) && !user.siblings('.byu-add').length) { $('
[x]
').insertBefore(user); } // if blacklist is paused do nothing if (Values.menuPause) return; // if content or blacklist are changed if (user.data('username') !== username || newAdd) { user.data('username', username); // hide if match if (ifMatch(username)) { user.closest(Where.renderer).attr('id', 'byu-is-black'); if (DEBUGGING) { console.log('BYU- MATCHED USER', user, user.closest(Where.renderer)); } user.data('black', 'yes'); // show if it was hidden with another username or deleted username from blacklist } else if (user.data('black')) { user.closest(Where.renderer).removeAttr('id'); user.data('black', ''); } } } // 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; } }) ); } /* EVENT LISTENERS */ // open/close options $(buttonB).on('click', openMenu); $(document).bind('keydown', function(e) { if (e.ctrlKey && e.altKey && e.key == 'b') { openMenu(); } }); function openMenu() { $('#byu-options').slideToggle(); $(buttonB).css('font-weight', $(buttonB).css('font-weight') === '500' ? '' : '500'); Values.menuOpen = !Values.menuOpen; if (Values.storageAdd) return; if (Values.menuOpen) { search(); } else { $('.byu-add').remove(); } } // save changes $('#byu-save').on('click', function() { if (/[*"]|^$/.test($('#byu-sep').val())) { $(this).text('ERROR! separator'); } else { Values.storageSep = $('#byu-sep').val(); Values.storageTimer = Math.max(parseInt($('#byu-timer').val(), 10), 500) || 1000; Values.storageComment = $('#byu-hidecomments').is(':checked') ? 'yes' : ''; Values.storageVideo = $('#byu-enablepause').is(':checked') ? 'yes' : ''; Values.storageAdd = $('#byu-enableadd').is(':checked') ? 'yes' : ''; Values.storageBlacklist = getArray($('#byu-blacklist').val().trim()); Values.storageWhitelist = getArray($('#byu-whitelist').val().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('savedblocks', Values.storageBlacklist.join(`${Values.storageSep} `)); GM.setValue('savedwhites', Values.storageWhitelist.join(`${Values.storageSep} `)); $(this).text('saved'); search(true); } setTimeout(() => $(this).text('save'), 2000); }); // pause $('#byu-pause').on('click', function() { Values.menuPause = !Values.menuPause; if (Values.menuPause) { $('[id="byu-is-black"]').removeAttr('id'); $(this).text('paused'); } else { search(true); $(this).text('pause'); } }); // add usernames to blacklist $(document).on('click contextmenu', '.byu-add', function(e) { e.preventDefault(); e.stopPropagation(); let username = $(this).next().text().trim().toLowerCase(); if (DEBUGGING) { console.log('BYU- YOU HAVE RIGHT-CLICKED ON [X]'); console.log('BYU- current # blacklist:', Values.storageBlacklist.length); console.log('BYU- element:', $(this)); console.log('BYU- username:', username); console.log('BYU- username already in the blacklist?', Values.storageBlacklist.includes(username)); } if (!Values.storageBlacklist.includes(username)) { Values.storageBlacklist.push(username); let blacks = Values.storageBlacklist.join(`${Values.storageSep} `); $('#byu-blacklist').val(blacks); GM.setValue('savedblocks', blacks); search(true); } }); })(jQuery);