Add Manaba to Syllabus Link

Turn manaba course code into KdB link

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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!");
        }
    });
}