Greasemonkey JavaScript-based YouTube adblocker and annoyances remover

Watching YouTube online is still impossible for me – the Net connection is still flaky and there are disturbances in the electricity supply. Downloading the videos and playing them offline is still the only option for me. Even then, the YouTube comes with a great deal of annoyances – ads (text and video), autoplay, “Recommended for you” video list, lack of RSS feeds, half-hidden description, YouTube-curated (or censored) “Top Comments”.

I have created several Greasemonkey scripts to deal with YouTube. Recently, I added a few more. Here they are:

The last one is new. It does several things – disables video ads, hides text ads, disables autoplay, deletes “recommended for you” ads.

Disabling video ads was tricky. The current implementation opens the same video URL in another tab and closes the current tab. This is repeated until YouTube relents and loads the original video. This can range anywhere from 2 to 10 reloads, probably. The code also finds the mute button and mutes the ad.

// ==UserScript==
// @name        YouTube Ads Disabler
// @namespace   com.vsubhash.js.youtube-ads-disabler
// @description Disables ads, swithces off autoplay, adds RSS link, deletes "recommended for you" videos, unhides description, displays latest comments first
// @include     https://www.youtube.com/watch*
// @version     1
// @grant       none
// ==/UserScript==

document.addEventListener("DOMContentLoaded", fixYouTubeAnnoyances, false);

window.setTimeout(
  function() {
    document.getElementsByTagName("video")[0].pause();
    document.getElementsByTagName("video")[1].pause();
  }, 
  3*1000);
  

function fixYouTubeAnnoyances() {
  console.log("YAD: Here");
  var sAdStyle = " { visibility: none!important; display: none!important; }";
  
  try {
    window.setTimeout(closeVideoAds, 1*1000);
    window.setTimeout(showDescription, 2*1000);
    window.setTimeout(removeRecommendedForYouAds, 3*1000);  
    window.setTimeout(disableAndHideAdContainers, 4*1000);  
    window.setTimeout(disableAutoPlay, 5*1000);
    
  } catch (e) {
    console.error("YAD Error: " + e);
  }
}

function closeVideoAds() {
  console.log("YAD: Detecting video ads...");        
  if ((document.getElementsByClassName("videoAdUiTopButtons").length > 0) || (document.getElementsByClassName("videoAdUi").length > 0)) {
    document.getElementsByTagName('video')[0].pause();
    console.log("YAD: Video ad found");      
    window.open(location.href, '_blank');
    //window.open(location, '_self').close();
    window.close();

    if (document.getElementsByClassName("ytp-mute-button").length > 0) {
      console.log("YAD: Video ad mute button found");        
      if (document.getElementsByClassName("ytp-volume-panel").length > 0) {
        if (document.getElementsByClassName("ytp-volume-panel")[0].getAttribute("aria-volumetext") != "100% volume muted") {
          document.getElementsByClassName("ytp-mute-button")[0].click();            
          console.log("YAD: Video ad muted");
        }
      }
    }      

  } else {
    console.log("YAD: No video ad");
  }
}


function disableAutoPlay() {
  console.log("YAD: Disabling autoplay...");
  if ((document.getElementById("autoplay-checkbox") != null) && (document.getElementById("autoplay-checkbox").checked)) {
    console.log("YAD: Autoplay button found");
    document.getElementById("autoplay-checkbox").click();  
    console.log("YAD: Autoplay disabled");
  }
}

function showDescription() {
  console.log("YAD: Finding description...");
  if (document.getElementById("action-panel-details") != null) {
    document.getElementById("action-panel-details").className = "action-panel-content yt-uix-expander yt-card yt-card-has-padding";
  }
  console.log("YAD: Description unhidden.");
}

function removeRecommendedForYouAds() {
  console.log("YAD: Removing recommended videos");
  var oRelatedColumn = document.getElementById("watch-related");
  if (oRelatedColumn != null) {
    var arRelatedVids = oRelatedColumn.getElementsByTagName("li");
    var j = 0;
    if (arRelatedVids.length > 0) {
      for (var i = arRelatedVids.length-1; i > -1; i--) {        
        if (arRelatedVids[i].textContent.indexOf("Recommended for you") != -1) {
          //console.log("YAD: Removing " + arRelatedVids[i].textContent);
          arRelatedVids[i].parentNode.removeChild(arRelatedVids[i]);
          ++j;
        }
      }
    }
    console.log("YAD: Removed " + j  + " recommended videos");      
  }
}

function disableAndHideAdContainers() {
  // Not published to protect YouTube content creators' interest
}

NOTE: I use an old version of Firefox and I spoof a newer but still old version in it. YouTube and other Google sites serve different styles of pages for different browser versions. The above code works with Firefox 42. To make the code work, you can spoof version 42 using the UserAgent Switcher add-on with this UA string.

Mozilla/5.0 (Windows NT 6.2; rv:42) Gecko/20100101 Firefox/42.0

Of course, I will not be publishing the ad-blocking code. YouTube apocalypse is on now. Ad agencies have done to YouTube what they had been doing for decades to newspaper and TV journalism – interfering in content. After CNN decided to launch a crusade against Alex Jones’ YouTube channels, ad agencies are now telling YouTube that their ads should not be playing on “controversial” content. The bottomline now is that videos from big music labels and stars can have all kind of crude/vulgar/unsuitable content but ordinary folk stepping out of the line will have to be immediately “demonetized.” I found one retired nurse, who publishes makeup tutorials, complaining that she never said a bad word or anything controversial but all her videos were demonetized.

Until the said apocalypse, YouTube content creators seem to have been generously paid. When the times were good, YouTube went all over the world asking people to make videos and make tons of money. Now, YouTube’s revenues are down and the first victims are the small and independent content creators who took YouTube’s bait. A million page views earns the creators very little. It is only with off-YouTube marketing deals and paid-promotion that YouTube content creators can make money. I am surprised that that many seemingly intelligent people left their day jobs and became “Youtubers” full time. Even more sad are the legion of retired folks whose savings have been destroyed by Bush and Obama’s zero-interest monetary regime and free-trade policies, and Wall Street’s looting of pension funds and local government funds. There can be no economy without local manufacturing. Selfies and cat videos are not what an advanced economy runs on.

Anyway, I can tolerate text ads. The videos ads are horrible. The “recommended for you” videos are downright creepy. Why are they recommended for me?

Advertisements
This entry was posted in news, technology and tagged , , . Bookmark the permalink.

Make a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s