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.77
  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://greasyfork.org/en/scripts/13226-youtube-whitelist-channels-in-ublock-origin
  14. // with adaption from https://greasyfork.org/en/forum/discussion/8985
  15. // further updates to enable compatibility with "material design" by burretploof
  16.  
  17. // First page load
  18. //
  19.  
  20. var exposeUserInURL = function() {
  21. 'use strict';
  22.  
  23. // To fix issue with user name, parse it from a different query, so
  24. // the following method works currently. Variable channelId will thus
  25. // contain the Youtube user name.
  26. // Replace lines from "var link = document.querySelector ..." to
  27. // "var channelId = ..." with the following code (within "// ---"):
  28. // ---
  29. if(window.location.href.indexOf("/watch") > -1) {
  30. var link = document.querySelector('[id="watch7-user-header"] a[href^="/user/"]');
  31. if ( link === null ) {
  32. link = document.querySelector('[id="watch7-user-header"] a[href^="/channel/"]');
  33. if ( link === null)
  34. //return;
  35. link = document.querySelector('[id="top-row"] a[href^="/user/"]');
  36. if ( link === null ) {
  37. link = document.querySelector('[id="top-row"] a[href^="/channel/"]');
  38. if ( link === null)
  39. return;
  40. }
  41. }
  42. }
  43. var linkHref = link.getAttribute('href');
  44. var linkmatch = linkHref.match(/\/(user|channel)\/(.+)/);
  45. if (linkmatch === null)
  46. return;
  47. var channelId = linkmatch[2];
  48. // ---
  49.  
  50. // Code below need not be changed
  51. var newArg = channelId !== '' ? 'user=' + encodeURIComponent(channelId) : '';
  52. var matches = location.search.match(/(?:[?&])(user=(?:[^&]+|$))/);
  53. var oldArg = matches !== null ? matches[1] : '';
  54. if ( newArg === oldArg ) {
  55. return;
  56. }
  57. var href = location.href;
  58. if ( oldArg === '' ) {
  59. location.replace(href + (location.search === '' ? '?' : '&') + newArg);
  60. return;
  61. }
  62. location.replace(href.replace(oldArg, newArg));
  63. };
  64.  
  65. setTimeout(exposeUserInURL, 25);
  66.  
  67. // DOM modifications
  68.  
  69. var mutationHandlerTimer = null;
  70.  
  71. var mutationHandlerAsync = function() {
  72. 'use strict';
  73.  
  74. mutationHandlerTimer = null;
  75. exposeUserInURL();
  76. };
  77.  
  78. var mutationHandler = function(mutations) {
  79. 'use strict';
  80.  
  81. if ( mutationHandlerTimer !== null ) {
  82. return;
  83. }
  84.  
  85. for ( var i = 0; i < mutations.length; i++ ) {
  86. if ( mutations[i].addedNodes ) {
  87. mutationHandlerTimer = setTimeout(mutationHandlerAsync, 25);
  88. break;
  89. }
  90. }
  91. };
  92.  
  93. var observer = new MutationObserver(mutationHandler);
  94. observer.observe(document.body, { childList: true, subtree: true });