Greasy Fork

Greasy Fork is available in English.

英华网课课助手 Plus

自动连播网课

当前为 2022-02-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         英华网课课助手 Plus
// @description  自动连播网课
// @author       one-ccs
// @namespace    one-ccs.TM
// @homepageURL  http://greasyfork.icu/zh-CN/users/782903
// @version      1.0.2
// @license      GNU GPL v2.0
// @icon         data:image/gif;base64,R0lGODlhQABAALMAAAAAABCWm/vaCPoFE3t+fu3t7AOi6+C8rXV0dJHR7leHifRPVpCQkUa48PTiUQAAACH5BAkAAA8ALAAAAABAAEAAAAT+8MlJq7046827/2B4LWJpVsNwrmVKsjCXpnGNLTNt71Ku8zXfDBgUqogs4xBpwikHL2bouZR+qLOoVYb9bTXd3FfjzC7OZ984MzscLoVDeW1JvTkFHL2yKIDkexJ3IXmBJ36GiYqLjB4FiBaIkIaPD4+TE5WVjH6YkZ9rl5uRohWDQAYYnRqSFAcORA2ppJ6Zlp4FAkQGsxSatRKXFwKwO7y9oBnAArrGvMoby8Q2x8i2wLbDzDWyxwk7DszbMNUGDeDizSvlzzbp4yfs5jXh78Ul8u0w7+Im3fLnWPATdw9EvmMBTdQbqO7DwWoJQyxkWLDDQ4giJjJsyOFiuYiSHDRu5Jjhn0eEIUcOjNGgW8uXDb5dEKmyXyMpASTkxLDTws6fPR/81GkigNGjSCcMJUohaNCmQp+KWNoU6VGlVpNGzWqUhVSrOntKZZozq9CiZqFSxaoVqtIVS8Vy7epzbNoSc51GPXuBLl+3Mfz2/dpVMFuwJwpXyGtXw9gQgq8udmx4K1e8NzNr3sy5s+cMEQAAOw==
// @match        *://*.yinghuaonline.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==


class YHAssistant {
    /**
     * 类名: YHAssistant
     * 说明: 主类, 用于存放程序所有数据.
     **/
    constructor() {
        this.HTML = null;
        this.setting = {};
        this.regexs = {
            courseId: /(?<=courseId=)\d+/,
            nodeId: /(?<=nodeId=)\d+/
        }
        this.pathnames = [
            '/user/node'
        ];
        this.beepURLs = [
            'https://cdn2.ear0.com:3321/preview?soundid=34591&type=mp3',
            'https://downsc.chinaz.net/Files/DownLoad/sound1/202106/14428.mp3',
            'https://downsc.chinaz.net/Files/DownLoad/sound1/202103/14039.mp3'
        ];
        this.finishBeep = 'https://ppt-mp3cdn.hrxz.com/d/file/filemp3/hrxz.com-bjxdrlfq5o143304.mp3';
        this.videoData = { // 本地视频总时长, 已提交时长, 本次播放有效时长
            totalTime: 0,
            submitTime: 0,
            validTime: 0
        };
        this.courseData = {
            page: 0,
            pageCount: 0,
            index: 0,
            recordsCount: 0,
            nextURL: ''
        };
        this.timer = {
            mainTimer: {
                id: null,
                value: 0
            }
        };
        this._elTree = {};
        this._locker = {
            refresh: {
                value: false,
                timeout: 3000
            },
            button: {
                value: false,
                timeout: 300
            },
            beep: {
                value: false,
                timeout: 1000
            },
            request: {
                value: false,
                timeout:500
            }
        };
        this._elBeep = document.createElement('audio');
        this._finished = false;

        this._init();
    }

    _init() {
        Function.prototype.getMultiLine = function() {
            var lines = new String(this);
            lines = lines.substring(lines.indexOf("/*") + 3, lines.lastIndexOf("*/"));
            return lines;
        }

        function divText() {
            /*
            <div data-yha="title">英华网课助手 Plus <span>控制台</span></div>
            <div data-yha="info"><span>作者: <a href="http://greasyfork.icu/zh-CN/users/782903-little3022">little3022</a></span><span>版本: 1.0.0</span></div>
            <div data-yha="toolbar">
                <span data-yha="icon">
                    <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M939.880137 299.43679 83.552951 299.43679c-16.57449 0-30.011524-13.101389-30.011524-29.67588s13.437034-29.67588 30.011524-29.67588L939.880137 240.08503c16.57449 0 30.011524 13.101389 30.011524 29.67588S956.454628 299.43679 939.880137 299.43679z"></path><path d="M785.821389 546.053584 83.552951 546.053584c-16.57449 0-30.011524-13.613042-30.011524-30.187533s13.437034-30.187533 30.011524-30.187533L785.821389 485.678518c16.57449 0 30.011524 13.613042 30.011524 30.187533S802.39588 546.053584 785.821389 546.053584z"></path><path d="M939.880137 791.647071 83.552951 791.647071c-16.57449 0-30.011524-13.101389-30.011524-29.67588s13.437034-29.67588 30.011524-29.67588L939.880137 732.295312c16.57449 0 30.011524 13.101389 30.011524 29.67588S956.454628 791.647071 939.880137 791.647071z"></path></svg>
                </span>
                <span data-yha="label-info">课程信息</span>
                <span data-yha="label-set" style="display: none;">设置</span>
                <span data-yha="button" yha-action="back" yha-tooltip="返回" style="display: none;">
                    <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M947.4 864C893.2 697.7 736.2 578.9 551 575.5c-23.1-0.4-44.9 0.1-65.6 1.5v164.3c0.1 0.5 0.2 1 0.2 1.5 0 4-3.3 7.3-7.3 7.3-2.7 0-5-1.4-6.2-3.5v0.7L68.8 465.4h2.1c-4 0-7.3-3.3-7.3-7.3 0-2.9 1.7-5.4 4.1-6.6L472 169v0.7c1.3-2.1 3.6-3.5 6.2-3.5 4 0 7.3 3.3 7.3 7.3 0 0.5-0.1 1-0.2 1.5v159.4c18.5-0.9 37.9-1.2 58.3-0.8 230.1 3.9 416.7 196.9 416.7 427.1 0.1 35.5-4.5 70.2-12.9 103.3z m-462-704.4v0.2h-0.4l0.4-0.2z m0 596.9l-0.3-0.2h0.3v0.2z"></path></svg>
                </span>
                <span data-yha="button" yha-action="reset" yha-tooltip="重置" style="display: none;">
                    <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M495.7 313.1c-16.6 0-30 13.4-30 30v182.6c0 3.3 0.5 6.4 1.5 9.3 2.1 14.5 14.6 25.7 29.7 25.7h182.6c16.6 0 30-13.4 30-30s-13.4-30-30-30H525.7V343.1c0-16.6-13.4-30-30-30zM857.3 366.1c-18.9-44.6-45.9-84.7-80.3-119.1-34.4-34.4-74.5-61.4-119.1-80.3-46.2-19.5-95.3-29.5-145.9-29.5-44.8 0-88.7 7.8-130.3 23.3-40.3 14.9-77.4 36.6-110.4 64.3-47.2 39.6-83.8 90.2-106.6 146.6l-16.1-30c-7.8-14.6-26-20.1-40.6-12.2-14.6 7.8-20.1 26-12.2 40.6l51.1 95.1c0.3 0.6 0.7 1.2 1.1 1.8 0.2 0.4 0.5 0.7 0.7 1.1 0.1 0.2 0.3 0.4 0.4 0.7 5.8 7.9 14.8 12.2 24.2 12.2 4.8 0 9.7-1.2 14.2-3.6 0 0 0.1 0 0.1-0.1l95-51c14.6-7.8 20.1-26 12.2-40.6-7.8-14.6-26-20.1-40.6-12.2l-32.5 17.5c19.3-46.1 49.5-87.4 88.3-120 27.7-23.3 58.9-41.4 92.7-54 35-13 71.8-19.5 109.5-19.5 84.1 0 163.1 32.7 222.5 92.2s92.2 138.5 92.2 222.5-32.9 163.2-92.4 222.6-138.4 92.2-222.5 92.2-163.1-32.7-222.5-92.2c-11.7-11.7-30.7-11.7-42.4 0s-11.7 30.7 0 42.4c34.4 34.4 74.5 61.4 119.1 80.3 46.2 19.5 95.3 29.5 145.9 29.5s99.7-9.9 145.9-29.5c44.6-18.9 84.7-45.9 119.1-80.3 34.4-34.4 61.4-74.5 80.3-119.1 19.5-46.2 29.5-95.3 29.5-145.9s-10.1-99.5-29.6-145.8z"></path></svg>
                </span>
                <span data-yha="button" yha-action="setting" yha-tooltip="设置">
                    <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M381.482667 673.877333a90.389333 90.389333 0 0 1 85.226666 60.245334H853.333333v64H465.28a90.389333 90.389333 0 0 1-167.573333 0H170.666667v-64h125.610666a90.389333 90.389333 0 0 1 85.205334-60.245334z m0 64a26.346667 26.346667 0 1 0 0 52.693334 26.346667 26.346667 0 0 0 0-52.693334z m261.034666-304.938666a90.389333 90.389333 0 0 1 85.205334 60.245333H853.333333v64h-127.04a90.389333 90.389333 0 0 1-167.573333 0H170.666667v-64h386.624a90.389333 90.389333 0 0 1 85.226666-60.245333z m0 64a26.346667 26.346667 0 1 0 0 52.693333 26.346667 26.346667 0 0 0 0-52.693333zM381.482667 192a90.389333 90.389333 0 0 1 85.226666 60.224H853.333333v64H465.28a90.389333 90.389333 0 0 1-167.573333 0H170.666667v-64h125.610666A90.389333 90.389333 0 0 1 381.482667 192z m0 64a26.346667 26.346667 0 1 0 0 52.693333 26.346667 26.346667 0 0 0 0-52.693333z"></path></svg>
                </span>
                <span data-yha="button" yha-action="refresh" yha-tooltip="刷新" style="fill: #666;">
                    <svg viewBox="0 0 1025 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M1017.856 460.8l-185.408-233.536c-20.48-25.6-58.368-25.6-78.848 0L569.216 460.8C554.88 478.208 568.256 504.832 590.784 504.832l72.704 0c-2.048 136.192-2.048 309.312-240.704 447.552-6.144 4.096-3.072 13.312 4.096 12.288 456.768-70.656 493.632-376.896 494.656-458.816l75.776 0C1019.904 504.832 1032.192 478.208 1017.856 460.8L1017.856 460.8zM434.048 519.168 361.344 519.168c2.048-136.192 2.048-309.312 240.704-447.552 6.144-4.096 3.072-13.312-4.096-12.288C141.12 129.984 104.256 437.248 103.232 518.144L27.456 518.144c-22.528 0-35.84 26.624-21.504 44.032l185.344 233.536c20.48 25.6 58.368 25.6 78.848 0l185.408-233.536C468.864 545.792 456.576 519.168 434.048 519.168L434.048 519.168zM434.048 519.168"></path></svg>
                </span>
            </div>
            <div data-yha="container">
                <span data-yha="progress">
                    <table data-yha="table" cellspacing="0">
                        <caption>null</caption>
                        <tbody> <tr> <th width="99px">观看次数</th> <td>null</td> </tr> <tr> <th>视频时长</th> <td>null</td> </tr> <tr> <th>剩余时长</th> <td>null</td> </tr> <tr> <th>状态</th> <td>null</td> </tr> <tr> <th>计时</th> <td>null</td> </tr> <tr> <th>预计完成</th> <td>null</td> </tr> </tr> <tr> <th>进度</th> <td>null</td> </tr> </tbody>
                    </table>
                </span>
                <span data-yha="setting">
                    <p data-yha="text" style="margin: 5px 0 5px;width: 75%;">视频设置</p>
                    <div data-yha="setting-item">
                        <span data-yha="setting-key">音量</span>
                        <span yha-setting="video-muting" class="yha-icon-muting"></span>
                        <span data-yha="setting-value">
                            <input yha-setting="video-volume" type="range"  min="0" max="100" step="1">
                        </span>
                    </div>
                    <div data-yha="setting-item">
                        <label data-yha="setting-key" for="c1">自动播放</label>
                        <span data-yha="setting-value">
                            <input id="c1" yha-setting="video-autoplay" type="checkbox">
                        </span>
                    </div>
                    <p data-yha="text">音效设置</p>
                    <div data-yha="setting-item">
                        <span data-yha="setting-key">音量</span>
                        <span yha-setting="beep-muting" class="yha-icon-muting"></span>
                        <span data-yha="setting-value">
                            <input yha-setting="beep-volume" type="range"  min="0" max="100" step="1">
                        </span>
                    </div>
                    <div data-yha="setting-item">
                        <span data-yha="setting-value">
                            <label data-yha="setting-key" for="a0">音效1
                                <input id="a0" yha-setting="beep-beep1" type="radio" name="acoustics" value="0">
                            </label>
                        </span>
                        <span data-yha="setting-value">
                            <label data-yha="setting-key" for="a1">音效2
                                <input id="a1" yha-setting="beep-beep2" type="radio" name="acoustics" value="1">
                            </label>
                        </span>
                        <span data-yha="setting-value">
                            <label data-yha="setting-key" for="a2">音效3
                                <input id="a2" yha-setting="beep-beep3" type="radio" name="acoustics" value="2">
                            </label>
                        </span>
                        <span data-yha="setting-value">
                            <label data-yha="setting-key" for="a3">超链接
                                <input id="a3" yha-setting="beep-beep4" type="radio" name="acoustics" value="3">
                            </label>
                            <input yha-setting="beep-beepURL" type="text" placeholder="仅支持 https 协议" disabled style="margin-top: 8px;">
                        </span>
                    </div>
                    <span data-yha="test" yha-action="test">测试</span>
                    <span data-yha="tip">注意: Edge 浏览器“标签睡眠”后程序将暂停运行!!! 播放声音可阻止睡眠.</span>
                </span>
            </div>
            <div data-yha="msgbox"></div>
            */
        }

        function css1() {
            /*
            #YHAssistant {
                --yha-width: 300px;
                --yha-height: 380px;
                --yha-color: gray;
                z-index: 999;
                position: fixed;
                top: calc(50vh - var(--yha-height) / 2);
                left: 8px;
                margin: 0;
                padding: 0;
                border: 0;
                border-radius: 8px;
                box-shadow: 3px 3px 8px #3338;
                width: var(--yha-width);
                max-height: var(--yha-height);
                font-size: 16px;
                color: var(--yha-color);
                background-color: #F5F5DCDD;
                transition: all 0.5s ease;
                overflow: hidden;
                -webkit-user-select: none;
                user-select: none;
            }
            [data-yha="title"] {
                margin: 1em auto;
                font-size: 1em;
                font-weight: bold;
                text-align: center;
            }
            [data-yha="title"] span {
                font-size: 0.6em;
                font-weight: normal;
                vertical-align: super;
            }
            [data-yha="info"] {
                border-bottom: 1px solid var(--yha-color);
                padding-bottom: 8px;
                font-size: 0.6em;
                text-align: center;
            }
            [data-yha="info"] span {
                padding: 0 5px 0 5px;
                border-right: 1px solid var(--yha-color);
            }
            [data-yha="info"] span:last-child {
                padding: 0 0 0 5px;
                border-right: none;
            }
            [data-yha="toolbar"] {
                z-index: 10;
                margin: 8px 12px;
            }
            [data-yha="toolbar"] [data-yha="icon"] {
                float: left;
                margin: 0 8px;
            }
            [data-yha="toolbar"] [data-yha="button"] {
                float: right;
                margin-left: 8px;
            }
            [data-yha="icon"] svg,
            [data-yha="button"] svg {
                width: 20px;
                height: 20px;
                margin: auto;
                pointer-events: none;
            }
            [data-yha="button"] {
                opacity: 0.6;
                height: 30px;
                width: 30px;
                display: block;
                cursor: pointer;
                line-height: 36px;
                text-align: center;
                border-radius: 8px;
                transition: opacity 0.1s ease;
                fill: #333;
            }
            [data-yha="button"]:hover {
                opacity: 1;
                background-color: #3331;
            }
            [data-yha="button"]::after {
                display: none;
                content: attr(yha-tooltip);
                position: relative;
                top: -15px;
                font-size: 0.8em;
            }
            [data-yha="button"]:hover::after {
                display: block;
            }
            @keyframes refreshing{
                from{transform: rotate(0deg);}
                to{transform: rotate(-360deg);}
            }
            [yha-action="refresh"].action svg {
                animation: refreshing 3s linear infinite;
                animation-fill-mode: forwards;
            }
            [data-yha="container"] {
                display: flex;
                flex-direction: row;
                position: relative;
                left: 0;
                width: 200%;
                min-height: 200px;
                transition: left 0.5s ease;
            }
            [data-yha="progress"],
            [data-yha="setting"] {
                padding: 5px 20px 0 20px;
                width: 50%;
            }
            [data-yha="table"] {
                clear: both;
                margin: 0 auto 18px;
                width: 80%;
                line-height: 1.5em;
                text-align: center;
            }
            [data-yha="table"] caption {
                margin: 8px auto;
                width: calc(var(--yha-width) * 0.8);
                font-size: 1.1em;
                font-weight: bold;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            [data-yha="table"] th, td {
                border-bottom: 1px solid #ccc;
            }
            [data-yha="table"] th {
                font-size: 0.9em;
            }
            [data-yha="setting"] {
                font-size: 0.85em;
            }
            [data-yha="setting-value"] input[type="range"] {
                position: relative;
                top: -2px;
                width: calc(100% - 75px);
            }
            [data-yha="setting-value"] input[type="range"]::after {
                content: attr(value);
                float: right;
                width: 0;
            }
            [data-yha="setting-value"] input[type="checkbox"] {
                position: relative;
                top: -1.5px;
                margin: 0 8px 0 0;
            }
            [data-yha="setting-value"] input[type="radio"] {
                position: relative;
                top: 1.5px;
                margin: 0 8px 0 0;
            }
            [data-yha="text"] {
                margin: 5px auto;
                padding-top: 5px;
                border-top: 1px var(--yha-color) dashed;
                font-size: 1em;
                font-weight: bold;
            }
            [data-yha="setting-item"] {
                margin: 5px auto;
            }
            [data-yha="setting-key"] {
                position: relative;
                top: -3px;
            }
            [data-yha="test"] {
                float: right;
                position: relative;
                top: -32px;
                left: -18px;
                display: inline-block;
                border-radius: 8px;
                width: 52px;
                height: 28px;
                color: #fff;
                line-height: 28px;
                text-align: center;
                background-color: #0005;
                box-shadow: 3px 3px 3px #3333;
                cursor: pointer;
            }
            [data-yha="test"]:hover {
                background-color: #0008;
            }
            [data-yha="tip"] {
                display: inline-block;
                position: relative;
                top: -20px;
                line-height: 20px;
                color: red;
                font-weight: bold;
                text-indent: 1.5em;
            }
            [data-yha="msgbox"] {
                position: fixed;
                top: 0;
                left: 0;
                width: 100vw;
                height: 100vh;
                display: flex;
                flex-direction: column;
                justify-content: center;
                pointer-events: none;
            }
            [data-yha="msgbox"] > span {
                z-index: 999;
                margin: 5px auto;
                padding: 8px 20px;
                border: 2px dashed royalblue;
                border-radius: 8px;
                text-align: center;
                color: white;
                transition: opacity 0.8s linear;
            }
            .msgbox-info {
                background-color: #ef950399;
            }
            .msgbox-error {
                background-color: #ff1e1e99;
            }
            .yha-input {
                opacity: 0;
                position: absolute;
                left: 0;
                cursor: pointer;
            }
            .yha-icon-muting {
                display: inline-block;
                width: 18px;
                height: 18px;
                background-image: url(data:image/svg+xml;base64,PHN2ZyBmaWxsPSJncmF5IiB2aWV3Qm94PSIwIDAgMTAyNCAxMDI0IiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxwYXRoIGQ9Ik03NjIuNyA0NjkuN2MwLTkzLjUtNjkuMy0xNzAuMS0xNTkuMi0xODN2NTMuMWM2MC41IDEyLjMgMTA2LjEgNjUuOCAxMDYuMSAxMzBzLTQ1LjYgMTE3LjctMTA2LjEgMTMwdjUzLjFjODkuOS0xMy4yIDE1OS4yLTg5LjggMTU5LjItMTgzLjJ6TTk5LjQgMzYzLjV2MjEyLjJjMCAyOS4zIDIzLjggNTMuMSA1My4xIDUzLjFoNzkuNlYzMTAuNWgtNzkuNmMtMjkuMyAwLTUzLjEgMjMuNy01My4xIDUzeiBtMzcxLjUtMjEyLjJMMjg1LjIgMjc1LjF2Mzg5LjFMNDcwLjkgNzg4YzI5LjMgMCA1My4xLTIzLjggNTMuMS01My4xVjIwNC40Yy0wLjEtMjkuNC0yMy44LTUzLjEtNTMuMS01My4xeiBtMTMyLjYtNTIuN3Y1NC41YzE1NS44IDMwIDI2NS4zIDE1Ny4xIDI2NS4zIDMxNi42IDAgMTU4LjYtMTA2LjEgMjgxLjUtMjY1LjMgMzE2LjZ2NTQuNWMxNzkuOC0yNi4zIDMxOC40LTE4MS42IDMxOC40LTM3MS4xIDAtMTg5LjUtMTM4LjYtMzQ0LjgtMzE4LjQtMzcxLjF6Ij48L3BhdGg+PC9zdmc+);
                cursor: pointer;
            }
            .yha-icon-muting.muted {
                background-image: url(data:image/svg+xml;base64,PHN2ZyBmaWxsPSJncmF5IiB2aWV3Qm94PSIwIDAgMTAyNCAxMDI0IiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxwYXRoIGQ9Ik0xMDAuMSAzOTl2MjIxYzAgMzAuNSAyNC43IDU1LjMgNTUuMyA1NS4zaDgyLjlWMzQzLjdoLTgyLjljLTMwLjUgMC01NS4zIDI0LjgtNTUuMyA1NS4zek04MDMgNTA5LjVsMTE1LjMtMTE1LjNjMTIuNC0xMi40IDEzLjYtMzEuNSAyLjYtNDIuNXMtMzAtOS45LTQyLjUgMi42TDc2My4xIDQ2OS42IDY0Ny45IDM1NC4zYy0xMi40LTEyLjUtMzEuNS0xMy42LTQyLjUtMi42cy05LjkgMzAgMi42IDQyLjVsMTE1LjMgMTE1LjNMNjA4IDYyNC44Yy0xMi40IDEyLjQtMTMuNiAzMS41LTIuNiA0Mi41czMwIDkuOSA0Mi41LTIuNmwxMTUuMy0xMTUuMyAxMTUuMyAxMTUuM2MxMi40IDEyLjQgMzEuNSAxMy42IDQyLjUgMi42czkuOS0zMC0yLjYtNDIuNUw4MDMgNTA5LjV6TTQ4Ni45IDE3OEwyOTMuNSAzMDYuOXY0MDUuMkw0ODYuOSA4NDFjMzAuNSAwIDU1LjMtMjQuNyA1NS4zLTU1LjNWMjMzLjJjLTAuMS0zMC41LTI0LjgtNTUuMi01NS4zLTU1LjJ6Ij48L3BhdGg+PC9zdmc+);
            }
            */
        }

        function css2() {
            /*
            #YHAssistant:hover {
                left: 0 !important;
            }
            */
        }
        this.divText = divText;
        this.css1 = css1;
        this.css2 = css2;
    }

    showGUI() {
        var _el = document.createElement('div');
        var _css = document.createElement('style');

        _el.id = 'YHAssistant';
        _el.innerHTML = this.divText.getMultiLine();
        _css.type = 'text/css';
        _css.innerHTML = this.css1.getMultiLine();

        document.documentElement.insertBefore(_el, document.body);
        document.head.appendChild(_css);

        this.HTML = _el;
        setTimeout(() => {
            _el.style.left = 20 - _el.offsetWidth + 'px';
            _css.innerHTML += this.css2.getMultiLine();
        }, 3000);
    }

    initElementTree() {
        this.HTML = document.getElementById('YHAssistant');
        let table = this.HTML.querySelector('table[data-yha="table"]');

        this._elTree = {
            msgbox: this.HTML.querySelector('[data-yha="msgbox"]'),
            container: this.HTML.querySelector('[data-yha="container"]'),
            labelInfo: this.HTML.querySelector('[data-yha="label-info"]'),
            labelSet: this.HTML.querySelector('[data-yha="label-set"]'),
            btRefresh: this.HTML.querySelector('[yha-action="refresh"]'),
            btSet: this.HTML.querySelector('[yha-action="setting"]'),
            btBack: this.HTML.querySelector('[yha-action="back"]'),
            btReset: this.HTML.querySelector('[yha-action="reset"]'),
            progress: {
                table: {
                    caption: table.caption,
                    cells: [
                        table.rows[0].cells[1],
                        table.rows[1].cells[1],
                        table.rows[2].cells[1],
                        table.rows[3].cells[1],
                        table.rows[4].cells[1],
                        table.rows[5].cells[1],
                        table.rows[6].cells[1]
                    ]
                }
            },
            setting: {
                video: {
                    muting: this.HTML.querySelector('span[yha-setting="video-muting"]'),
                    volume: this.HTML.querySelector('input[yha-setting="video-volume"]'),
                    autoplay: this.HTML.querySelector('input[yha-setting="video-autoplay"]')
                },
                beep:{
                    muting: this.HTML.querySelector('[yha-setting="beep-muting"]'),
                    volume: this.HTML.querySelector('input[yha-setting="beep-volume"]'),
                    beep1: this.HTML.querySelector('input[yha-setting="beep-beep1"]'),
                    beep2: this.HTML.querySelector('input[yha-setting="beep-beep2"]'),
                    beep3: this.HTML.querySelector('input[yha-setting="beep-beep3"]'),
                    beep4: this.HTML.querySelector('input[yha-setting="beep-beep4"]'),
                    beepURL: this.HTML.querySelector('input[yha-setting="beep-beepURL"]'),
                    test: this.HTML.querySelector('[yha-action="test"]')
                }
            }
        }
    }

    timerLock(locker) {
        if(!locker.value) {
            locker.value = true;
            setTimeout(() => {locker.value = false;}, locker.timeout);
            return false;
        }
        return true;
    }

    bindEvent() {
        let elContainer = this._elTree.container;
        let labelInfo = this._elTree.labelInfo;
        let labelSet = this._elTree.labelSet;
        let btRefresh = this._elTree.btRefresh;
        let btSet = this._elTree.btSet;
        let btBack = this._elTree.btBack;
        let btReset = this._elTree.btReset;
        let vMute = this._elTree.setting.video.muting;
        let bMute = this._elTree.setting.beep.muting;
        let vVolume = this._elTree.setting.video.volume;
        let bVolume = this._elTree.setting.beep.volume;
        let vAutoplay = this._elTree.setting.video.autoplay;
        let beep4 = this._elTree.setting.beep.beep4;
        let txtURL = this._elTree.setting.beep.beepURL;
        let btTest = this._elTree.setting.beep.test;
        let radioArr = [
            this._elTree.setting.beep.beep1,
            this._elTree.setting.beep.beep2,
            this._elTree.setting.beep.beep3,
            this._elTree.setting.beep.beep4,
        ];
        let arr = [];

        btRefresh.addEventListener('click', () => {
            if(this.timerLock(this._locker.refresh)) return;
            let tStr = btRefresh.getAttribute('yha-tooltip');
            let tTime = this._locker.refresh.timeout;

            btRefresh.classList.toggle('action');
            this.refreshClick();
            let tTimer = setInterval(() => {
                btRefresh.setAttribute('yha-tooltip', `${(tTime -= 100) / 1000}s`);

                if(tTime <= 0) {
                    btRefresh.setAttribute('yha-tooltip', tStr);
                    btRefresh.classList.toggle('action');
                    tTimer && clearInterval(tTimer);
                }
            }, 100);
        });
        btSet.addEventListener('click', () => {
            if(this.timerLock(this._locker.button)) return;
            elContainer.style.left = '-100%';
            labelInfo.style.display = 'none';
            labelSet.style.display = 'inline-block';
            btRefresh.style.display = 'none';
            btSet.style.display = 'none';
            btBack.style.display = 'block';
            btReset.style.display = 'block';
        });
        btBack.addEventListener('click', () => {
            if(this.timerLock(this._locker.button)) return;
            elContainer.style.left = '0';
            labelInfo.style.display = 'inline-block';
            labelSet.style.display = 'none';
            btRefresh.style.display = 'block';
            btSet.style.display = 'block';
            btBack.style.display = 'none';
            btReset.style.display = 'none';
        });
        btReset.addEventListener('click', () => {
            if(this.timerLock(this._locker.button)) return;

            this.recoverSetting();
        });
        arr = [
            this._elTree.progress.table.cells[1],
            this._elTree.progress.table.cells[2],
            this._elTree.progress.table.cells[4]
        ];
        arr.forEach(item => {
            item.set = function(value) {
                if(value < 0) value = 0;
                item.innerText = value + ' s';
            };
        });
        vMute.addEventListener('click', () => {
            if(this.timerLock(this._locker.button)) return;

            if(!this.setting.video.muted) {
                vMute.classList.add('muted');
                vVolume.value = 0;
            }
            else {
                vMute.classList.remove('muted');
                vVolume.value = this.setting.video.volume;
            }
            this.setting.video.muted = !this.setting.video.muted;

            if(this.elVideo) {
                this.elVideo.muted = this.setting.video.muted;
            }
            this.saveSetting();
        });
        bMute.addEventListener('click', () => {
            if(this.timerLock(this._locker.button)) return;

            if(!this.setting.beep.muted) {
                bMute.classList.add('muted');
                bVolume.value = 0;
            }
            else {
                bMute.classList.remove('muted');
                bVolume.value = this.setting.beep.volume;
            }
            this.setting.beep.muted = !this.setting.beep.muted;
            this.saveSetting();
        });
        vVolume.addEventListener('input', () => {
            vVolume.setAttribute('value', vVolume.value);

            if(vVolume.value == 0) vMute.classList.add('muted');
            vMute.classList.remove('muted');
            if(this.elVideo) {
                this.elVideo.muted = false;
                this.elVideo.volume = vVolume.value / 100;
            }
        });
        bVolume.addEventListener('input', () => {
            bVolume.setAttribute('value', bVolume.value);

            if(bVolume.value == 0) bMute.classList.add('muted');
            bMute.classList.remove('muted');
        });
        vVolume.addEventListener('change', () => {
            if(this.timerLock(this._locker.button)) return;

            if(vVolume.value == 0) {
                vMute.classList.add('muted');
                vVolume.setAttribute('value', this.setting.video.volume);
                this.setting.video.muted = true;
            }
            else {
                vMute.classList.remove('muted');
                this.setting.video.volume = parseInt(vVolume.value);
                this.setting.video.muted = false;
            }
            if(this.elVideo) {
                this.elVideo.muted = this.setting.video.muted;
                this.elVideo.volume = this.setting.video.volume / 100;

                // 同步视频标签操作, 防止音量被重置
            }
            this.saveSetting();
        });
        bVolume.addEventListener('change', () => {
            if(this.timerLock(this._locker.button)) return;

            if(bVolume.value == 0) {
                bMute.classList.add('muted');
                bVolume.setAttribute('value', this.setting.beep.volume);
                this.setting.beep.muted = true;
            }
            else {
                bMute.classList.remove('muted');
                this.setting.beep.volume = parseInt(bVolume.value);
                this.setting.beep.muted = false;
                this.beep();
                this.saveSetting();
            }
        });
        vAutoplay.addEventListener('click', () => {
            if(this.timerLock(this._locker.button)) return;

            if(!this.setting.video.autoplay) {
                vAutoplay.checked = true;
            }
            else {
                vAutoplay.checked = false;
            }
            this.setting.video.autoplay = !this.setting.video.autoplay;
            this.saveSetting();
        });
        arr = [
            this._elTree.setting.beep.beep1,
            this._elTree.setting.beep.beep2,
            this._elTree.setting.beep.beep3
        ]
        arr.forEach(item => {
            item.addEventListener('click', () => {
                if(this.timerLock(this._locker.beep)) {
                    radioArr[this.setting.beep.effect].checked = true;
                    return;
                }

                txtURL.disabled = true;
                this.setting.beep.effect= parseInt(item.value);
                this.beep();
                this.saveSetting();
            });
        });
        beep4.addEventListener('click', () => {
            if(this.timerLock(this._locker.button)) {
                radioArr[this.setting.beep.effect].checked = true;
                return;
            }

            this.setting.beep.effect = 3;
            txtURL.disabled = false;
        });
        txtURL.addEventListener('input', () => {
            if(this.setting.beep.customURL.search(/https:\S+/) != 0) return;

            this.setting.beep.customURL = txtURL.value;
        });
        txtURL.addEventListener('change', () => {
            if(this.timerLock(this._locker.button)) return;
            if(this.setting.beep.customURL.search(/https:\S+/) != 0) return this.showError('#000', '无效的 URL');

            this.saveSetting();
        });
        btTest.addEventListener('click', () => {
            if(this.timerLock(this._locker.beep)) return;
            if(this.setting.beep.effect == 3 && (this.setting.beep.customURL.search(/https:\S+/) != 0)) return this.showError('#001', '无效的 URL');

            this.beep();
        });
    }

    loadSetting() {
        this.setting = GM_getValue('YHAssistant', null);

        if(!this.setting) this.recoverSetting();
        if(this.setting.beep.effect == 3 && (this.setting.beep.customURL.search(/https:\S+/) != 0)) {
            this.setting.beep.effect = 0;
            this.setting.beep.customURL = '';
        }

        this.showSetting();
    }

    saveSetting() {
        if(this.setting.beep.effect == 3 && (this.setting.beep.customURL.search(/https:\S+/) != 0)) {
            this.setting.beep.effect = 0;
            this.setting.beep.customURL = '';
        }

        GM_setValue('YHAssistant', this.setting);
    }

    recoverSetting() {
        /**
         * 函数名: recoverSetting
         * 说明: 重置设置.
         **/
        let tURL = '';

        if(this.setting.beep.customURL && (this.setting.beep.customURL.search(/https:\S+/) === 0)) tURL = this.setting.beep.customURL;
        this.setting = {
            video: {
                muted: false,
                volume: 1,
                autoplay: true
            },
            beep: {
                muted: false,
                volume: 80,
                effect: 0,
                customURL: ''
            },
        };
        this.setting.beep.customURL = tURL;
        this.showSetting();
        this.saveSetting();
    }

    _setRange(el, value) {
        el.value = value;
        el.setAttribute('value', value);
    }

    showSetting() {
        let vMute = this._elTree.setting.video.muting;
        let bMute = this._elTree.setting.beep.muting;
        let vVolume = this._elTree.setting.video.volume;
        let bVolume = this._elTree.setting.beep.volume;
        let vAutoplay = this._elTree.setting.video.autoplay;
        let txtURL = this._elTree.setting.beep.beepURL;

        if(this.setting.video.muted) {
            vMute.classList.add('muted');
            vVolume.setAttribute('value', this.setting.video.volume);
            vVolume.value = 0;
        }
        else {
            this._setRange(vVolume, this.setting.video.volume);
        }
        if(this.setting.video.autoplay) vAutoplay.checked = true;
        if(this.setting.beep.muted) {
            bMute.classList.add('muted');
            vVolume.setAttribute('value', this.setting.beep.volume);
            bVolume.value = 0;
        }
        else {
            this._setRange(bVolume, this.setting.beep.volume);
            if(this.setting.beep.volume < 50) this.showInfo('提示音音量过低', 6000);
        }
        switch(this.setting.beep.effect) {
            case 0:
                this._elTree.setting.beep.beep1.checked = true;
                txtURL.disabled = true;
                break;
            case 1:
                this._elTree.setting.beep.beep2.checked = true;
                txtURL.disabled = true;
                break;
            case 2:
                this._elTree.setting.beep.beep3.checked = true;
                txtURL.disabled = true;
                break;
            case 3:
                this._elTree.setting.beep.beep4.checked = true;
                txtURL.disabled = false;
        }
        txtURL.value = this.setting.beep.customURL;
    }

    beep(src='') {
        if(this.setting.beep.muted || this._finished) return;

        if(src) {
            this._elBeep.src = src;
        }
        else if(this.setting.beep.effect == 3) {
            this._elBeep.src = this.setting.beep.customURL;
        }
        else {
            this._elBeep.src = this.beepURLs[this.setting.beep.effect];
        }
        this._elBeep.volume = this.setting.beep.volume / 100;
        this._elBeep.play();
    }

    loopBeep(times=3, interval=1000) {
        let i = 0;

        if(times <= 1) {
            this.beep();
            return;
        }

        let val = setInterval(() => {
            if(i++ < times) this.beep();
            else val && clearInterval(val);
        }, interval);

        return val;
    }

    beepTip() { // 组合提示音
        this.timer.a = this.loopBeep(3, 2000);
        this.timer.c = setTimeout(() => {
            this.timer.b = this.loopBeep(5, 10000);
        }, 30000);
    }

    clearBeepTip() {
        this.timer.a && clearInterval(this.timer.a);
        this.timer.b && clearInterval(this.timer.b);
        this.timer.c && clearTimeout(this.timer.c);
    }

    showInfo(msg, timeout=3000) {
        let elTip = document.createElement('span');

        elTip.className = 'msgbox-info';
        elTip.innerText = 'Ⓘ ' + msg;
        this._elTree.msgbox.appendChild(elTip);
        setTimeout(() => {
            elTip.style.opacity = 0;
        }, timeout - 800);
        setTimeout(() => {
            elTip.style.display = 'none';
        }, timeout);

        return true;
    }

    showError(id, msg, timeout=5000) {
        let elTip = document.createElement('span');

        elTip.className = 'msgbox-error';
        elTip.innerText = `⚠ 错误 ID: ${id}, 描述: ${msg}.`;
        this._elTree.progress.table.caption.innerText = `⚠ ${msg} ⚠`;
        this._elTree.progress.table.caption.title = `⚠ 错误 ID: ${id}, 描述: ${msg}.`;
        this._elTree.progress.table.caption.style.color = 'red';
        this._elTree.msgbox.appendChild(elTip);
        setTimeout(() => {
            elTip.style.opacity = 0;
        }, timeout - 800);
        setTimeout(() => {
            elTip.style.display = 'none';
        }, timeout);

        return false;
    }

    simulateMouseMove(element) {

    }

    refreshClick() {
        if(!this.courseData) return this.showError('#002', '不支持的页面');

        this.showInfo('正在检查进度...');
        let courseData = this.checkPage();

        if(courseData) {
            this.courseData = courseData;
            this.showCourseData(courseData);
            // 刷新计时器
            if(this.timer.d) {
                clearTimeout(this.timer.d);
                this.timer.d = null;
            }
            let surplusTime = this.videoData.totalTime - this.videoData.submitTime - this.videoData.validTime;
            this.timer.d = setTimeout(() => {
            }, parseInt(surplusTime > 0? surplusTime + 3: 0) * 1000);
        }
        else {
            this.showInfo('刷新失败');
        }
    }

    furureTime(sec=0) {
        /**
         * 返回当前时间(hh:mm:ss)加 sec 后的时间
         */
        let date = new Date();
        let sTime = date.toJSON().match(/\d\d:\d\d:\d\d/)[0];
        let time = this.parseSec(sTime);

        return this.formatSec(time + sec);
    }

    parseSec(sTime="00:00:00") {
        /**
         * 函数名: parseSec()
         * 说明: 把时间格式的字符串转换为秒数.*/
        let sec = 0;
        if(sTime != "" && !isNaN(Date.parse("1970-1-1 " + sTime))) { //判断是否是时间格式
            let t = sTime.split(":");
            sec += parseInt(t[0]) * 3600 + parseInt(t[1]) * 60 + parseInt(t[2]);
        }
        else return -1;

        return sec;
    }

    formatSec(sec=0) {
        /**
         * 函数名: formateSec()
         * 说明: 把秒数转换为时间格式的字符串.*/
        return (new Date(sec * 1000).toTimeString().slice(0, 8));
    }

    getCourseID() {
        /**
         * 爬取网页源码中的课程 ID, 用于请求课程数据
         */
        let courseId = '';

        try {
            courseId = document.querySelector('#wrapper > div.curPlace > div.center > a:last-child').href.match(/(?<=courseId=)\d+/)[0];
        }
        catch(err) {}

        return courseId;
    }

    request(courseId, page=1) {
        /**
         * 描述: 从服务器获取课程数据(JSON)
         * 请求模式: 同步
         */
        let xhttp = new XMLHttpRequest();
        let url = `/user/study_record.json?courseId=${courseId}&page=${page}&_=${(new Date()).valueOf()}`;
        let result = null;

        xhttp.onreadystatechange = () => {
            if(xhttp.readyState == 4 && xhttp.status == 200) {
                result = JSON.parse(xhttp.responseText);
            }
        };
        xhttp.open('get', url, false);
        xhttp.send();

        return result;
    }

    checkPage(page=1, pageCount=1) {
        /**
         * 跳转未完成页面, 返回对应课程数据
         */
        let courseId = this.getCourseID();

        if(!courseId) return this.showError('#003', 'courseId 获取失败');
        if(this.setting.pageData && this.setting.pageData.courseId == courseId) page = this.setting.pageData.page;

        for(; page <= pageCount; page++) {
            this.showInfo(`请求数据, courseId: ${courseId}, page: ${page}`);
            let json = this.request(courseId, page);

            if(json && json.status) {
                this.showInfo('数据请求成功');
                pageCount = json.pageInfo.pageCount;
                for(let courseData of json.list) { // 查找未完成页面
                    if(courseData.state.match(/(未学完|未学)/)) {
                        let currentNodeID = document.location.href.match(this.regexs.nodeId)[0];
                        let newNodeID = courseData.url.match(this.regexs.nodeId)[0];
                        let index = json.list.indexOf(courseData);
                        let nextCourse = json.list[index + 1];

                        if(currentNodeID != newNodeID) { // 本节已完成 跳转未完成页面
                            window.open(courseData.url, '_self');
                            // 立即退出循环, 防止 window.open() 重复请求
                            return this.showInfo('正在跳转新页面...');
                        }
                        // 当前页面为未完成, 添加页面信息方便后续处理
                        courseData.page = json.pageInfo.page;
                        courseData.pageCount = json.pageInfo.pageCount;
                        courseData.index = json.pageInfo.page * 20 - 20 + index;
                        courseData.recordsCount = json.pageInfo.recordsCount;
                        if(nextCourse) courseData.nextURL = nextCourse.url;

                        // 保存页码减少重复请求
                        this.setting.pageData = {
                            courseId: courseId,
                            page: page
                        };
                        this.saveSetting();

                        return courseData;
                    }
                }
            }
            else return this.showError('#004', '数据请求失败');
        }
        // 课程进度 100%
        this._finished = true;
        this.HTML.style.color = 'black';
        this.HTML.style.backgroundColor = 'mediumseagreen';
        this.showInfo('该课程观看进度 100%');
        this._elTree.progress.table.caption.innerText = '(已完成) ' + this._elTree.progress.table.caption.innerText;
        this._elTree.progress.table.caption.title = this._elTree.progress.table.caption.innerText;
        this.beep(this.finishBeep);

        return false;
    }

    showCourseData() {
        this._elTree.progress.table.caption.innerText = this.courseData.name;
        this._elTree.progress.table.caption.title = this.courseData.name;
        this._elTree.progress.table.cells[0].innerText = parseInt(this.courseData.viewCount) + 1;
        this._elTree.progress.table.cells[1].set(this.parseSec(this.courseData.videoDuration));
        this._elTree.progress.table.cells[2].set(this.parseSec(this.courseData.videoDuration) - this.courseData.duration);
        this._elTree.progress.table.cells[3].innerText = this.courseData.state.match(/(未学完|未学|已学)/)[0];
        this._elTree.progress.table.cells[4].set(0);
        this._elTree.progress.table.cells[5].innerText = this.furureTime(this.parseSec(this.courseData.videoDuration) - this.courseData.duration);
        this._elTree.progress.table.cells[6].innerText = `${this.courseData.index}/${this.courseData.recordsCount} (${(this.courseData.index / this.courseData.recordsCount * 100).toFixed(2)}%)`;
    }

    listeningVideo() {
        let elVideo = document.querySelector('video');

        if(!elVideo) return this.showError('#005', '未获取视频对象');
        if(elVideo.src != this.courseData.localFile) return this.showError('#006', '视频 URL 不匹配');
        this.showInfo('开始监听视频对象');

        // 初始化数据
        this.videoData.totalTime = this.parseSec(this.courseData.videoDuration);
        this.videoData.submitTime = parseInt(this.courseData.duration);
        this.timer.d = null; // 超时查询进度

        // 绑定事件
        let pos1 = parseInt(elVideo.currentTime), pos2 = 0;
        elVideo.addEventListener('timeupdate', () => {
            let pos2 = parseInt(elVideo.currentTime);

            if(pos2 >= pos1 + 1) { // 经过 1s
                pos1 = pos2;

                this._elTree.progress.table.cells[2].set(this.videoData.totalTime - this.videoData.submitTime - this.videoData.validTime++);
            }
        });
        elVideo.addEventListener('play', () => {
            if(!this.timer.d) { // 超时查询进度
                let surplusTime = this.videoData.totalTime - this.videoData.submitTime - this.videoData.validTime;
                this.timer.d = setTimeout(() => {
                    this.showInfo('到达设定时间, 正在检查进度...');
                    this.checkPage();
                }, parseInt(surplusTime + 3) * 1000);
                // 刷新预计完成时间
                this._elTree.progress.table.cells[5].innerText = this.furureTime(surplusTime + 3);
            }
            this.clearBeepTip();
        });
        elVideo.addEventListener('pause', () => {
            if(this.timer.d) {
                clearTimeout(this.timer.d);
                this.timer.d = null;
            }
            if(elVideo.ended) this.refreshClick();
            else this.beepTip();
        });
        // 监听鼠标移动事件
        document.body.onmousemove = () => {
            this.clearBeepTip();
        };

        // 应用设置
        elVideo.muted = this.setting.video.muted;
        elVideo.volume = this.setting.video.volume / 100;
        if(this.setting.video.autoplay) elVideo.play();
        elVideo.playbackRate = 1;
        this.elVideo = elVideo;

        this.showCourseData();
    }

    exec() {
        this.showGUI();
        this.initElementTree();
        this.bindEvent();
        this.loadSetting();
        if(this.pathnames.indexOf(document.location.pathname) > -1) {
            let courseData = this.checkPage();

            if(courseData) {
                this.courseData = courseData;
                // 设置计时器
                this.timer.mainTimer.id = setInterval(() => {
                    this._elTree.progress.table.cells[4].set(++this.timer.mainTimer.value);
                }, 1000);
                this.listeningVideo();
            }
        }
        else {
            this.showError('#000', '不支持的页面');
        }
    }
}

(function() {
    'use strict';

    // 伪造 localStorage 视频播放位置缓存数据
    let nodeId = document.querySelector('#video-nodeId').getAttribute('value') || '';
    let userId = document.querySelector('#user-id').getAttribute('value') || '0';
    let schoolId = document.querySelector('#school-id').getAttribute('value') || '0';
    let playId = 'node_' + schoolId + userId + '_' + nodeId;
    window.localStorage.setItem(playId, '0.0');

    window.onload = function() {
        var app = new YHAssistant();

        app.exec();
    };
})();