超星后台刷课

请手动打开网课视频页面,然后会自动播放视频,自动尝试回答视频中的问题,自动检测当前视频是否已看完,如果已看完则自动跳转到下一节。有阅读课外阅读时长要求的,请手动打开课外阅读页面,自动滚动页面,自动翻页。

As of 2019-04-16. See the latest version.

// ==UserScript==
// @name         超星后台刷课
// @namespace    http://i.mooc.chaoxing.com/
// @version      0.1
// @description  请手动打开网课视频页面,然后会自动播放视频,自动尝试回答视频中的问题,自动检测当前视频是否已看完,如果已看完则自动跳转到下一节。有阅读课外阅读时长要求的,请手动打开课外阅读页面,自动滚动页面,自动翻页。
// @author       Ganlv
// @match        https://mooc1-2.chaoxing.com/*
// @icon         https://www.chaoxing.com/favicon.ico
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    if (location.href.startsWith('https://mooc1-2.chaoxing.com/mycourse/studentstudy')) {
        // 接收子页面传来的“下一节”消息
        window.addEventListener("message", function (event) {
            if (event.origin !== "https://mooc1-2.chaoxing.com") {
                return;
            }
            if (event.data == "next_chapter") {
                var as = document.querySelectorAll('.ncells a');
                var a = document.querySelector('.ncells a h4.currents').parentElement;
                var next = 0;
                as.forEach(function(item, key) {
                    if (item === a) {
                        next = key + 1;
                    }
                });
                if (next <= as.length - 1) {
                    as[next].querySelector('h4').click();
                }
            }
        }, false);

        // 自动切换到“视频”标签页
        setInterval(function () {
            var tab = document.querySelector('span[title="视频"]');
            if (tab) {
                if (tab.className.indexOf('current') < 0) {
                    tab.click();
                }
            } else {
                window.postMessage("next_chapter", "https://mooc1-2.chaoxing.com");
            }
        }, 2000);

    } else if (location.href.startsWith('https://mooc1-2.chaoxing.com/knowledge/cards')) {
        // 已完成则跳过
        var timerFinished = setInterval(function () {
            var finished = document.querySelector('.ans-job-finished');
            if (finished) {
                window.top.postMessage("next_chapter", "https://mooc1-2.chaoxing.com");
                clearInterval(timerFinished);
            }
        }, 2000);

    } else if (location.href.startsWith('https://mooc1-2.chaoxing.com/ananas/modules/video/index.html')) {
        // 屏蔽“答案错误”提示框
        window.alert = console.log;

        // 视频暂停了则自动继续
        setInterval(function () {
            var video = document.querySelector('video');
            if (video) {
                if (video.paused) {
                    var poster = document.querySelector('.vjs-poster');
                    poster.click();
                }
                if (video.duration - video.currentTime < 10) {
                    setTimeout(function () {
                        window.top.postMessage("next_chapter", "https://mooc1-2.chaoxing.com");
                    }, 10000);
                }
            }

            // 自动完成视频中的题目
            var quizLabels = document.querySelectorAll('.ans-videoquiz-opt label');
            if (quizLabels.length > 0) {
                for (var i = 0; i < quizLabels.length; i++) {
                    !function (i) {
                        setTimeout(function () {
                            var quizLabels = document.querySelectorAll('.ans-videoquiz-opt label');
                            if (quizLabels.length > 0) {
                                quizLabels[i].click();
                                var submit = document.querySelector('.ans-videoquiz-submit');
                                submit.click();
                            }
                        }, i * 1000);
                    }(i);
                }
            }

            // 自动切换视频源
            var videoSource = document.querySelector('.vjs-error-display:not(.vjs-hidden) .ans-vjserrdisplay-opt input[type="radio"]:not([disabled])');
            if (videoSource) {
                videoSource.click();
            }
        }, 1000);
    } else if (location.href.startsWith('https://mooc1-2.chaoxing.com/ztnodedetailcontroller/visitnodedetail')) {
        // 寻找下一页按钮
        var nextPage = document.querySelector('.i.i_c.i_35');
        setInterval(function () {
            // 判断是否看到下一页按钮
            if (nextPage.getBoundingClientRect().y - window.innerHeight <= 0) {
                nextPage.click();
            } else {
                // 如果没看到下一页按钮就慢慢向下滚动屏幕
                document.documentElement.scrollTop += 2;
            }
        }, 300);
    }
})();