BenjaVR / Leho NewFileChecker

// ==UserScript==
// @name            Leho NewFileChecker
// @namespace       https://github.com/BenjaVR
// @version         1
// @description     marks the new files on Leho
// @author          BenjaVR
// @include         *://leho.howest.be*
// @grant           none
// ==/UserScript==

$(document).ready(function () {

    function createHTML() {
        var html = '' +
            '<ul style="float:left;" id="lehoTimeChecker">' +
            '   <li>' +
            '       <a href="#" id="lehoTimeButton">' +
            '           Save Current Time!' +
            '       </a>' +
            '   </li>' +
            '   <li>' +
            '       <div>' +
            '           Last saved date: ' + '<div style="display:inline" id="printedDate">' + getDateObjectInLocalStorage().printableDate + '</div>' +
            '       </div>' +
            '   </li>' +
            '</ul>';
        $('div#usermenu').append(html);
    }

    function updatePrintedDate(){
        $('#printedDate').replaceWith(getDateObjectInLocalStorage().printableDate);
    }

    function checkNewDatesOnThePage() {
        $("td").each(function () {
            var date = new Date($(this).text());
            if (!isNaN(date.getTime())) {
                if (compareWithSavedDate(date)){
                    changeBackgroundColor(this.parentElement, "pink");
                }
            }
        });
    }

    function compareWithSavedDate(date) {
        var storedDefaultDate = new Date(getDateObjectInLocalStorage().defaultDate);
        return storedDefaultDate < date;
    }

    function changeBackgroundColor(element, color) {
        element.style.backgroundColor = color;
    }

    function checkIfLocalstorageExists() {
        if (!localStorage.lehoTimeCheck) {
            var aDate = formatDefaultDate2localStorageDateObject(new Date());
            setDateArrayInLocalStorage(aDate);
        }
    }

    function formatDefaultDate2localStorageDateObject(pageDate) {
        return {defaultDate: new Date(pageDate), printableDate: formatDefaultDate2PrintableDate(new Date(pageDate))};
    }

    function setDateArrayInLocalStorage(dateObject){
        localStorage.lehoTimeCheck = JSON.stringify(dateObject);
    }

    function getDateObjectInLocalStorage(){
        checkIfLocalstorageExists();
        return JSON.parse(localStorage.lehoTimeCheck);
    }

    function addZero(number, totalDigits) {
        number = String(number);
        while (number.length < totalDigits) {
            number = "0" + number;
        }
        return number;
    }

    function formatDefaultDate2PrintableDate(date) {
        var day = addZero(date.getDate(), 2),
            month = addZero(date.getMonth() + 1, 2),
            year = date.getFullYear(),
            hours = addZero(date.getHours(), 2),
            minutes = addZero(date.getMinutes(), 2),
            fullDay = getDayName(date.getDay(), 2);

        return fullDay + " " + day + "/" + month + "/" + year + " " + hours + ":" + minutes;
    }

    function getDayName(daynumber) {
        var fullDay;
        switch (daynumber) {
            case 1:
                fullDay = "Monday";
                break;
            case 2:
                fullDay = "Tuesday";
                break;
            case 3:
                fullDay = "Wednesday";
                break;
            case 4:
                fullDay = "Thursday";
                break;
            case 5:
                fullDay = "Friday";
                break;
            case 6:
                fullDay = "Saturday";
                break;
            case 7:
                fullDay = "Sunday";
                break;
            default:
                fullDay = "";
        }
        return fullDay;
    }

    function updateSavedDate() {
        var nowObject = formatDefaultDate2localStorageDateObject(new Date()),
            conf = confirm("Confirm if you want to override your current saved date:\n" + getDateObjectInLocalStorage().printableDate + "\nto:\n" + nowObject.printableDate);
        if (conf) {
            setDateArrayInLocalStorage(nowObject);
            updatePrintedDate();
            checkNewDatesOnThePage();
        }
    }

    if (document.getElementById("usermenu")) {
        createHTML();
        checkNewDatesOnThePage();
    }

    $("#lehoTimeButton").on("click", function () {
        updateSavedDate();
    });

});