RWTH Online Colorcoded Calendar

Fix RWTH Online Calendar Date Coloring

Fra og med 13.03.2019. Se den nyeste version.

// ==UserScript==
// @name         RWTH Online Colorcoded Calendar
// @namespace    https://github.com/Seneral/
// @version      0.1.1
// @description  Fix RWTH Online Calendar Date Coloring
// @author       Me
// @match        https://online.rwth-aachen.de/RWTHonline/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // "col" : color applied to matching events
    // "opc" : opacity of WHOLE event
    // "in" : included in description
    // "ex" : excluded from description
    // "name" : included in name
    // All colors from the settings are valid - red, blue, green, yellow, aquamarine, etc.

    var colorMap = [
        { "opc": 1.0, "col" : "red",        "in" : "Abhaltung; Vorlesung;" },  // ALL Lectures
        { "opc": 1.0, "col" : "yellow",     "in" : "Abhaltung; Übung;", "ex" : "Gruppe" }, // All Global
        { "opc": 1.0, "col" : "yellow",     "in" : "Abhaltung; Tutorium;", "name" : "Lineare Algebra" }, // Global LA
        { "opc": 0.8, "col" : "gray",       "in" : "Abhaltung; Übung;", "name" : "Lineare Algebra" }, // Tut LA
        { "opc": 0.8, "col" : "aquamarine", "in" : "Abhaltung; Tutorium;", "name" : "Formale Systeme" }, // Tut FSAP
        { "opc": 0.8, "col" : "orchid",     "in" : "Abhaltung; Tutorium;", "name" : "Betriebssysteme" }, // Tut BS
        { "opc": 0.8, "col" : "wheat",      "in" : "Abhaltung; Tutorium;", "name" : "Datenstrukturen" } // Tut DA
    ]

    function handleEventColor(event){
        var colorBar = event.children[0];
        var name = event.children[1].children[0].children[2].innerText;
        var desc = event.children[1].children[1].children[0].innerText;
        //var type = desc.match (/Abhaltung; ([a-zA-z]+);/)[1];

        var color;
        var opacity = 1;
        for (var i = 0; i < colorMap.length; i++)
        {
            var rules = colorMap[i];
            var inc  = !rules.in   ||  desc.includes (rules.in);
            var exc  = !rules.ex   || !desc.includes (rules.ex);
            var nm = !rules.name ||  name.includes (rules.name);
            if (inc && exc && nm)
            {
                color = rules.col;
                opacity = rules.opc;
                break;
            }
        }
        /*switch (type)
        {
            case "Vorlesung":
                color = eventLectureColor;
                break;
            case "Übung":
                color = eventExcerciseColor;
                break;
            case "Tutorium":
                color = eventTutoriumColor;
                break;
            case undefined:
                console.log(" --- Event not an Abhaltung! --- ");
                return;
            default:
                console.log(" --- Unknown type of event: '" + type + "' --- ");
                return;
        }*/

        console.log(" --- Event of color " + color + " --- ");

        event.style.opacity = opacity;

        if (!color) return;

        colorBar.className = colorBar.className.replace(/\b(cocal\-bgk\-ev\-status\-)([a-zA-Z-]+)\b/g, "") + "cocal-bgk-ev-status-" + color;
    }

    function updateCalendar(){
        if(!window.location.href.includes ("wbKalender.wbPerson")) return;
        if (window.top == window.self) return; // Only run in the calender frame

        var events = Array.prototype.slice.call(document.querySelectorAll("[id^='idTermin_']"));
        for(var i=0 ; i < events.length ; i++)
        {
            handleEventColor(events[i]);
        }

        console.log(" --- Found " + events.length + " events! --- ");
    }

    // Execute every seconds in case new content has been added to the page
    setInterval(updateCalendar, 1000);
})();