philipmwhiteside / Outlook Office365 Calendar Conditional Formatting

// ==UserScript==
// @name         Outlook Office365 Calendar Conditional Formatting
// @version      0.1
// @match        https://outlook.office365.com/owa/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require      https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant        GM_addStyle
// @run-at       document-load
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

var tagColours =
    "/******************************************************/ "+
    "/*                                         MISC       */ "+
    "/******************************************************/ "+
    "div.tag.day-header *     { background-color: #000 !important; color: #fff; } "+
    "div.tag.travel-hotel *   { background-color: #717300 !important; color: #fff; } "+
    "/******************************************************/ "+
    "/*                                         TASKS      */ "+
    "/******************************************************/ "+
    "div.tag div.calendarFree      { border-color: transparent !important; } "+
    "div.tag.todo div.calendarBusy { background-color: black !important;   } "+
    "/******************************************************/ "+
    "div.tag.todo *           { background-color: #008000 !important; color: #fff; } "+
    "div.tag.todo.important * { background-color: #800000 !important; color: #fff; } "+
    "div.tag.completed *      { background-color: #fafafa !important; color: #ccc; } "+
"";

function addTag (task, tags){
    var classes = "tag " + tags;
    task.parentNode.parentNode.parentNode.className += " "+classes;
}

function autoTagMarkup (taskTag) {
    if ( taskTag[0].innerHTML.indexOf("[") == 0
      || taskTag[0].innerHTML.indexOf("{") == 0 ){
        if ( taskTag[0].innerHTML.indexOf("_] ") == 1
          || taskTag[0].innerHTML.indexOf("_} ") == 1 ){
            addTag(taskTag[0], "todo");
        }
        else if ( taskTag[0].innerHTML.indexOf("_]! ") == 1
               || taskTag[0].innerHTML.indexOf("_}! ") == 1 ){
            addTag(taskTag[0], "todo important");
        }
        else if ( taskTag[0].innerHTML.indexOf("X] ") == 1
               || taskTag[0].innerHTML.indexOf("X} ") == 1
               || taskTag[0].innerHTML.indexOf("x} ") == 1
               || taskTag[0].innerHTML.indexOf("x} ") == 1 ){
            addTag(taskTag[0], "completed");
        }
    }
    else if ( taskTag[0].innerHTML.indexOf("!:") == 0 ){
        addTag(taskTag[0], "day-header");
    }
    else if ( taskTag[0].innerHTML.indexOf("Travel: ") == 0
           || taskTag[0].innerHTML.indexOf("Hotel: ") == 0 ){
        addTag(taskTag[0], "travel-hotel");
    }
}

function main(){
    console.log("==============================================================");
    console.log("RUNNING - Outlook.Orifice365.com Script");
    console.log("==============================================================");
    GM_addStyle(tagColours);
    waitForKeyElements ("._wx_5 span", autoTagMarkup);  // Calendar events in all-day view
    waitForKeyElements ("._wx_m1 span", autoTagMarkup); // Calendar events in main view

}

main();
})();