Rush lecture

【深圳大学2020级新生专用/使用前请看使用说明】可用于抢领航讲座,由于无法进行测试,大家请抱着试用的心态使用该脚本。

当前为 2020-12-02 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Rush lecture
// @name:en      Rush lecture
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  【深圳大学2020级新生专用/使用前请看使用说明】可用于抢领航讲座,由于无法进行测试,大家请抱着试用的心态使用该脚本。
// @description:en 【深圳大学2020级新生专用/使用前请看使用说明】可用于抢领航讲座,由于无法进行测试,大家请抱着试用的心态使用该脚本。
// @author       cc
// @match        http://lecture.szu.edu.cn/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const IS_LIHU_AREA = false;
    location.area = ['粤海校区', '沧海校区'];
    function sendRequest (url, params='', mode='GET') {
        let request = new XMLHttpRequest();
        request.open(mode, url, true);
        request.setRequestHeader('Content-Type', 'application/json');
        request.send(params);
        request.onreadystatechange = function () {
            if (request.readyState == 4 && request.status == 200) {
                location.ans = request.responseText;
            };
        };
    };
    function getStartTime() {
        let currentTime = new Date();
        let year = currentTime.getFullYear();
        let month = currentTime.getMonth() + 1;
        let day = currentTime.getDate();
        let startTime = new Date(`${year}-${month}-${day} 12:30:00`);
        return startTime.getTime();
    };
    function searchCourse() {
        location.ans = undefined;
        let listUrl = 'http://lecture.szu.edu.cn/tLectureSignUp/list?page=1&limit=20';
        sendRequest(listUrl);
        (function waitListResponse() {
            if (location.ans) {
                let firstItem = JSON.parse(location.ans)['data'][0];
                location.ans = undefined;
                if (firstItem.status == '正在报名中') {
                    let lectureId = firstItem.id;
                    let signUpUrl = `http://lecture.szu.edu.cn/lectureClassroomSignUp/list?page=1&limit=20&lectureId=${lectureId}`;
                    sendRequest(signUpUrl);
                    (function waitSignUpResponse() {
                        if (location.ans) {
                            let reservedSeats = JSON.parse(location.ans)['data'];
                            location.ans = undefined;
                            let availableReservedSeats = [];
                            for (let reservedSeat of reservedSeats) {
                                if (location.area.includes(reservedSeat.campus)) {
                                    availableReservedSeats.push(reservedSeat);
                                };
                            };
                            if (availableReservedSeats.length > 0) {
                                let iframe = window.frames[1].document;
                                let course = iframe.querySelector('.layui-table-box .layui-table-main table tr:first-child td:last-child a');
                                if (course) {
                                    course.click();
                                    (function waitFrame() {
                                        if (window.frames.length > 2) {
                                            let innerIframe = window.frames[2].document;
                                            let seats = innerIframe.querySelectorAll('.layui-table-box .layui-table-main table tr');
                                            if (seats && seats.length > 0) {
                                                let availableSeats =  [];
                                                for (let seat of seats) {
                                                    let a = seat.querySelector('td:last-child a');
                                                    if (!a.classList.contains('layui-btn-disabled')) {
                                                        availableSeats.push(a);
                                                    };
                                                };
                                                if (availableSeats.length > 0) {
                                                    let a = availableSeats[0];
                                                    a.click();
                                                    (function waitClick() {
                                                        if (a.innerHTML == '已报名') {
                                                            console.log('已经抢到领航讲座 (code: 5)');
                                                        } else {
                                                            setTimeout(waitClick, 50);
                                                        };
                                                    })();
                                                } else {
                                                    console.log('已经没有可抢位置 (code: 4)');
                                                };
                                            } else {
                                                setTimeout(waitFrame, 50);
                                            };
                                        } else {
                                            setTimeout(waitFrame, 50);
                                        };
                                    })();
                                } else {
                                    console.log('找不到课程列表,请重试 (code: 3)');
                                };
                            } else {
                                console.log('所在校区没有位置 (code: 2)');
                            };
                        } else {
                            setTimeout(waitSignUpResponse, 50);
                        };
                    })();
                } else {
                    console.log('暂无可报名的领航讲座 (code: 1)');
                };
            } else {
                setTimeout(waitListResponse, 50);
            };
        })();
    };
    function exec() {
        if (IS_LIHU_AREA) {
            location.area = ['丽湖校区'];
        };
        let startTime = getStartTime();
        let currentTime = new Date().getTime();
        if (currentTime < startTime) {
            let deltaTime = startTime - currentTime;
            setTimeout(() => {
                location.reload();
            }, deltaTime);
        } else {
            searchCourse();
        };
    };
    exec();
})();