优科学堂& 南昌大学科学技术学院继续教育平台自动刷课时脚本//该脚本能实现自动播放下一个视频

Automatically play videos in order

// ==UserScript==
// @name         优科学堂& 南昌大学科学技术学院继续教育平台自动刷课时脚本//该脚本能实现自动播放下一个视频
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Automatically play videos in order
// @author       You
// @match        https://ndky.youkexuetang.cn/student/#/courseList/courseList/course?id=7860
// @grant        none
// @license      3.0
// ==/UserScript==

(function() {
    'use strict';

    var currentVideoIndex = 0;
    var videos = [];

    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList') {
                videos = document.getElementsByTagName('video');
                if (currentVideoIndex < videos.length) {
                    var video = videos[currentVideoIndex];
                    if (video.readyState === 4) {  // the video is ready
                        video.play();
                        setEndEventListener(video);
                    } else {  // wait until the video is ready
                        video.oncanplaythrough = function() {
                            this.play();
                            setEndEventListener(this);
                        }
                    }
                }
            }
        });
    });

    observer.observe(document, { childList: true, subtree: true });

    function setEndEventListener(video) {
        video.onended = function() {
            currentVideoIndex++;
            if (currentVideoIndex < videos.length) {
                var nextVideo = videos[currentVideoIndex];
                if (nextVideo.readyState === 4) {  // the video is ready
                    nextVideo.play();
                    setEndEventListener(nextVideo);
                } else {  // wait until the video is ready
                    nextVideo.oncanplaythrough = function() {
                        this.play();
                        setEndEventListener(this);
                    }
                }
            }
        }
    }

})();