// ==UserScript==
// @name music.youtube.com auto continue playback
// @namespace q1k
// @version 1.3
// @description auto continue when it asks to press 'yes' to keep playing
// @author q1k
// @match *://music.youtube.com/*
// @grant none
// @run-at document-idle
// @downloadURL none
// ==/UserScript==
var check_playback_interval = 1000; //enter interval time in ms (1000ms = 1second)
//beware of low values--browser slowdowns may occur
var play_text = "play"; // if your language is not english change this to match
var pause_text = "pause"; // if your language is not english change this to match
var autostart = false; // should it be enabled by default?,..
//if you set this to true make sure to allow auto-playing of sounds,,, see description below for more info
var playbackbutton;
var autocontinue;
var checkplaybackID;
var checkplaybackstatus = document.createElement('div');
checkplaybackstatus.setAttribute("id","checkplaybackstatus");
checkplaybackstatus.setAttribute("style","position:fixed;bottom:85px;left:12px;/*top:45px;left: 50%;transform:translateX(-50%);*/padding:15px;pointer-events:none;font-size:2em;background:rgba(180,180,180,1);z-index:999999999;");
checkplaybackstatus.setAttribute("class","myfadeoutclass");
checkplaybackstatus.innerText="";
var checkboxdiv = document.createElement("div");
checkboxdiv.setAttribute("class","onoffswitch");
checkboxdiv.setAttribute("title","Playback checker");
var checkboxinput = document.createElement("input");
checkboxinput.setAttribute("type","checkbox");
checkboxinput.setAttribute("name","onoffswitch");
checkboxinput.setAttribute("class","onoffswitch-checkbox");
checkboxinput.setAttribute("id","myonoffswitch");
if (autostart){
checkboxinput.checked=true;
autocontinue=true;
} else {
checkboxinput.checked=false;
autocontinue=false;
}
var checkboxinputlabel = document.createElement("label");
checkboxinputlabel.setAttribute("class","onoffswitch-label");
checkboxinputlabel.setAttribute("for","myonoffswitch");
checkboxinputlabel.innerHTML="";
checkboxdiv.appendChild(checkboxinput);
checkboxdiv.appendChild(checkboxinputlabel);
document.body.appendChild(checkplaybackstatus);
var myCSS = document.createElement("style");
myCSS.innerHTML=".myfadeoutclass{transition:opacity 1s ease-in;transition:opacity 1s cubic-bezier(0.5, 0, 0.85, 0.45);opacity:0;}.myshowclass{opacity:1!important;}.onoffswitch{margin:0 15px 0 20px;position:relative;width:55px;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;}.onoffswitch-checkbox{display:none;}.onoffswitch-label{display:block;overflow:hidden;cursor:pointer;border:2px solid #999;border-radius:50px;}.onoffswitch-inner{display:block;width:200%;margin-left:-100%;transition:margin 0.3s ease-in 0s;}.onoffswitch-inner:before,.onoffswitch-inner:after{display:block;float:left;width:50%;height:16px;padding:0;line-height:16px;font-size:11px;color:white;font-family:Trebuchet,Arial,sans-serif;font-weight:700;box-sizing:border-box;}.onoffswitch-inner:before{content:'ON';padding-left:5px;background-color:#fff;color:#000;}.onoffswitch-inner:after{content:'OFF';padding-right:5px;background-color:#777;color:#fff;text-align:right;}.onoffswitch-switch{display:block;width:20px;margin:-2px;background:#666;position:absolute;top:0;bottom:0;right:35px;border:2px solid #999;border-radius:50px;transition:all 0.3s ease-in 0s;}.onoffswitch-checkbox:checked+.onoffswitch-label .onoffswitch-inner{margin-left:0;}.onoffswitch-checkbox:checked+.onoffswitch-label .onoffswitch-switch{right:0;background:#fff;}";
document.head.appendChild(myCSS);
var labelappended=false;
function startchecker() {
checkplaybackID = setInterval(function(){
if(!labelappended){
if ( (typeof(document.querySelector("#left-controls .left-controls-buttons")) == undefined || document.querySelector("#left-controls .left-controls-buttons") == null) ) {
return;
}
var leftcontrolsbuttons = document.querySelector("#left-controls .left-controls-buttons");
leftcontrolsbuttons.appendChild(checkboxdiv);
labelappended=true;
}
if(autocontinue==false){return;}
playbackbutton = document.querySelector("#play-pause-button");
if( playbackbutton.title.toLowerCase()==pause_text && playbackbutton.hidden==false ){return;}
else if ( playbackbutton.title.toLowerCase()==play_text ) { playbackbutton.click(); }
},check_playback_interval);
checkplaybackstatus.innerText="playback checker: ON";
checkplaybackstatus.classList.remove("myfadeoutclass");
checkplaybackstatus.classList.add("myshowclass");
setTimeout(function(){checkplaybackstatus.classList.add("myfadeoutclass");checkplaybackstatus.classList.remove("myshowclass");},100);
}
function stopchecker() {
clearInterval(checkplaybackID);
checkplaybackstatus.innerText="playback checker: OFF";
checkplaybackstatus.classList.remove("myfadeoutclass");
checkplaybackstatus.classList.add("myshowclass");
setTimeout(function(){checkplaybackstatus.classList.add("myfadeoutclass");checkplaybackstatus.classList.remove("myshowclass");},100);
}
checkboxinput.onclick=function(){
if(this.checked){ autocontinue=true;startchecker(); }
else { autocontinue=false;stopchecker(); }
}
document.addEventListener('keyup', function(e) {
if (e.code == "KeyX" || e.which == 88) {
if (autocontinue) { checkboxinput.checked=false;autocontinue=false;stopchecker(); }
else { checkboxinput.checked=true;autocontinue=true;startchecker(); }
}
});
startchecker();
/*
https://greasyfork.org/en/scripts/390352

Are you annoyed when youtube music playback suddenly stops,
only to find that it asks for your input to make sure you are still there?
Install this little script and you'll never have to bother with the "You There? Continue playing?" popup.
The script will automatically resume playback.
**Features:**
- press X to enable/disable auto resuming
- popup to show the current status (active or inactive) when X is pressed
- button next to the controls to toggle the playback checker complementary to the hotkey
**== IMPORTANT ==**
- Firefox users, you'll have to allow auto-playing of sounds if you want music to start immediately, see attached picture
- Chrome/Opera users, no additional steps are required, but just to be safe you can allow sounds anyway, see attached picture
**Disclaimer:**
If the 'continue playing popup' appeared, playback may stop for up to 1 second (1000 milliseconds) until it checks again.
If you want this gap to be smaller, then change the variable (check_playback_interval) value in the top from 1000 to a lower one.
Just remember, the more often it checks the more resources its going to use.
*/