YSU教室课表标色

燕山大学教室课表标色

目前为 2021-04-20 提交的版本。查看 最新版本

// ==UserScript==
// @name         YSU教室课表标色
// @version      1.0.2
// @author       Haomin Kong
// @description  燕山大学教室课表标色
// @match        */zjdxgc/mycjcx/*
// @icon         http://ysu.edu.cn/images/favicon.png
// @require      https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_openInTab
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_notification
// @license      GPL-3.0 License
// @run-at       document-end
// @namespace    https://gitee.com/a645162/ysu-web-browser-scripts
// ==/UserScript==

http: (function () {
  var week = 1;

  function DoJob() {
    let weekText = /本周为第[0-9]+周周[0-9]/.exec(
      document.getElementsByTagName("body")[0].textContent
    );
    weekText = /第[0-9]+周/.exec(weekText);
    week = parseInt(/[0-9]+/.exec(weekText));
    let as = document.getElementsByTagName("table");
    for (var i = 0, j = as.length; i < j; i++) {
      TraverseTable(as[i]);
    }
  }

  function TraverseTable(tableObj) {
    for (var i = 0; i < tableObj.rows.length; i++) {
      for (var j = 0; j < tableObj.rows[i].cells.length; j++) {
        re(tableObj.rows[i].cells[j]);
      }
    }
  }

  function re(td) {
    let text = td.textContent.trim();

    let now = week;
    //now = 10;

    let regExp = /[0-9]+-[0-9]+周/g;
    let result = [];
    let match;

    let state = 0;

    while ((match = regExp.exec(text))) {
      let current = /[0-9]+-[0-9]+/.exec(match[0])[0];

      let l = current.split("-");
      if (l.length == 2) {
        let start = parseInt(l[0]);
        let end = parseInt(l[1]);

        if (start <= now && now <= end) {
          //还在上
          state = 1;
        }

        if (now < start && state != 1) {
          //未开课
          state = 2;
        }
      }

      result.push(current);
    }

    if (state == 1) {
      td.bgColor = "FFFFBB";
    } else if (state == 2) {
      td.bgColor = "FFDDDD";
    } else {
      td.bgColor = "FFFFFF";
      if (result.length != 0) {
        td.textContent = "";
      }
    }
  }

  function addLink() {
    //let as = document.getElementsByTagName("table");
    var btn1 = document.createElement("Button");
    btn1.setAttribute(
      "onclick",
      "window.location.href = 'https://gitee.com/a645162/ysu-web-browser-scripts'"
    );
    btn1.innerText = "更新脚本";

    var lab1 = document.createElement("a");
    lab1.innerText = " 脚本作者:孔昊旻";
    lab1.setAttribute("href", "https://gitee.com/a645162");
    let parent1 = document.getElementsByName("Submit")[0].parentNode;
    parent1.appendChild(btn1);
    parent1.appendChild(lab1);
  }
  //DoJob();
  //addLink();
  window.onload = function () {
    DoJob();
    addLink();
  };
})();