Asana: days to the due date

You have x days before the due date

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Asana: days to the due date
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  You have x days before the due date
// @author       Tosho Hirasawa
// @match        https://app.asana.com/0/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var str2date = (s) => {
        var days = ['Monday', 'Tuesday', 'Wednesday', 'Thurseday', 'Friday', 'Saturday', 'Sunday'];
        if (s == 'Today') { return new Date(); }
        if (s == 'Tomorrow') { return new Date((new Date()).getTime() + 24*60*60*1000); }
        if (days.indexOf(s) > -1) {
            var date = new Date();
            for (var i = 1; i < 7; i++) {
                date = new Date((new Date()).getTime() + i*24*60*60*1000);
                if (date.getDayName() == s) {
                    return date;
                }
            }
        }
        return new Date(s.contains(',') ? s : s + ',' + (new Date()).getFullYear());
    };

    // Your code here...
    const mobs = new MutationObserver((mutations) => {
        mutations.forEach((m) => {
            var duedate = m.target.querySelector('div.DueDate');
            if (duedate && !duedate.innerText.contains('(')) {
                var date_today = new Date();
                var date_due = str2date(duedate.innerText);
                var days_left = (date_due - date_today) / (1000 * 60 * 60 * 24);
                if (days_left > 0) {
                    duedate.innerText = duedate.innerText + ' (' + Math.ceil(days_left) + ' days)';
                }
            }
        });
    });

    mobs.observe(document.body, {
        subtree: true,
        childList: true,
    });
})();