KAT: smart full date/time

Today: "1 hour ago", yesterday: "yesterday, 11:44", earlier: "12 days ago, 01 Oct 2015, 10:45"

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         KAT: smart full date/time
// @description  Today: "1 hour ago", yesterday: "yesterday, 11:44", earlier: "12 days ago, 01 Oct 2015, 10:45"
// @include      https://kat.cr/*
// @version      1.0.3
// @author       wOxxOm
// @namespace    https://greasyfork.org/en/users/2159-woxxom
// @license      MIT License
// @run-at       document-start
// @grant        GM_addStyle
// @require      https://greasyfork.org/scripts/12228/code/setMutationHandler2.js
// ==/UserScript==

var dateLocale = 'en-GB';
//var dateLocale = navigator.language;

GM_addStyle('.wOxxOmified {opacity:0.7}');

var today = new Date();
today.setHours(0, 0, 0, 0);

var yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
yesterday.setHours(0, 0, 0, 0);

var year = new Date();
year.setMonth(0, 1);
year.setHours(0, 0, 0, 0);

var timestamps; // = [].slice.call(document.getElementsByTagName('time'));
setMutationHandler(document, 'time', datify);

function datify(nodes) {
	nodes.forEach(function(n) {
		if (n.wOxxOm == n.textContent)
			return;
		var d = new Date(n.getAttribute('datetime') || n.title);
		if (d >= today) {
			// no change
		} else if (d >= yesterday) {
			setContent(n, 'yesterday, ', d, {hour:'2-digit', minute:'2-digit'});
		} else if (d >= year) {
			setContent(n, '', d, {day:'numeric', month:'short', hour:'2-digit', minute:'2-digit'});
		} else {
			setContent(n, '', d, {day:'numeric', month:'short', year:'2-digit', hour:'2-digit', minute:'2-digit'});
		}
	});
	return true;

	function setContent(n, prefix, d, options) {
		var time = d.toLocaleTimeString(dateLocale, options);
		var text = prefix ? prefix + time : time + ', <span class="wOxxOmified">' + n.innerHTML + '</span>';
		if (n.innerHTML != text) {
			var pristine = !n.wOxxOm;
			n.innerHTML = text;
			n.wOxxOm = n.textContent;
			if (pristine)
				setMutationHandler(n, 'time', datify, {
					characterData: true,
					attributes: true, attributeFilter: ['title'],
					childList: true,
					subtree: true
				});
		}
	}
}