gazza911 / View # of data tasks

// ==UserScript==
// @name         View # of data tasks
// @version      1.03
// @description  Shows a (cached) number of current data tasks
// @match        http://www.tvmaze.com/*
// @match        https://www.tvmaze.com/*
// @grant        GM_getValue
// @grant        GM_setValue
// @license     MIT
// @updateURL https://openuserjs.org/meta/gazza911/View_of_data_tasks.meta.js
// ==/UserScript==

var expMinutes = 60; // Number of minutes before cache expirery (allowing show data tasks count to be updated)

var cached = GM_getValue("tvmaze_datatasks");
var timestamp = new Date().getTime();

function addToMenu(time, count) {
    $("<li class='menuitem'><a class='dtasks' href='/datatask'><span class='underline'>Data Tasks</span><span class='expirery' title='Expires at " + time + "'>" + count + "</span></a></li>").appendTo("#header-menu ul.menu");
}

if (window.location.pathname == "/datatask" || window.location.pathname == "/datatask/index")
{
    var expires = timestamp + (1000 * 60 * expMinutes); // Set expirery time to value of expMinutes
    var dt = new Date(expires);
    var mins = dt.getMinutes();
    var time = dt.getHours() + ":" + (mins < 10 ? '0' + mins : mins);
    var count = $("div.summary b:last").text().replace(",","") || 0;
    addToMenu(time, count);
    cached = { "count" : count, "expires" : expires };
    console.log("Setting data tasks count to " + count);
    GM_setValue("tvmaze_datatasks", cached);
}
else if (!cached || (cached != {} && timestamp > cached.expires))
{
    $.get("/datatask", function(data)
    {
        var start = data.indexOf("<div class=\"summary\">Showing <b>");
        var value = 0;
        if (start > -1) {
            data = data.substring(start + 32);
			start = data.indexOf("<b>") + 3;
			var end = data.indexOf("</b>", start);
            value = data.substring(start, end).replace(",", "");
        }
        var expires = timestamp + (1000 * 60 * expMinutes); // Set expirery time to value of expMinutes
        var dt = new Date(expires);
        var mins = dt.getMinutes();
        var time = dt.getHours() + ":" + (mins < 10 ? '0' + mins : mins);
        addToMenu(time, value);
        cached = { "count" : value, "expires" : expires };
        console.log("Previous value non-existent or has expired; new data tasks count is " + value);
        GM_setValue("tvmaze_datatasks", cached);
    });
}
else
{
    var expirery = (cached.expires - timestamp) / 1000;
    var dt = new Date(cached.expires);
    var mins = dt.getMinutes();
    var time = dt.getHours() + ":" + (mins < 10 ? '0' + mins : mins);
    console.log("Read from cache: " + cached.count + " data tasks; expires in " + (expirery / 60) + " minutes (" + expirery + " seconds)");
    addToMenu(time, cached.count);
}

$(`<style>
.dtasks:hover
{
text-decoration: none;
}
li:hover .underline
{
    text-decoration: underline;
}
.expirery
{
    margin-left:10px;
    padding: 2px 7px 2px 7px;
    border: 2px solid;
    border-radius: 8px;
    font-style: italic;
}
#site-navigation .menu li a {
    padding: 0.9rem 1.1rem;
}
</style>`).appendTo('head');

// First two styles ensure that only the 'Data Tasks' text and not the value is underlined