🏠 Home 

Block Youtube Users

Hide videos of blacklisted users/channels and comments


Install this script?
  1. // ==UserScript==
  2. // @name Block Youtube Users
  3. // @namespace https://github.com/Schegge
  4. // @description Hide videos of blacklisted users/channels and comments
  5. // @icon https://raw.githubusercontent.com/Schegge/Userscripts/master/images/BYUicon.png
  6. // @version 2.5.3.1
  7. // @author Schegge
  8. // @match https://www.youtube.com/*
  9. // @exclude *://*.youtube.com/embed/*
  10. // @exclude *://*.youtube.com/live_chat*
  11. // @run-at document-end
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM.getValue
  15. // @grant GM.setValue
  16. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
  17. // ==/UserScript==
  18. // gm4 polyfill https://github.com/greasemonkey/gm4-polyfill
  19. if (typeof GM == 'undefined') {
  20. this.GM = {};
  21. Object.entries({
  22. 'GM_getValue': 'getValue',
  23. 'GM_setValue': 'setValue'
  24. }).forEach(([oldKey, newKey]) => {
  25. let old = this[oldKey];
  26. if (old && (typeof GM[newKey] == 'undefined')) {
  27. GM[newKey] = function(...args) {
  28. return new Promise((resolve, reject) => { try { resolve(old.apply(this, args)); } catch (e) { reject(e); } });
  29. };
  30. }
  31. });
  32. }
  33. (async function($) {
  34. /* VALUES */
  35. const DEBUGGING = false;
  36. const Values = {
  37. storageVer: '1',
  38. storageSep: ',',
  39. storageTimer: 1000,
  40. storageComment: '',
  41. storageVideo: '',
  42. storageAdd: '',
  43. storageBlacklist: [],
  44. storageWhitelist: [],
  45. menuOpen: false,
  46. menuPause: false
  47. };
  48. // get saved values
  49. Values.storageVer = await GM.getValue('byuver', '1');
  50. Values.storageSep = await GM.getValue('sep', ',');
  51. Values.storageTimer = await GM.getValue('timer', 1000);
  52. Values.storageComment = await GM.getValue('hidecomments', '');
  53. Values.storageVideo = await GM.getValue('enablepause', '');
  54. Values.storageAdd = await GM.getValue('enableadd', '');
  55. Values.storageBlacklist = getArray(await GM.getValue('savedblocks', ''));
  56. Values.storageWhitelist = getArray(await GM.getValue('savedwhites', ''));
  57. if (DEBUGGING) {
  58. console.log('BYU- current blacklist:', Values.storageBlacklist);
  59. console.log('BYU- current whitelist:', Values.storageWhitelist);
  60. }
  61. // get array from string
  62. function getArray(string) {
  63. if (!string) return [];
  64. return string.split(Values.storageSep).map(v => v.trim()).filter(v => v.length);
  65. }
  66. const Where = {
  67. // home, related and page playlist: #metadata #text.ytd-channel-name
  68. // search video: #channel-info #text.ytd-channel-name
  69. // search channel: #channel-title.ytd-channel-renderer span.ytd-channel-renderer, #info #text.ytd-channel-name, #metadata #subscribers.ytd-channel-renderer
  70. // video playlist: #byline.ytd-playlist-panel-video-renderer
  71. // user video: #meta #upload-info #channel-name #text.ytd-channel-name, #owner #upload-info #channel-name #text.ytd-channel-name
  72. // comment: #author-text span.ytd-comment-renderer, #name #text.ytd-channel-name
  73. user: `#metadata #text.ytd-channel-name,
  74. #channel-info #text.ytd-channel-name,
  75. #channel-title.ytd-channel-renderer span.ytd-channel-renderer,
  76. #info #text.ytd-channel-name,
  77. #metadata #subscribers.ytd-channel-renderer,
  78. #byline.ytd-playlist-panel-video-renderer,
  79. #meta #upload-info #channel-name #text.ytd-channel-name
  80. ${Values.storageComment ? ', #author-text span.ytd-comment-renderer, #name #text.ytd-channel-name' : ''}`,
  81. renderer: `ytd-rich-item-renderer,
  82. ytd-video-renderer,
  83. ytd-channel-renderer,
  84. ytd-playlist-renderer,
  85. ytd-playlist-video-renderer,
  86. ytd-playlist-panel-video-renderer,
  87. ytd-movie-renderer,
  88. ytd-compact-video-renderer,
  89. ytd-compact-movie-renderer,
  90. ytd-compact-radio-renderer,
  91. ytd-compact-autoplay-renderer,
  92. ytd-compact-playlist-renderer,
  93. ytd-grid-video-renderer,
  94. ytd-grid-playlist-renderer,
  95. ytd-secondary-search-container-renderer
  96. ${Values.storageComment ? ', ytd-comment-renderer.ytd-comment-replies-renderer, ytd-comment-thread-renderer' : ''}`,
  97. userVideo: '#meta #upload-info #channel-name #text.ytd-channel-name'
  98. };
  99. /* INTERVAL FOR BLACKLISTING */
  100. search();
  101. setInterval(search, Values.storageTimer);
  102. /* CSS */
  103. $('head').append(`<style>
  104. #byu-is-black { display: none!important; }
  105. .byu-add { font-size: .8em; margin-right: .5em; cursor: pointer; color: var(--yt-brand-youtube-red, red); font-family: consolas, monospace; float: left; }
  106. #byu-icon { display: inline-block; position: relative; text-align: center; width: 40px; height: 24px; margin: 0 8px; font-weight: 100; }
  107. #byu-icon span { color: var(--yt-spec-icon-active-other); cursor: pointer; font-size: 20px; vertical-align: middle; }
  108. #byu-options { width: 30%; max-width: 250px; display: flex; flex-flow: row wrap; align-items: baseline; position: fixed; right: 10px; padding: 15px; text-align: center; color: var(--yt-spec-text-primary); background-color: var(--yt-spec-base-background); border: 1px solid var(--yt-spec-10-percent-layer); z-index: 99999; }
  109. #byu-options div { width: 50%; flex-grow: 1; box-sizing: border-box; padding: 5px; font-size: .9em; }
  110. #byu-save { font-size: 1.5em!important; font-weight: bold; cursor: pointer; color: var(--yt-brand-youtube-red, red); }
  111. #byu-pause { cursor: pointer; }
  112. #byu-options .byu-textarea { width: 100%; }
  113. #byu-options .byu-textarea span { font-size: 1.2em; width: 100%; text-align: center; font-weight: bold; }
  114. #byu-options .byu-textarea textarea { font-size: 1em; line-height: 1em; resize: vertical; width: 100%; padding: 4px; color: var(--yt-spec-text-primary); background-color: var(--yt-spec-badge-chip-background); box-sizing: border-box; border: 0; border-radius: 1em; }
  115. #byu-options .byu-textarea textarea#byu-blacklist { min-height: 6em; }
  116. #byu-options .byu-textarea textarea#byu-whiteklist { min-height: 4em; }
  117. #byu-options .byu-opt { text-align: right; padding-right: 2em; }
  118. #byu-options .byu-opt input { color: var(--yt-spec-text-primary); background-color: var(--yt-spec-badge-chip-background); border: 0; padding: 0 2px; height: 1.6em; line-height: 1em; vertical-align: middle; box-sizing: border-box; margin: 0; border-radius: .5em; }
  119. #byu-sep { width: 1em; }
  120. #byu-timer { width: 5em; }
  121. #byu-video-page-black { position: fixed; z-index: 99999; bottom: 2em; left: 2em; width: 20%; min-width: 10em; font-size: 1.5em; padding: 1em; background: var(--yt-brand-youtube-red, red); color: #fff; border-radius: .5em; }
  122. #byu-notice { position: fixed; z-index: 99999; bottom: 2em; right: 2em; width: 30%; min-width: 10em; font-size: 1.2em; padding: 1.5em; color: var(--yt-brand-youtube-red, red); background: #fff; border-radius: .5em; border: 1px solid var(--yt-brand-youtube-red, red); }
  123. #byu-notice-close { cursor: pointer; background: var(--yt-brand-youtube-red, red); color: #fff; border-radius: .5em; padding: 0 .5em; }
  124. </style>`);
  125. /* VIDEO FIRST PAGE */
  126. if (Values.storageVideo && /\/watch/.test(window.location.href)) {
  127. let waitUserVideo = setInterval(() => {
  128. if ($(Where.userVideo).length) {
  129. clearInterval(waitUserVideo);
  130. let username = $(Where.userVideo).text().trim();
  131. if (ifMatch(username.toLowerCase())) {
  132. let video = $('#player video.video-stream.html5-main-video');
  133. video.get(0).pause();
  134. video.get(0).currentTime = 0;
  135. let pausing = setInterval(() => {
  136. if (!video.get(0).paused) {
  137. video.get(0).pause();
  138. video.get(0).currentTime = 0;
  139. }
  140. }, 500);
  141. $('body').append($(`<div id="byu-video-page-black">${username} is blacklisted</div>`));
  142. $('body').on('click', '.html5-main-video, .html5-video-player, .ytp-play-button, #secondary',
  143. () => clearInterval(pausing));
  144. setTimeout(() => {
  145. $('#byu-video-page-black').remove();
  146. clearInterval(pausing);
  147. }, 10000);
  148. }
  149. }
  150. }, 1000);
  151. }
  152. /* BLACKLIST MENU */
  153. $('body').append(`<div id="byu-options" style="display: none;">
  154. <div><span id="byu-save">save</span></div>
  155. <div><span id="byu-pause">pause</span></div>
  156. <div class="byu-textarea"><span>blacklist</span>
  157. <textarea spellcheck="false" id="byu-blacklist">${
  158. Values.storageBlacklist.join(`${Values.storageSep} `)
  159. }</textarea></div>
  160. <div class="byu-textarea"><span>whitelist</span>
  161. <textarea spellcheck="false" id="byu-whitelist">${
  162. Values.storageWhitelist.join(`${Values.storageSep} `)
  163. }</textarea></div>
  164. <div class="byu-opt" title="between usernames">separator <input id="byu-sep" type="text" maxlength="1" value="${
  165. Values.storageSep
  166. }"></div>
  167. <div class="byu-opt" title="hide comments">comments
  168. <input id="byu-hidecomments" type="checkbox" value="comments" ${
  169. Values.storageComment ? 'checked' : ''
  170. }></div>
  171. <div class="byu-opt" title="interval between new checks">timer
  172. <input id="byu-timer" type="number" min="500" max="5000" step="500" title="in milliseconds" value="${
  173. Values.storageTimer
  174. }"></div>
  175. <div class="byu-opt" title="if user blacklisted">pause video
  176. <input id="byu-enablepause" type="checkbox" value="pausevideo" ${
  177. Values.storageVideo ? 'checked' : ''
  178. }></div>
  179. <div class="byu-opt" title="always show [x]">right click add
  180. <input id="byu-enableadd" type="checkbox" value="clickadd" ${
  181. Values.storageAdd ? 'checked' : ''
  182. }></div>
  183. </div>`);
  184. // for the B wait till the masthead buttons are added
  185. let buttonB = $('<div id="byu-icon"><span>B</span></div>');
  186. let waitButton = setInterval(() => {
  187. if ($('#buttons').length) {
  188. clearInterval(waitButton);
  189. $('#buttons').before(buttonB);
  190. $('head').append(`<style>#byu-options { top:${
  191. $('#container.ytd-masthead').height()
  192. }px; }</style>`);
  193. }
  194. }, 1000);
  195. /* NEW VERSION NOTIFICATION */
  196. if (Values.storageVer !== '2.5.3.1') {
  197. Values.storageVer = '2.5.3.1';
  198. GM.setValue('byuver', Values.storageVer);
  199. /* $('body').append(`<div id="byu-notice">BLOCK YOUTUBE USERS [${Values.storageVer}]<br><br>--
  200. <br><br><span id="byu-notice-close">close</span></div>`);
  201. $('#byu-notice-close').on('click', () => $('#byu-notice').remove()); */
  202. }
  203. /* BLACKLISTING FUNCTIONS */
  204. // global search
  205. function search(newAdd = false) {
  206. $(Where.user).each(function() {
  207. findMatch($(this), newAdd);
  208. });
  209. }
  210. // do the thing
  211. function findMatch(user, newAdd) {
  212. // retrieve current username
  213. let username = user.text().trim().toLowerCase();
  214. if (!username) return;
  215. // add [x] when menu is open or always add selected
  216. if ((Values.menuOpen || Values.storageAdd) && !user.siblings('.byu-add').length) {
  217. $('<div class="byu-add">[x]</div>').insertBefore(user);
  218. }
  219. // if blacklist is paused do nothing
  220. if (Values.menuPause) return;
  221. // if content or blacklist are changed
  222. if (user.data('username') !== username || newAdd) {
  223. user.data('username', username);
  224. // hide if match
  225. if (ifMatch(username)) {
  226. user.closest(Where.renderer).attr('id', 'byu-is-black');
  227. if (DEBUGGING) {
  228. console.log('BYU- MATCHED USER', user, user.closest(Where.renderer));
  229. }
  230. user.data('black', 'yes');
  231. // show if it was hidden with another username or deleted username from blacklist
  232. } else if (user.data('black')) {
  233. user.closest(Where.renderer).removeAttr('id');
  234. user.data('black', '');
  235. }
  236. }
  237. }
  238. // check if it needs to be blacklisted
  239. function ifMatch(u) {
  240. return (
  241. !Values.storageWhitelist.some(w => u === w.toLowerCase()) &&
  242. Values.storageBlacklist.some(b => {
  243. b = b.toLowerCase();
  244. if (b.startsWith('*')) {
  245. b = b.replace('*', '');
  246. return b && u.includes(b);
  247. } else {
  248. return u === b;
  249. }
  250. })
  251. );
  252. }
  253. /* EVENT LISTENERS */
  254. // open/close options
  255. $(buttonB).on('click', openMenu);
  256. $(document).bind('keydown', function(e) {
  257. if (e.ctrlKey && e.altKey && e.key == 'b') {
  258. openMenu();
  259. }
  260. });
  261. function openMenu() {
  262. $('#byu-options').slideToggle();
  263. $(buttonB).css('font-weight', $(buttonB).css('font-weight') === '500' ? '' : '500');
  264. Values.menuOpen = !Values.menuOpen;
  265. if (Values.storageAdd) return;
  266. if (Values.menuOpen) {
  267. search();
  268. } else {
  269. $('.byu-add').remove();
  270. }
  271. }
  272. // save changes
  273. $('#byu-save').on('click', function() {
  274. if (/[*"]|^$/.test($('#byu-sep').val())) {
  275. $(this).text('ERROR! separator');
  276. } else {
  277. Values.storageSep = $('#byu-sep').val();
  278. Values.storageTimer = Math.max(parseInt($('#byu-timer').val(), 10), 500) || 1000;
  279. Values.storageComment = $('#byu-hidecomments').is(':checked') ? 'yes' : '';
  280. Values.storageVideo = $('#byu-enablepause').is(':checked') ? 'yes' : '';
  281. Values.storageAdd = $('#byu-enableadd').is(':checked') ? 'yes' : '';
  282. Values.storageBlacklist = getArray($('#byu-blacklist').val().trim());
  283. Values.storageWhitelist = getArray($('#byu-whitelist').val().trim());
  284. GM.setValue('sep', Values.storageSep);
  285. GM.setValue('timer', Values.storageTimer);
  286. GM.setValue('hidecomments', Values.storageComment);
  287. GM.setValue('enablepause', Values.storageVideo);
  288. GM.setValue('enableadd', Values.storageAdd);
  289. GM.setValue('savedblocks', Values.storageBlacklist.join(`${Values.storageSep} `));
  290. GM.setValue('savedwhites', Values.storageWhitelist.join(`${Values.storageSep} `));
  291. $(this).text('saved');
  292. search(true);
  293. }
  294. setTimeout(() => $(this).text('save'), 2000);
  295. });
  296. // pause
  297. $('#byu-pause').on('click', function() {
  298. Values.menuPause = !Values.menuPause;
  299. if (Values.menuPause) {
  300. $('[id="byu-is-black"]').removeAttr('id');
  301. $(this).text('paused');
  302. } else {
  303. search(true);
  304. $(this).text('pause');
  305. }
  306. });
  307. // add usernames to blacklist
  308. $(document).on('click contextmenu', '.byu-add', function(e) {
  309. e.preventDefault();
  310. e.stopPropagation();
  311. let username = $(this).next().text().trim().toLowerCase();
  312. if (DEBUGGING) {
  313. console.log('BYU- YOU HAVE RIGHT-CLICKED ON [X]');
  314. console.log('BYU- current # blacklist:', Values.storageBlacklist.length);
  315. console.log('BYU- element:', $(this));
  316. console.log('BYU- username:', username);
  317. console.log('BYU- username already in the blacklist?', Values.storageBlacklist.includes(username));
  318. }
  319. if (!Values.storageBlacklist.includes(username)) {
  320. Values.storageBlacklist.push(username);
  321. let blacks = Values.storageBlacklist.join(`${Values.storageSep} `);
  322. $('#byu-blacklist').val(blacks);
  323. GM.setValue('savedblocks', blacks);
  324. search(true);
  325. }
  326. });
  327. })(jQuery);