MagicTouch / YouTube NonStop!

// ==UserScript==
// @name         YouTube NonStop!
// @namespace    heavensIsland.omg
// @version      0.8
// @description  Youtube.com Auto-confirm, Auto-Playnext, Auto-Fullscreen-Resume Tested with Firefox for fullscreen to work goto about:config -> full-screen-api.allow-trusted-requests-only = false
// @author       MagicTouch
// @match        *://www.youtube.com/*
// @grant        none
// @licence     MIT
// ==/UserScript==

 setInterval(function(){
    //===========================================================
    //Check#1 For confirmation dialog by confirm button existance
    //===========================================================
    var confirm_dialog = document.getElementById("confirm-button");
    if(confirm_dialog){

        //Removing popup confirmation dialog
        var elem = document.getElementsByTagName("ytd-popup-container"), index;
        for (index = elem.length - 1; index >= 0; index--) {

            //Remove element childs
            while (elem[index].firstChild) {
                elem[index].removeChild(elem[index].firstChild);
            }
        }
        //Resume playback by clicking player window
        elem = document.getElementById("movie_player");
        elem.click();
        console.log('%c C1-Youtube-Auto-Resumed from confirm dialog! ', 'background: #0f1c66; color: #ffffff');
        //Wait 1 second and make video fullscreen
        setTimeout(function() {
            //launchIntoFullscreen(document.getElementById("ytd-player"));
            if (!CheckFullscreen()) {
                document.querySelector(".ytp-fullscreen-button").click();
                console.log('%c C1-Youtube-WindowMode-Detected! Resuming-On-Fullscreen! ', 'background: #ba1d1d; color: #ffffff');
            }
        }, 1000);
    }
    //============================================
    //Check#2 For autoplay paused playnext button
    //============================================
    var playnextexist = document.querySelector(".ytp-upnext");
    if (playnextexist) {
        var playnextvisible = (document.querySelector(".ytp-upnext").offsetParent !== null);
        if (playnextvisible) {
            var elem2 = document.querySelector(".ytp-upnext-autoplay-icon");
            elem2.click();
            console.log('%c C2-Youtube-Auto-PlayNext Send! ', 'background: #2b6b26; color: #ffffff');

            setTimeout(function() {
                if (!CheckFullscreen()) {
                    document.querySelector(".ytp-fullscreen-button").click();
                    console.log('%c C2-Youtube-WindowMode-Detected! Resuming-On-Fullscreen! ', 'background: #ba1d1d; color: #ffffff');
                }
            }, 1500);
        }
    }
    
    //============================================
    //Check#3 For paused state and resume playback
    //============================================
    var elem3 = document.querySelector(".ytp-play-button");
    if (elem3) {
       if (elem3.getAttribute("aria-label").includes("Play")) {
           elem3.click();
           console.log('%c C3-Youtube-Auto-Resumed Send! ', 'background: #990082; color: #ffffff');
           if (!CheckFullscreen()) {
                    document.querySelector(".ytp-fullscreen-button").click();
                    console.log('%c C3-Youtube-WindowMode-Detected! Resuming-On-Fullscreen! ', 'background: #ba1d1d; color: #ffffff');
            }
       }
    }
 }, 4000);

function CheckFullscreen() {
    var state = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
    var event = state ? true : false;
    //TO DISABLE RESUME TO FULLSCREEN replace : return event; WITH return true;
    return event;
}