NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name View # of requests
// @version 1.03
// @description Shows a (cached) number of current requests
// @match http://www.tvmaze.com/*
// @match https://www.tvmaze.com/*
// @grant GM_getValue
// @grant GM_setValue
// @updateURL https://openuserjs.org/meta/gazza911/View_of_requests.meta.js
// @license MIT
// ==/UserScript==
var expMinutes = 30; // Number of minutes before cache expirery (allowing show request count to be updated)
var cached = GM_getValue("tvmaze_requests");
var timestamp = new Date().getTime();
function addToMenu(time, count) {
$("<li class='menuitem'><a class='reqs' href='/request'><span class='underline'>Requests</span><span class='expirery' title='Expires at " + time + "'>" + count + "</span></a></li>").appendTo("#header-menu ul.menu");
}
if (window.location.pathname == "/request" || window.location.pathname == "/request/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 request count to " + count);
GM_setValue("tvmaze_requests", cached);
}
else if (!cached || (cached != {} && timestamp > cached.expires))
{
$.get("/request", 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 request count is " + value);
GM_setValue("tvmaze_requests", 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 + " requests; expires in " + (expirery / 60) + " minutes (" + expirery + " seconds)");
addToMenu(time, cached.count);
}
$(`<style>
.reqs: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;
}
</style>`).appendTo('head');
// First two styles ensure that only the 'Requests' text and not the value is underlined