Skip to content

Instantly share code, notes, and snippets.

@vsubhash
Created September 10, 2018 06:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vsubhash/aef574758879aca17bc0d067be81240e to your computer and use it in GitHub Desktop.
Save vsubhash/aef574758879aca17bc0d067be81240e to your computer and use it in GitHub Desktop.
YouTube Ads Disabler (com.vsubhash.js.youtube-ads-disabler) is a User Javascript for YouTube that will automaticaly disable ads, switch off autoplay, add RSS feeds, remove "Recommended for you" video, unhide description and display latest comments.
// ==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
// @include https://www.youtube.com/watch*
// @version 1
// @grant none
// ==/UserScript==
try {
window.setTimeout(
function() {
document.getElementsByTagName('video')[0].pause();
document.getElementsByTagName('video')[1].pause();
},
5*1000);
} catch (e) {
console.error("YAD Error: " + e);
}
document.addEventListener("DOMContentLoaded", fixYouTubeAnnoyances, false);
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)) {
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...");
var arAutoPlayIds = [ "toggleButton", "autoplay-checkbox" ];
for (var i = 0; i < arAutoPlayIds.length; i++) {
console.log("YAD: Checking for autoplay ID " + arAutoPlayIds[i]);
if ((document.getElementById(arAutoPlayIds[i]) != null) && (document.getElementById(arAutoPlayIds[i]).checked)) {
console.log("YAD: Aautoplay ID found " + arAutoPlayIds[i]);
document.getElementById(arAutoPlayIds[i]).click();
console.log("YAD: Autoplay ID disabled - " + arAutoPlayIds[i]);
}
}
}
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() {
console.log("YAD: Disabling/deleting ad containers...");
var arDivIds = ["AdSense", "watch7-sidebar-ads", "promotion-shelf", "live-chat-iframe"];
var arDivClasses = [ "adDisplay", "annotation", "html5-endscreen", "iv-promo", "videoAdUiBottomBar", "ytp-endscreen-content", "ytp-cards-button", "ytp-cards-teaser" ];
for (var i = 0; i < arDivIds.length; i++) {
var oDiv = document.getElementById(arDivIds[i]);
if (oDiv != null) {
oDiv.style.visibility = "hidden!important";
oDiv.style.display = "none!important";
oDiv.parentNode.removeChild(oDiv);
console.log("YAD: Removed " + arDivIds[i] + " by ID");
} else {
console.log("YAD: Not found: " + arDivIds[i] + " by ID");
}
sAdStyle = "#" + arDivIds[i] + ((i==0)?" ":" , ") + sAdStyle;
}
for (var i = 0; i < arDivClasses.length; i++) {
var oDivs = document.getElementsByClassName(arDivClasses[i]);
if (oDivs != null) {
for (var j = 0; j < oDivs.length; j++) {
oDivs[j].style.visibility = "hidden!important";
oDivs[j].style.display = "none!important";
oDivs[j].parentNode.removeChild(oDivs[j]);
}
} else {
console.log("YAD: Not found: " + oDivs[j] + " by ID");
}
sAdStyle = "*." + arDivClasses[i] + " , " + sAdStyle;
}
document.getElementsByTagName("head")[0].innerHTML = document.getElementsByTagName("head")[0].innerHTML + "\n<style>" + sAdStyle + "\n</style>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment