hackernews tweaks

Show absolute time next to relative time on posts and comments

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         hackernews tweaks
// @namespace    HackerNewsTweaks
// @version      1.0.0
// @description  Show absolute time next to relative time on posts and comments
// @author       [email protected]
// @match        https://news.ycombinator.com/item*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  function updateTimestamps() {
    const times = document.querySelectorAll("span.age");

    times.forEach((timeEl) => {
      // Avoid duplicating the absolute time
      if (timeEl.dataset.absoluteShown) return;

      const absoluteTime = timeEl.getAttribute("title");
      const cleanDateStr = absoluteTime.split(' ')[0];
      if (cleanDateStr) {
        // Format the absolute time
        const date = new Date(cleanDateStr);
        //const formatted = date.toLocaleString(); // Local user format
        let formatted = new Date(date.getTime() - (new Date().getTimezoneOffset() * 60000)).toISOString().slice(0,16).replace('T', ' ');
        if(formatted.slice(0,4) === new Date().toLocaleDateString().slice(-4)){
           formatted = formatted.slice(5);
        }

        // Append absolute time in parentheses
        timeEl.textContent += ` (${formatted})`;
        timeEl.dataset.absoluteShown = "true";
      }
    });
  }

  // Initial call
  updateTimestamps();

  // Observe dynamically loaded content (React-based site)
  const observer = new MutationObserver(updateTimestamps);
  observer.observe(document.body, { childList: true, subtree: true });
})();