Twitch clips auto player

Auto plays twich clips

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Twitch clips auto player
// @namespace    http://tampermonkey.net/
// @version      69.4201
// @description  Auto plays twich clips
// @author       lazylion2
// @match        https://*.twitch.tv/*

// @require       https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

(function main () {
    var clips_in_url= 0;
    check_for_clip_in_url()
    var started = 0
    function nextvideo() {
        if (started){
        //console.log('next')
        var curUrl = window.location.href.match("clip/(.*)-")[1]
        var allCards = document.querySelectorAll('[data-a-target^="clips-card"]')
        for (var i=0;i<allCards.length;i++){
            //console.log('i =',i)
            var ref = $(allCards[i]).find('div a:first')[0].href

            if(ref.indexOf(curUrl)>-1){
               // console.log("i = ",i,"all=",allCards.length)
                if (i+1 == allCards.length){
                   // console.log("i == all")
                    allCards[i].scrollIntoView();
                    setTimeout(() => {
                        var newref = document.querySelectorAll('[data-a-target^="clips-card"]')
                        $(newref[i+1]).find('div a:first')[0].click()

                    }, 1000);
                    started = 0
                    break;


                }
                else{
               // console.log(allCards[i+1] )
                         setTimeout(() => {
                        $(allCards[i+1]).find('div a:first')[0].click()
                    }, 1000);
                    started = 0

                  }
                break;
            }
        }
        }

    }
    function findvideo() {
        const videoPlayer = document.getElementsByTagName('video')[0];
        //console.log(videoPlayer)
        videoPlayer.addEventListener('pause', (event) => {
            //console.log('paused')

        })
        videoPlayer.addEventListener('ended', (event) => {
            //console.log('ended')
            if(clips_in_url){
                nextvideo()
            }

        })
        videoPlayer.addEventListener('play', (event) => {
            //console.log('started')
            started = 1

        })
    }


    let observer = new MutationObserver(mutationRecords => {
        mutationRecords.forEach(function(mutation) {
            var v = document.body.getElementsByTagName("video")
            if (v.length >0){
                started = 1
                //console.log('f')
                findvideo()
                observer.disconnect()



            }


        });
    });
    function startObserver(){
        observer.observe(document.querySelector('body'), { childList: true, subtree: true });
    }
    startObserver()

// all this to detect url change -_-
    history.pushState = ( f => function pushState(){
        var ret = f.apply(this, arguments);
        window.dispatchEvent(new Event('pushstate'));
        window.dispatchEvent(new Event('locationchange'));
        return ret;
    })(history.pushState);

    history.replaceState = ( f => function replaceState(){
        var ret = f.apply(this, arguments);
        window.dispatchEvent(new Event('replacestate'));
        window.dispatchEvent(new Event('locationchange'));
        return ret;
    })(history.replaceState);

    window.addEventListener('popstate',()=>{
        window.dispatchEvent(new Event('locationchange'))
});
    window.addEventListener('locationchange', function(){
    check_for_clip_in_url()


})
    function check_for_clip_in_url(){
        const url = window.location.href
        if (url.includes("/clip")){
            if (!clips_in_url){
                clips_in_url =1;
                startObserver()
            }

            //console.log('clips ON')
        }
        else{
            if (clips_in_url){
                clips_in_url =0;
                if(observer) {
                    observer.disconnect()
                }
            }

            //console.log('clips OFF')

        }
    }
})();