UOOC assistant

UOOC优课联盟,视频自动连播,可选自动二倍速播放(因为超过二倍速可能无法记录任务点),可选是否静音,离开页面能够继续播放,能够自动回答视频中途弹出问题;如果视频一开始处于停止状态,可以手动点击播放;【有问题可以进行反馈,如果觉得不错,可以在反馈留下好评】

Від 15.10.2020. Дивіться остання версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         UOOC assistant
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  UOOC优课联盟,视频自动连播,可选自动二倍速播放(因为超过二倍速可能无法记录任务点),可选是否静音,离开页面能够继续播放,能够自动回答视频中途弹出问题;如果视频一开始处于停止状态,可以手动点击播放;【有问题可以进行反馈,如果觉得不错,可以在反馈留下好评】
// @author       cc
// @include      *
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
    const jsName = 'UOOCassistant.js';
    const host = window.location.host;
    if (host == 'www.uooc.net.cn') {
        console.log(`excute ${jsName}`);
        let recursive = () => {
            let extraTime = 0;
            try {
                let done = false;
                let video = document.querySelector('#player_html5_api');
                if (video) {
                    if (document.getElementById('rate').checked)
                        video.playbackRate = 2;
                    else
                        video.playbackRate = 1;
                    if (document.getElementById('volume').checked)
                        video.muted = true;
                    else
                        video.muted = false;
                    video.autoplay = true;
                    if (video.ended) {
                        done = true;
                    };
                    let quizLayer = document.querySelector('#quizLayer');
                    if (quizLayer && quizLayer.style.display != 'none') {
                        if (done) {
                            setTimeout(() => {
                                document.querySelectorAll('.layui-layer-shade').forEach(e => e.style.display = 'none');
                            }, 1000);
                        };
                        let source = JSON.parse(document.querySelector('div[uooc-video]').getAttribute('source'));
                        let quizList = source.quiz;
                        let quizIndex = 0;
                        let currentTime = video.currentTime;
                        let quizQuestion = document.querySelector('.smallTest-view .ti-q-c').innerHTML;
                        for (let i = 0; i < quizList.length; i++) {
                            if (quizList[i].question == quizQuestion) {
                                quizIndex = i;
                                break;
                            };
                        };
                        let quizAnswer = eval(quizList[quizIndex].answer);
                        let quizOptions = quizLayer.querySelector('div.ti-alist');
                        for (let ans of quizAnswer) {
                            let labelIndex = ans.charCodeAt() - 'A'.charCodeAt();
                            quizOptions.children[labelIndex].click();
                        }; // end for
                        quizLayer.querySelector('button').click();
                        extraTime = 1000;
                    }; // end if
                    if (!done) {
                        if (video.paused) {
                            video.play();
                        } else {
                            document.querySelectorAll('.layui-layer-shade, #quizLayer').forEach(e => e.style.display = 'none');
                        };
                    };
                }; // end if (video)
                if (!done) {
                    console.log('continue recursive function...');
                    setTimeout(recursive, 250 + extraTime);
                } else {
                    console.log('done!');
                    if (video) {
                        let uncomplete_video = document.querySelector(`li[ng-repeat='pointItem in sectionItem.children'] div.uncomplete`);
                        if (!uncomplete_video) {
                            uncomplete_video = document.querySelector(`li[ng-repeat='sectionItem in chapterItem.children'] div.uncomplete`);
                        };
                        if (uncomplete_video) {
                            console.log('found uncomplete video, ready to click...');
                            uncomplete_video.click();
                            setTimeout(() => {
                                uncomplete_video.nextElementSibling.firstElementChild.click();
                                setTimeout(recursive, 1500);
                            }, 500);
                        } else {
                            console.log('not found uncomplete video, end recursive function.');
                        };
                    } else {
                        console.log('video not found');
                    };
                };
            } catch (e) {
                // do nothing
            };
        }; // end recursive
        let wait = () => {
            if (document.readyState == 'complete') {
                console.log('ready to set checkbox.');
                let getCheckbox = (name, text) => {
                    let p = document.createElement('p');
                    p.style.color = '#cccccc';
                    let checkbox = document.createElement('input');
                    checkbox.id = name;
                    checkbox.type = 'checkbox';
                    checkbox.checked = true;
                    checkbox.name = name;
                    checkbox.value = name;
                    checkbox.style.marginLeft = '20px';
                    p.append(checkbox);
                    let label = document.createElement('label');
                    label.setAttribute('for', name);
                    label.innerText = text;
                    label.style.marginLeft = '15px';
                    p.append(label);
                    p.style.margin = '10px';
                    return p;
                };
                let rateCheckbox = getCheckbox('rate', '倍速');
                let volumeCheckbox = getCheckbox('volume', '静音');
                let head = document.querySelector('.learn-head');
                let container = document.createElement('div');
                container.style.display = 'flex';
                container.style.flexDirection = 'row';
                container.append(rateCheckbox);
                container.append(volumeCheckbox);
                head.append(container);
                recursive();
            } else {
                setTimeout(wait, 100);
            };
        }; // end wait
        wait();
    }
})();