Greasy Fork is available in English.

KAT: smart full date/time

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

< Commentaires sur KAT: smart full date/time

Question / commentaire

q1k
§
Posté le: 2015-10-17
Édité le: 2015-10-17

I did a "few" changes :)

// ==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-BZ'; // i used this because it didn't show the comma after the year -> 1 Oct 2015, 10:45 (seamonkey)
//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 onehour = new Date();
onehour.setHours(onehour.getHours() - 2 );



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


var days2 = new Date();
days2.setDate(today.getDate() - 2);
days2.setHours(0, 0, 0, 0);

var days3 = new Date();
days3.setDate(today.getDate() - 3);
days3.setHours(0, 0, 0, 0);

var days4 = new Date();
days4.setDate(today.getDate() - 4);
days4.setHours(0, 0, 0, 0);

var days5 = new Date();
days5.setDate(today.getDate() - 5);
days5.setHours(0, 0, 0, 0);

var days6 = new Date();
days6.setDate(today.getDate() - 6);
days6.setHours(0, 0, 0, 0);

var days7 = new Date();
days7.setDate(today.getDate() - 7);
days7.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 >= onehour){
            // no change :)
        }
        else if (d >= today) {
            // no change, ------ yes change :P
            setContent(n, '<span class="wOxxOmified">today, </span>', d, {hour:'2-digit', minute:'2-digit'});
        } else if (d >= yesterday) {
            setContent(n, '<span class="wOxxOmified">yesterday, </span>', d, {hour:'2-digit', minute:'2-digit'});
        } else if (d >= days2){
            setContent(n, '<span class="wOxxOmified">2 days ago, </span>', d, {day:'numeric', month:'short', hour:'2-digit', minute:'2-digit'});
        } else if (d >= days3){
            setContent(n, '<span class="wOxxOmified">3 days ago, </span>', d, {day:'numeric', month:'short', hour:'2-digit', minute:'2-digit'});
        } else if (d >= days4){
            setContent(n, '<span class="wOxxOmified">4 days ago, </span>', d, {day:'numeric', month:'short', hour:'2-digit', minute:'2-digit'});
        } else if (d >= days5){
            setContent(n, '<span class="wOxxOmified">5 days ago, </span>', d, {day:'numeric', month:'short', hour:'2-digit', minute:'2-digit'});
        } else if (d >= days6){
            setContent(n, '<span class="wOxxOmified">6 days ago, </span>', d, {day:'numeric', month:'short', hour:'2-digit', minute:'2-digit'});
        } else if (d >= days7){
            setContent(n, '<span class="wOxxOmified">7 days ago, </span>', d, {day:'numeric', month:'short', hour:'2-digit', minute:'2-digit'});
        } else if (d >= year) {
            setContent(n, '<span class="wOxxOmified">' + n.innerHTML + ', </span>', d, {day:'numeric', month:'short', hour:'2-digit', minute:'2-digit'});
        } else {
            setContent(n, '<span class="wOxxOmified">' + n.innerHTML + ', </span>', d, {day:'numeric', month:'short', year:'numeric', 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, datify, 'time', {
                    characterData: true,
                    attributes: true, attributeFilter: ['title'],
                    childList: true,
                    subtree: true
                });
        }
    }
}

Thank you for this.

woxxomAuteur
§
Posté le: 2015-10-17
Édité le: 2015-10-17

I think it would be better if you submit your code as a separate script, the MIT license allows it. Since you use the KAT site, unlike me, you'll be able to update the script. If you do, I'll hide my script from the listing. Also add a // @contributor q1k and increase the version to 1.1.0 (because your changes are more substantial than a mere bugfix) in the metablock.

q1k
§
Posté le: 2015-10-31
Édité le: 2015-10-31

But what I can't figure out is this
var text = prefix ? prefix + time : time + ', ' + n.innerHTML + '';

The way I see it, is that the full date will be added as a prefix to the original text. Correct?

So whatever I try to do to swap these around, so that the full date is appended on the original text, I end up with a non functional script.

Poster une réponse

Connectez-vous pour poster une réponse.