Youtube.com Auto Continue Playback

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

目前为 2021-03-06 提交的版本。查看 最新版本

// ==UserScript==
// @name         Youtube.com Auto Continue Playback
// @namespace    q1k
// @version      1.6.0
// @description  auto continue when it asks to press 'yes' to keep playing
// @author       q1k
// @match        *://music.youtube.com/*
// @include      *://www.youtube.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

var check_playback_interval = 1000; //enter interval time in ms (1000ms = 1second)
//beware of low values-- browser/system slowdowns may occur

var play_text = "play"; // if your language is not english change this to match (if multiple words, enter only the first word)
var pause_text = "pause"; // if your language is not english change this to match (if multiple words, enter only the first)

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 userPauseDetected=false;
var skipcheck=false;
var clickedButton=false;

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 (hotkey: X)");
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="<span class='onoffswitch-inner'></span><span class='onoffswitch-switch'></span>";

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;display:flex;flex-direction:column;justify-content:center;}.onoffswitch-checkbox{display:none;}.onoffswitch-label{display:block;overflow:hidden;cursor:pointer;border:2px solid #999;border-radius:50px;position:relative;}.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);

function revealpopup() {
    checkplaybackstatus.classList.remove("myfadeoutclass");
    checkplaybackstatus.classList.add("myshowclass");
    setTimeout(function(){checkplaybackstatus.classList.add("myfadeoutclass");checkplaybackstatus.classList.remove("myshowclass");},100);
}

function isPlaying() {
    if (playbackbutton.title.toLowerCase().split(" ")[0]==play_text) {
        return false;
    }
    else if (playbackbutton.title.toLowerCase().split(" ")[0]==pause_text) {
        return true;
    }
    return true;
}

function startchecker() {
    checkboxinput.checked=true;
    autocontinue=true;
    userPauseDetected=false;
    checkplaybackID = setInterval(function(){
        if(autocontinue==false){return;}
        if(skipcheck){return;}
        if(userPauseDetected){return;}

        if( isPlaying() && playbackbutton.hidden==false ){ return; }
        else if ( !isPlaying() ) { playbackbutton.click(); }
    },check_playback_interval);

    checkplaybackstatus.innerText="playback checker: ON";
    revealpopup();
}

function stopchecker() {
    checkboxinput.checked=false;
    autocontinue=false;
    userPauseDetected=false;
    clearInterval(checkplaybackID);

    checkplaybackstatus.innerText="playback checker: OFF";
    revealpopup();
}

checkboxinput.onclick=function(e){
    if(this.checked){ startchecker(); }
    else { stopchecker(); }
    clickedButton=true;
}
window.addEventListener('keyup', function(e) {
    if (e.code == "KeyX" || e.which == 88) {
        if (autocontinue) { stopchecker(); }
        else { startchecker(); }
    }
    else {
        skipcheck=true;
        setTimeout(function(){
            if( isPlaying() ) {
                userPauseDetected=false;
            } else {
                userPauseDetected=true;
            }
            skipcheck=false;
        },100);
    }
});
window.addEventListener('mouseup', function(e) {
    if (e.which == 1) {
        skipcheck=true;
        setTimeout(function(){
            if (clickedButton) { clickedButton=false; skipcheck=false; return; }
            if( isPlaying() ) {
                userPauseDetected=false;
            } else {
                userPauseDetected=true;
            }
            skipcheck=false;
        },250);
    }
});
var labelappended=false;
var checkElementID;
function startElementChecker(){
    if(document.domain=="music.youtube.com"){
        checkElementID = 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;
            playbackbutton = document.querySelector("#play-pause-button");
            clearInterval(checkElementID);
            if(autocontinue){
                startchecker();
            }
        //}
        },check_playback_interval);
    } else if(document.domain=="www.youtube.com"){
        checkElementID = setInterval(function(){
        //if(!labelappended){
            if ( (typeof(document.querySelector(".ytp-chrome-controls .ytp-left-controls")) == undefined || document.querySelector(".ytp-chrome-controls .ytp-left-controls") == null) ) {
                return;
            }
            var leftcontrolsbuttons = document.querySelector(".ytp-chrome-controls .ytp-left-controls");
            //leftcontrolsbuttons.appendChild(checkboxdiv);
            var beforethis = document.querySelector(".ytp-volume-area");
            beforethis.parentNode.insertBefore(checkboxdiv, beforethis);
            labelappended=true;
            playbackbutton = document.querySelector(".ytp-chrome-controls .ytp-left-controls .ytp-play-button");
            clearInterval(checkElementID);
            if(autocontinue){
                startchecker();
            }
        //}
        },check_playback_interval);
    }
}
startElementChecker();






/*
https://gf.qytechs.cn/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
- 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
<br>

**== 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

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址