Grab Lecture in SZU

【使用前先看介绍/有问题可反馈】深圳大学抢领航讲座脚本 (Grab Lecture in SZU):可用于在深圳大学抢领航讲座,目前处于测试阶段。

目前為 2021-04-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Grab Lecture in SZU
// @namespace    http://tampermonkey.net/
// @version      0.7.1
// @description  【使用前先看介绍/有问题可反馈】深圳大学抢领航讲座脚本 (Grab Lecture in SZU):可用于在深圳大学抢领航讲座,目前处于测试阶段。
// @author       cc
// @match        http://lecture.szu.edu.cn/*
// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @require      https://greasyfork.org/scripts/422854-bubble-message.js
// @noframes
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
  'use strict'
  let config = null
  let bm = new BubbleMessage()
  bm.config.cmap.info = '#009688'
  bm.config.width = 300
  function getStartTime () {
    let now = new Date()
    let year = now.getFullYear()
    let month = now.getMonth() + 1
    let day = now.getDate()
    let startTime = new Date(`${year}-${month}-${day} 12:53:00`)
    return startTime.getTime()
  }
  function rushLecture () {
    $.ajax({
      url: 'http://lecture.szu.edu.cn/tLectureSignUp/list?page=1&limit=10',
      type: 'GET',
    }).then(res => {
      if (res.code === 0) {
        let availableLectures = res.data.filter(lecture => Date.now() < new Date(lecture.lectureEndTime))
        Promise.all(availableLectures.map(lecture => {
          return $.ajax({
            url: `http://lecture.szu.edu.cn/lectureClassroomSignUp/list?page=1&limit=20&lectureId=${lecture.id}`,
            type: 'GET',
          }).then(res => {
            return res.code === 0 ? res.data.filter(room => room.remainSeats > 0) : []
          })
        })).then(res => {
          let availableRooms = []
          res.forEach(rooms => availableRooms = availableRooms.concat(rooms))
          availableRooms = availableRooms.filter(room => config.campus.indexOf(room.campus) >= 0)
          if (availableRooms.length === 0) {
            bm.message({
              type: 'warning',
              message: '无可抢讲座',
              duration: 1500,
            })
          } else {
            Promise.all(availableRooms.map(room => {
              return $.ajax({
                url: `http://lecture.szu.edu.cn/tSelectLecture/addItem?lectureClassroomId=${room.id}&lectureId=${room.lectureId}&classroomId=${room.classroomId}`,
                type: 'POST',
              }).then(res => {
                return res
              })
            })).then(res => {
              bm.message({
                type: 'info',
                message: '抢领航讲座已完成,请刷新页面查看结果',
                duration: 3000,
              })
              console.info(res)
            })
          }
        })
      } else {
        bm.message({
          type: 'error',
          message: '请求失败',
          duration: 3000,
        })
      }
    })
  }
  function setSelect () {
    let div = document.createElement('div')
    div.style = 'display: inline-flex; height: 100%; align-items: center; margin-left: 10px;'
    let select = document.createElement('select')
    select.name = 'campus'
    let optionYHCH = document.createElement('option')
    optionYHCH.value = '粤海校区/沧海校区'
    optionYHCH.innerHTML = optionYHCH.value
    let optionLH = document.createElement('option')
    optionLH.value = '丽湖校区'
    optionLH.innerHTML = optionLH.value
    select.appendChild(optionYHCH)
    select.appendChild(optionLH)
    select.addEventListener('change', function (event) {
      config.campusIndex = event.target.selectedIndex
      config.campus = event.target.value.split('/')
      GM_setValue('config', config)
    })
    div.appendChild(select)
    let nextItem = document.querySelector('.layui-nav.layui-layout-right')
    nextItem.parentNode.insertBefore(div, nextItem)
  }
  function loadConfig () {
    let select = document.querySelector('select[name=campus]')
    config = GM_getValue('config')
    if (!config) {
      config = {
        campus: select.options[select.selectedIndex].value.split('/'),
        campusIndex: select.selectedIndex,
      }
      GM_setValue('config', config)
    } else {
      select.selectedIndex = config.campusIndex
    }
  }
  window.onload = function () {
    setSelect()
    loadConfig()
    let currentTime = Date.now()
    let startTime = getStartTime()
    if (currentTime < startTime) {
      let deltaTime = startTime - currentTime
      bm.message({
        type: 'info',
        message: `准备于 ${parseInt(deltaTime / 1000)} 秒后开抢`,
        duration: 3000,
      })
      setTimeout(rushLecture, deltaTime)
    } else {
      bm.message({
        type: 'info',
        message: `已经过了抢课时间`,
        duration: 3000,
      })
    }
  }
})()