Last active
October 20, 2023 11:22
-
-
Save shinmai/4e57cbb5f1c251f3bbe54adf5ed89481 to your computer and use it in GitHub Desktop.
Mastodon absolute timestamps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Mastodon absolute timestamps | |
// @namespace rip.aapo.userscripts | |
// @version 0.1 | |
// @description show absolute time on posts on Mastodon | |
// @author @shinmai - https://shinmai.wtf | |
// @match https://tech.lgbt/* | |
// @grant none | |
// @updateURL https://gist.github.com/shinmai/4e57cbb5f1c251f3bbe54adf5ed89481/raw/chronotoot.user.js | |
// @downloadURL https://gist.github.com/shinmai/4e57cbb5f1c251f3bbe54adf5ed89481/raw/chronotoot.user.js | |
// ==/UserScript== | |
/** | |
* NOTA BENE: Unless your home instance is tech.lgbt, you'll need to change the script (or your userscript extension) to also match your instance. | |
* secondly, the default format for the timestamps is YYMMDD-HHMMSS using 24h time, you can update timestampFormat below with an strftime compatible format string to change this. | |
* (the above is powered by T. H. Doan's JS "port" of strftime, which is English only and might not be 100% 1:1 identical to strftime().) | |
*/ | |
/** | |
* Changelog: | |
* 0.0.1 (231020) - initial public version | |
*/ | |
(async function() { | |
'use strict'; | |
const timestampFormat = "%y%m%d-%H%M%S" | |
// https://github.com/thdoan/strftime | |
const strftime = (n,a) => {if("string"!==typeof n)return"";a instanceof Date||(a=new Date);var h=a.getDay(),k=a.getDate(),e=a.getMonth(),f=a.getFullYear(),g=a.getHours(),p=a.getTime(),q="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),r="January February March April May June July August September October November December".split(" "),u=[0,31,59,90,120,151,181,212,243,273,304,334],m=function(){var c=new Date(a);c.setDate(k-(h+6)%7+3);return c},b=function(c,l){return(Math.pow(10,l)+ | |
c+"").slice(1)};return n.replace(/%[a-z]+\b/gi,function(c){var l;return({"%a":q[h].slice(0,3),"%A":q[h],"%b":r[e].slice(0,3),"%B":r[e],"%c":a.toUTCString().replace(",",""),"%C":Math.floor(f/100),"%d":b(k,2),"%e":k,"%F":(new Date(p-6E4*a.getTimezoneOffset())).toISOString().slice(0,10),"%G":m().getFullYear(),"%g":(m().getFullYear()+"").slice(2),"%H":b(g,2),"%I":b((g+11)%12+1,2),"%j":b(u[e]+k+(1<e&&(0===f%4&&0!==f%100||0===f%400)?1:0),3),"%k":g,"%l":(g+11)%12+1,"%m":b(e+1,2),"%n":e+1,"%M":b(a.getMinutes(), | |
2),"%p":12>g?"AM":"PM","%P":12>g?"am":"pm","%s":Math.round(p/1E3),"%S":b(a.getSeconds(),2),"%u":h||7,"%V":function(){var d=m(),v=d.valueOf();d.setMonth(0,1);var t=d.getDay();4!==t&&d.setMonth(0,1+(4-t+7)%7);return b(1+Math.ceil((v-d)/6048E5),2)}(),"%w":h,"%x":a.toLocaleDateString(),"%X":a.toLocaleTimeString(),"%y":(f+"").slice(2),"%Y":f,"%z":a.toTimeString().replace(/.+GMT([+-]\d+).+/,"$1"),"%Z":a.toTimeString().replace(/.+\((.+?)\)$/,"$1"),"%Zs":null==(l=(new Intl.DateTimeFormat("default",{timeZoneName:"short"})).formatToParts(a).find(function(d){return"timeZoneName"=== | |
d.type}))?void 0:l.value}[c]||"")+""||c})} | |
const swapTimes = elm => { | |
elm.title=elm.innerText | |
elm.innerText = strftime(timestampFormat, new Date(elm.dateTime)) | |
} | |
const observer = new MutationObserver(function (mutations, observer) { | |
for (const mutation of mutations) { | |
if(mutation.type == "childList") | |
for (const node of mutation.addedNodes) { | |
if (node.nodeType !== 1) continue | |
for (const elm of node.getElementsByTagName("time")) { | |
swapTimes(elm) | |
} | |
} | |
else if(mutation.type == "characterData") { | |
if(mutation.target.parentElement.tagName != "TIME") continue | |
swapTimes(mutation.target.parentElement) | |
} | |
} | |
}) | |
observer.observe(document, { childList: true, subtree: true, characterData: true }) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment