youtube继续播放

当出现"影片已暂停,要继续观赏吗?"对话方块时自动按下"是"

目前为 2020-03-18 提交的版本。查看 最新版本

// ==UserScript==
// @name               youtube continue play
// @name:en            youtube continue play
// @name:zh-CN         youtube继续播放
// @name:zh-TW         youtube繼續播放
// @name:ja            youtube履歴書再生
// @namespace          https://greasyfork.org/zh-TW/users/461233-jack850628
// @version            1.0
// @description        When the "Video paused, do you want to continue watching?" Dialog box appears, press "Yes" automatically
// @description:en     When the "Video paused, do you want to continue watching?" Dialog box appears, press "Yes" automatically
// @description:zh-TW  當出現"影片已暫停,要繼續觀賞嗎?"對話方塊時自動按下"是"
// @description:zh-CN  当出现"影片已暂停,要继续观赏吗?"对话方块时自动按下"是"
// @description:ja    「ビデオを一時停止しました。引き続き視聴しますか?」ダイアログボックスが表示されたら、「はい」を自動的に押します
// @author             jack850628
// @include            /^https?:\/\/(:?.*?\.?)youtube.com/.*$/
// @license            MIT
// ==/UserScript==

(function() {
    let pausedF = function({target: videoPlay}){
        setTimeout(function(){
            let ytConfirmDialog = document.querySelector('yt-confirm-dialog-renderer');
            if(
                ytConfirmDialog &&
                ytConfirmDialog.parentElement &&
                ytConfirmDialog.parentElement.style.display != 'none'
            ){
                ytConfirmDialog.querySelector('yt-button-renderer[dialog-confirm]').click();
            }
        }, 500);
    }
    function listenerVideoPlayer(){
        let videoPlay = document.querySelector('video');
        if(!videoPlay){
            return;
        }
        videoPlay.addEventListener('pause', pausedF);
    }
    window.spf._request = window.spf.request;
    Object.defineProperty(window.spf, 'request', {
        value: function(){
            if(arguments[1]){
                if(arguments[1].onDone){
                    let onDone = arguments[1].onDone;
                    arguments[1].onDone = function(){
                        let result = onDone.apply(this,arguments);
                        listenerVideoPlayer();
                        return result;
                    }
                }else{
                    arguments[1].onDone = () => listenerVideoPlayer();
                }
            }
            return window.spf.script._done.apply(this,arguments);
        },
        writable: true,
        configurable: true
    });
    listenerVideoPlayer();
})();