Hacker News Date Tooltips

Deobfuscate the "n days ago" dates on Hacker News with YYYY-MM-DD tooltips

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name          Hacker News Date Tooltips
// @description   Deobfuscate the "n days ago" dates on Hacker News with YYYY-MM-DD tooltips
// @author        chocolateboy
// @copyright     chocolateboy
// @version       1.1.0
// @namespace     https://github.com/chocolateboy/userscripts
// @license       GPL: https://www.gnu.org/copyleft/gpl.html
// @include       https://news.ycombinator.com/*
// @require       https://cdn.jsdelivr.net/npm/[email protected]/dist/cash.min.js
// @require       https://unpkg.com/[email protected]/dayjs.min.js
// @grant         GM_log
// ==/UserScript==

const DATES = 'span.age a'
const DELTA = 1, UNIT = 2

function isoDate (ago) {
    const match = ago.match(/^(\d+)\s+(\w+)\s+ago$/)

    return match
        ? dayjs().subtract(match[DELTA], match[UNIT]).format('YYYY-MM-DD')
        : null
}

$(DATES).each(function () {
    const $this = $(this)
    const ago   = $this.text().trim()
    const date  = isoDate(ago)

    if (date) {
        $this.attr('title', date)
    }
})