YouTube - whitelist channels in uBlock Origin

To whitelist YouTube channels in uBlock Origin

  1. // ==UserScript==
  2. // @name YouTube - whitelist channels in uBlock Origin
  3. // @namespace https://github.com/gorhill/uBlock
  4. // @version 1.6
  5. // @description To whitelist YouTube channels in uBlock Origin
  6. // @author Raymond Hill (gorhill)
  7. // @match https://*.youtube.com/*
  8. // @grant none
  9. // @license http://creativecommons.org/licenses/by-sa/4.0/
  10. // @supportURL https://github.com/gorhill/uBlock/issues
  11. // ==/UserScript==
  12.  
  13. // based on https://greatest.deepsurf.us/en/scripts/13226-youtube-whitelist-channels-in-ublock-origin
  14. // with adaption from https://greatest.deepsurf.us/en/forum/discussion/8985
  15.  
  16. // First page load
  17. //
  18.  
  19. var exposeUserInURL = function() {
  20. 'use strict';
  21.  
  22. // To fix issue with user name, parse it from a different query, so
  23. // the following method works currently. Variable channelId will thus
  24. // contain the Youtube user name.
  25. // Replace lines from "var link = document.querySelector ..." to
  26. // "var channelId = ..." with the following code (within "// ---"):
  27. // ---
  28. var link = document.querySelector('[id="watch7-user-header"] a[href^="/user/"]');
  29. if ( link === null ) {
  30. link = document.querySelector('[id="watch7-user-header"] a[href^="/channel/"]');
  31. if ( link === null)
  32. return;
  33. }
  34. var linkHref = link.getAttribute('href');
  35. var linkmatch = linkHref.match(/\/(user|channel)\/(.+)/);
  36. if (linkmatch === null)
  37. return;
  38. var channelId = linkmatch[2];
  39. // ---
  40.  
  41. // Code below need not be changed
  42. var newArg = channelId !== '' ? 'user=' + encodeURIComponent(channelId) : '';
  43. var matches = location.search.match(/(?:[?&])(user=(?:[^&]+|$))/);
  44. var oldArg = matches !== null ? matches[1] : '';
  45. if ( newArg === oldArg ) {
  46. return;
  47. }
  48. var href = location.href;
  49. if ( oldArg === '' ) {
  50. location.replace(href + (location.search === '' ? '?' : '&') + newArg);
  51. return;
  52. }
  53. location.replace(href.replace(oldArg, newArg));
  54. };
  55.  
  56. setTimeout(exposeUserInURL, 25);
  57.  
  58. // DOM modifications
  59.  
  60. var mutationHandlerTimer = null;
  61.  
  62. var mutationHandlerAsync = function() {
  63. 'use strict';
  64.  
  65. mutationHandlerTimer = null;
  66. exposeUserInURL();
  67. };
  68.  
  69. var mutationHandler = function(mutations) {
  70. 'use strict';
  71.  
  72. if ( mutationHandlerTimer !== null ) {
  73. return;
  74. }
  75.  
  76. for ( var i = 0; i < mutations.length; i++ ) {
  77. if ( mutations[i].addedNodes ) {
  78. mutationHandlerTimer = setTimeout(mutationHandlerAsync, 25);
  79. break;
  80. }
  81. }
  82. };
  83.  
  84. var observer = new MutationObserver(mutationHandler);
  85. observer.observe(document.body, { childList: true, subtree: true });