music.youtube.com auto continue playback

auto continue when it asks to press 'yes' to keep playing

目前為 2019-09-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name         music.youtube.com auto continue playback
// @namespace    q1k
// @version      1.1
// @description  auto continue when it asks to press 'yes' to keep playing
// @author       q1k
// @match        *://music.youtube.com/*
// @grant        none
// ==/UserScript==

var check_playback_interval = 1000; //enter interval time in ms (1000ms = 1second)
var play_text = "play";
var pause_text = "pause";

var playbackbutton;
var autocontinue = true;
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");
checkboxdiv.innerHTML="<input type='checkbox' name='onoffswitch' class='onoffswitch-checkbox' id='myonoffswitch' checked><label class='onoffswitch-label' for='myonoffswitch'><span class='onoffswitch-inner'></span><span class='onoffswitch-switch'></span></label>";
var leftcontrolsbuttons = document.querySelector("#left-controls .left-controls-buttons");
leftcontrolsbuttons.appendChild(checkboxdiv);
var checkboxinput=document.querySelector("#myonoffswitch");

document.body.appendChild(checkplaybackstatus);
document.head.innerHTML += "<style>.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;}</style>";

checkboxinput.onclick=function(){
    if(this.checked){
        startchecker();
    }
    else{
        stopchecker();
    }
}

function startchecker() {
    checkplaybackID = setInterval(function(){
        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);
    autocontinue=true;
    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);
    autocontinue=false;
    checkplaybackstatus.innerText="playback checker: OFF";
    checkplaybackstatus.classList.remove("myfadeoutclass");
    checkplaybackstatus.classList.add("myshowclass");
    setTimeout(function(){checkplaybackstatus.classList.add("myfadeoutclass");checkplaybackstatus.classList.remove("myshowclass");},100);
}

startchecker();

document.addEventListener('keyup', function(e) {
    if (e.code == "KeyX" || e.which == 88) {
        if (autocontinue) { checkboxinput.checked=false;stopchecker(); }
        else { checkboxinput.checked=true;startchecker(); }
    }
});

/*
https://greasyfork.org/en/scripts/390352
![image](https://i.imgur.com/xDYYoJ6.png)

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
- has a nifty popup to show the current status (active or inactive) when X is pressed

<br>
**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.
*/