YouTube - whitelist channels in Adblock Plus

Helps whitelist YouTube channels in Adblock Plus

< Feedback on YouTube - whitelist channels in Adblock Plus

Question/comment

§
Posted:
Edited:

Patch: Avoid refresh

I've made a modification to my local copy of this script to avoid refreshing the page. This makes things smoother, because I use click-to-play and refreshes prevent the next video from loading. I've attached my local copy, and a Git-style patch is below. I couldn't help a little refactoring also. Please feel free to take this code or any part of it for inclusion in a newer version.

--- a/scratchpad.js
+++ b/scratchpad.js
@@ -12,20 +12,26 @@
 // @supportURL  https://adblockplus.org/forum/viewtopic.php?f=1&t=23697
 // ==/UserScript==

-// For static pages
-var uo = document.querySelector('#watch7-content link[href*="/user/"]');
-var uv = document.querySelector('.yt-user-info > a[href*="/channel/"]');
-if (uo) {
-  addMenu();
-  var ut = uo.href.slice(uo.href.lastIndexOf("/")+1);
-  if (location.href.search("&user=") == -1) location.replace(location.href+"&user="+ut);
-}
-else if (uv) {
-  addMenu();
-  var ut = uv.textContent;
-  if (location.href.search("&user=") == -1) location.replace(location.href+"&user="+ut);
+var updateHref = function (url) {
+  window.history.replaceState(history.state, "", url);
+};
+
+var activate = function () {
+  if (location.href.search("&user=") != -1) return;
+  
+  var uo = document.querySelector('#watch7-content link[href*="/user/"]');
+  var uv = document.querySelector('.yt-user-info > a[href*="/channel/"]');
+  var channelName = (uo && uo.href.slice(uo.href.lastIndexOf("/")+1)) || (uv && uv.textContent);
+
+  if (channelName) {
+    addMenu(channelName);
+    updateHref(location.href+"&user="+channelName);
+  }
 }

+// For static pages
+activate();
+
 // For dynamic content changes, like when clicking a video on the main page.
 // This bit is based on Gantt's excellent Download YouTube Videos As MP4 script:
 // https://github.com/gantt/downloadyoutube
@@ -34,21 +40,8 @@ var observer = new MutationObserver(function(mutations) {
     if (mutation.addedNodes !== null) {
       for (i = 0; i < mutation.addedNodes.length; i++) {
         if (mutation.addedNodes[i].id == "watch7-main-container") {
-          addMenu();
-          var uo = document.querySelector('#watch7-content link[href*="/user/"]');
-          var uv = document.querySelector('.yt-user-info > a[href*="/channel/"]');
-          if (uo) {
-            addMenu();
-            var ut = uo.href.slice(uo.href.lastIndexOf("/")+1);
-            if (location.href.search("&user=") == -1) location.replace(location.href+"&user="+ut);
-            break;
-          }
-          else if (uv) {
-            addMenu();
-            var ut = uv.textContent;
-            if (location.href.search("&user=") == -1) location.replace(location.href+"&user="+ut);
-            break;
-          } 
+          activate();
+          break;
         }
       }
     }
@@ -58,7 +51,7 @@ observer.observe(document.body, {childList: true, subtree: true});

 // Add the context menu to the user name below the video
 // Only works in Firefox
-function addMenu() {
+function addMenu(channelName) {

   var uh = document.getElementById("watch7-user-header");
   var menu = document.createElement("menu");
@@ -75,9 +68,7 @@ function addMenu() {
   function abpShowFilter() {
     var fpo = "@@||youtube.com/*&user=";
     var fpt = "$document";
-    if (uo) var ut = uo.href.slice(uo.href.lastIndexOf("/")+1);
-    else if (uv) var ut = uv.textContent;
-    var ffl = fpo+ut+fpt;
+    var ffl = fpo+channelName+fpt;
     var wh = document.getElementById("watch8-action-buttons");
     var wlf = wh.parentNode.querySelector("#whitelistfilter");
     if (!wlf && ffl) {
§
Posted:
Edited:
Nice
§
Posted:
Thank you for your contribution. As I said previously, I can't reproduce the issue, but I've implemented the change regardless because I see no difference.

Post reply

Sign in to post a reply.