Ryunigia / YouTube Adfriend

// ==UserScript==
// @name        YouTube Adfriend
// @description This script main focus, is to give control of the advertisements on YouTube to the user.
// @namespace   Ryunigia
// @author      Ryunigia
// @homepage    http://glennmoreels.eu/
// @icon        https://image.flaticon.com/icons/png/128/174/174883.png
// @include     http://www.youtube.com/*
// @include     https://www.youtube.com/*
// @exclude     https://www.youtube.com/embed/*
// @exclude     http://www.youtube.com/embed/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// @version     0.776
// @grant       GM_addStyle
// @grant       GM_getValue
// @grant       GM_setValue
// @license     MIT
// ==/UserScript==
var AdVolumeMultiplier = GM_getValue("SoundMultiplier", 0.07);
var CountDownSeconds = GM_getValue("MovieCountDown", 4);
var CurrentCountDownSeconds = CountDownSeconds;
var BannerCountDownSeconds = GM_getValue("BannerCountDown", 12);
var CurrentBannerCountDownSeconds = BannerCountDownSeconds;
var BannerOpaque = GM_getValue("BannerOpacity", 0.4);
var AddSubscriberNoBlock = GM_getValue("AddSubscriberNoBlock", true);
var oldVolume = 0.5;
var UIContainer;
var skipContainer;
var closeContainer;
var video;
var button;
var InputBox;
var SkipBannerInputBox;
var SoundInputBox;
var OpacityInputBox;
var AddSubscriberNoBlockInputBox;

// only when add
function AdDetected (jNode) 
{   
    //Check if the ad is stil there
    UIContainer = document.querySelector(".videoAdUi");
    skipContainer = document.querySelector(".videoAdUiSkipContainer");
    video = document.querySelector(".html5-main-video");
    oldVolume = video.volume;
    //var preSkip = document.querySelector(".videoAdUiPreSkipContainer");
    //preSkip.style.opacity = 0;
    //skipContainer.opacity = 1;
    //skipContainer.display = "initial";
    button = document.querySelector(".videoAdUiSkipButton");
    //CountDownText = document.createTextNode("Counting");
    //skipContainer.appendChild(CountDownText);
    if(UIContainer)
    {
        console.log("Adfriend: Add detected");
        setTimeout( QuickSoundCheck, 100 );
        setTimeout( CheckIfSkipable, 1000);
    }
}
waitForKeyElements (".videoAdUi", AdDetected);
//Check if the skip button is visible
function CheckIfSkipable(jNode)
{
    if(skipContainer.style.opacity > 0.5)
    {
        console.log("Addfriend: movie ad countdown started");
        CurrentCountDownSeconds = CountDownSeconds;
        CountDownToSkip();
    }
    else setTimeout( CheckIfSkipable, 1000);
        
}
// Count until skip
function CountDownToSkip(jNode)
{
    --CurrentCountDownSeconds;
    console.log("Adfriend: counting down: "+CurrentCountDownSeconds);
    if(button)
        button.textContent = "Skip in "+CurrentCountDownSeconds;
    else 
    {
        console.log("Adfriend:Could not find button");
        button = document.querySelector(".videoAdUiSkipButton");
        if(button)
            CountDownToSkip();
        else
        {
            console.log("Adfriend:no button, ad is possibly skipped");
            return;
        }
    }
    if(CurrentCountDownSeconds < 1)
        SkipAd();
    else
        setTimeout( CountDownToSkip, 1000);
}
// REDUCE SOUND
function QuickSoundCheck(jNode)
{
    //check if ad
    UIContainer = document.querySelector(".videoAdUi");
    if(UIContainer)
    {
        //console.log("Add detected: current Volume:"+ video.volume + " vs old:" + oldVolume);
        if(video.volume > oldVolume * AdVolumeMultiplier)
        {
            console.log("volume was readjusted: "+video.volume);
            video.volume = oldVolume * AdVolumeMultiplier;
            console.log("Lowered to:"+video.volume);
        }
        setTimeout(QuickSoundCheck,500);
        return;
    }
    
    //add is gone
    console.log("Add is gone, moving volume back to: "+oldVolume);
    video.volume = oldVolume;
}
// SKIP THE ADD, WHEN THEBUTTON IS AVAILABLE
function SkipAd(jNode)
{
    //retry
    button = document.querySelector(".videoAdUiSkipButton");
    UIContainer = document.querySelector(".videoAdUi");
    
    if(button)
    {
        console.log("Skipping ad");
        button.click();
    }
    else if(UIContainer)
    {
        console.log("Can't skip this overpayed advertisement, sorry folks");
        return;
    }
    
    button = document.querySelector(".videoAdUiSkipButton");
    if(button)
    {
        console.log("Ad skip failed... retrying");
        setTimeout(SkipAd,1000);
        return;
    }
    else
    {
        console.log("Ad was successfully skipped~");
    }
}

// CHECK IF SUBSCRIBED and create ui
function checkSubscribed (jNode) 
{
    // check it
    if ( ! /noblock/.test (window.location.href) ) 
    {
        console.log("Checking subscription");
        var elem = document.querySelector("#watch7-headline");
        if(elem)
        {
            if(AddSubscriberNoBlock)
            {
                var child = elem.children[0].children[1];
                if (child)
                {
                    if (child.getAttribute("data-is-subscribed") == 'True')
                    {
                        location.replace (location.href + "&noblock");
                    }
                }
            }
        }
        else
        {
            var subscribebox = document.querySelector("#subscribe-button");
            var child = subscribebox.children[0].children[0];
            if(child)
            {
                //if()
                var subscribed = child.getAttribute("subscribed");
                if(subscribed !== null)
                    location.replace (location.href + "&noblock");
                else
                {
                    console.log("not subscribed?");
                    //console.log(child);
                    //console.log(subscribed);
                }
            }
            else
            {
                console.log("Could not find subscribe box");
            }
        }
    }
}
waitForKeyElements ("#watch7-subscription-container", checkSubscribed);
waitForKeyElements ("#watch7-headline", checkSubscribed);
function AddUI (jNode)
{
    var adfriend = document.querySelector("#Adfriend");
    if(adfriend)
    {
        console.log("Adfriend was already added");
    }
    else
    {
        //Our interface container
        var newspan = document.createElement("SPAN");
        newspan.className = "date style-scope ytd-video-secondary-info-renderer";
        newspan.style.display = "inline-block";
        newspan.id = "Adfriend";
        newspan.style.marginLeft = "5px";
        newspan.style.marginTop = "5px";
        // old system or new system?
        var header = document.querySelector("#placeholder-player");
        if(!header) header = document.querySelector("#info");
        if(header)
            console.log("Found suitable ui container!");
        else
            console.log("Can't find place to add ui container..");
        header.appendChild(newspan);
        //Information
        var textnode = document.createTextNode("Adfriend::Movie:skip:");
        newspan.appendChild(textnode);

        //Input
        InputBox = document.createElement("input");
        InputBox.type = "number";
        InputBox.style.width = "25px";
        InputBox.defaultValue = CountDownSeconds;
        InputBox.oninput = saveValue;
        newspan.appendChild(InputBox);

        textnode = document.createTextNode("sound%:");
        newspan.appendChild(textnode);
        //Input
        SoundInputBox = document.createElement("input");
        SoundInputBox.type = "number";
        SoundInputBox.style.width = "35px";
        SoundInputBox.defaultValue = AdVolumeMultiplier * 100;
        SoundInputBox.oninput = saveValue;
        newspan.appendChild(SoundInputBox);

        textnode = document.createTextNode("Banner:skip:");
        newspan.appendChild(textnode);

        SkipBannerInputBox = document.createElement("input");
        SkipBannerInputBox.type = "number";
        SkipBannerInputBox.style.width = "35px";
        SkipBannerInputBox.defaultValue = BannerCountDownSeconds;
        SkipBannerInputBox.oninput = saveValue;
        newspan.appendChild(SkipBannerInputBox);

        textnode = document.createTextNode("Opacity%:");
        newspan.appendChild(textnode);

        OpacityInputBox = document.createElement("input");
        OpacityInputBox.type = "number";
        OpacityInputBox.style.width = "35px";
        OpacityInputBox.defaultValue = BannerOpaque * 100;
        OpacityInputBox.oninput = saveValue;
        newspan.appendChild(OpacityInputBox);

        //AddSubscriberNoBlock
        textnode = document.createTextNode("sub&noblock:");
        newspan.appendChild(textnode);

        AddSubscriberNoBlockInputBox = document.createElement("input");
        AddSubscriberNoBlockInputBox.type = "checkbox";
        //AddSubscriberNoBlockInputBox.style.width = "35px";
        AddSubscriberNoBlockInputBox.checked = AddSubscriberNoBlock;
        AddSubscriberNoBlockInputBox.onclick = saveValueNoblock;
        newspan.appendChild(AddSubscriberNoBlockInputBox);
    }
}
waitForKeyElements ("#player-container", AddUI);
waitForKeyElements ("#placeholder-player", AddUI);
function saveValue()
{
    CountDownSeconds = InputBox.value;
    GM_setValue("MovieCountDown", CountDownSeconds);
    
    BannerCountDownSeconds = SkipBannerInputBox.value;
    GM_setValue("BannerCountDown", BannerCountDownSeconds);
    
    AdVolumeMultiplier = SoundInputBox.value / 100;
    GM_setValue("SoundMultiplier", AdVolumeMultiplier);
    
    BannerOpaque = OpacityInputBox.value / 100;
    GM_setValue("BannerOpacity", BannerOpaque);
}
function saveValueNoblock()
{
    AddSubscriberNoBlock = AddSubscriberNoBlockInputBox.checked;
    GM_setValue("AddSubscriberNoBlock", AddSubscriberNoBlock);   
}


//make banner ads transparent
function bannerFound(jnode)
{
    var bannerContainer = document.querySelector(".ima-container");
    bannerContainer.style.opacity = BannerOpaque;
    //bannerContainer.style.transition = "";
    CurrentBannerCountDownSeconds = BannerCountDownSeconds;
    console.log("Adfriend: Banner ad detected: applying opacity");
    
    //<div class="close-padding"><a class="close-button" title="Close" role="button" tabindex="0"></a></div>
    var closeParent = document.querySelector(".close-padding");
    closeContainer = document.createElement("SPAN");
    closeParent.appendChild(closeContainer);
    //var countDownText = document.createTextNode(""+BannerCountDownSeconds);
    closeContainer.appendChild(document.createTextNode(BannerCountDownSeconds));
    setTimeout(CloseBannerCountDown,1000); 
}
waitForKeyElements (".ima-container", bannerFound);
function CloseBannerCountDown()
{
    var bannerContainer = document.querySelector(".ima-container");
    bannerContainer.style.opacity = BannerOpaque;
    --CurrentBannerCountDownSeconds;
        closeContainer.textContent = CurrentBannerCountDownSeconds;
    if(CurrentBannerCountDownSeconds < 1)
        CloseBannerAd();
    else
        setTimeout( CloseBannerCountDown, 1000);
}
function CloseBannerAd()
{
    var closeButton = document.querySelector(".close-button");
    if(closeButton)
        closeButton.click();
    
    closeButton = document.querySelector(".close-button");
    if(closeButton)
    {
        console.log("Adfriend: Banner ad closing failed");
        setTimeout(CloseBannerAd,1000);
    }else
    {
        console.log("Adfriend: succesfully closed banner ad");
        CurrentBannerCountDownSeconds = BannerCountDownSeconds;
        var bannerContainer = document.querySelector(".ima-container");
        bannerContainer.style.opacity = 1.0;
        //bannerContainer.style.transition = "";
        CurrentBannerCountDownSeconds = BannerCountDownSeconds;
    }
}