Add Manaba to Syllabus Link

Turn manaba course code into KdB link

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Add Manaba to Syllabus Link
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Turn manaba course code into KdB link
// @author       eggplants
// @homepage     https://github.com/eggplants
// @match        https://manaba.tsukuba.ac.jp/ct/course*
// @grant        GM.xmlHttpRequest

// ==/UserScript==

const get_nendo = () => {
    let date = new Date();
    date.setMonth(date.getMonth() - 3);
    return date.getFullYear();
}

const get_code = () => {
    let elms = document.getElementsByClassName("coursecode")
    if(elms.length === 0){
        throw 'Error: div.coursecode is missing';
    }
    return elms[0].innerText;
}

const get_kdb_url = () => {
    return `
      https://kdb.tsukuba.ac.jp/syllabi/${
          get_nendo()
      }/${
          get_code()
      }/jpn
    `.trim();
}

const replace_code = () => {
    let elms = document.getElementsByClassName("coursecode")
    if(elms.length === 0){
        throw 'Error: div.coursecode is missing';
    }
    elms[0].innerHTML = `
      <a href=${get_kdb_url()} class="coursecode_link">${get_code()}</a>
    `.trim();
}

const check_kdb_status = (res) => {
    if(res.responseText.includes("シラバスが見つかりません")){
        console.warn(`There is no syllabus page for this code in KdB!: ${get_code()}`);
    } else {
        replace_code();
    }
}

window.onload = () => {
    "use strict";
    console.log(get_kdb_url());
    GM.xmlHttpRequest({
        method: "GET",
        url: get_kdb_url(),
        onload: check_kdb_status,
        timeout: 5000,
        ontimeout: () => {
            console.warn("Commection timeout!");
        }
    });
}