Discord Timestamp with Seconds and Date

Gives users the ability to see seconds to Discord message timestamps and improves the date format. It also allows users to see the time elapsed since a message was sent.

Από την 26/03/2023. Δείτε την τελευταία έκδοση.

// ==UserScript==
// @name         Discord Timestamp with Seconds and Date
// @namespace    http://tampermonkey.net/
// @version      2.8
// @description  Gives users the ability to see seconds to Discord message timestamps and improves the date format. It also allows users to see the time elapsed since a message was sent.
// @author       Sam (and ChatGPT lol; yes, ChatGPT helped me with this. With the help of many others listed on the "History" section of the GreasyFork page of this script. (https://greasyfork.org/en/scripts/462588-discord-timestamp-with-seconds-and-date)
// @match        https://discord.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // Define a function to handle mutations
    function handleMutations(mutationsList, observer) {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                const messages = mutation.target.querySelectorAll('.timestamp-p1Df1m time');
                messages.forEach((message) => {
                    const timestamp = message.getAttribute('datetime');

                    const date = new Date(timestamp);
                    const now = new Date();
                    const timeElapsed = getTimeElapsed(date, now);
                    const formattedTime = date.toLocaleTimeString([], { hour: 'numeric', minute: 'numeric', second: 'numeric' });
                    const formattedDate = date.toLocaleDateString([], { year: 'numeric', month: 'short', day: 'numeric' });
                    const todayFormattedTime = `Today at ${formattedTime}`;

                    let messageContent;
                    const dateElement = '<i class="separator-AebOhG" aria-hidden="true"> — </i>'
                    if (now.toDateString() === date.toDateString()) {
                        messageContent = message.parentElement.classList.contains('timestampVisibleOnHover-9PEuZS')
                            ? `${dateElement} ${formattedTime} (${timeElapsed})`
                            : `${dateElement} ${todayFormattedTime} (${timeElapsed})`;
                    } else {
                        messageContent = message.parentElement.classList.contains('timestampVisibleOnHover-9PEuZS')
                            ? `${dateElement} ${formattedTime} (${timeElapsed})`
                            : `${dateElement} ${formattedDate} ${formattedTime} (${timeElapsed})`;
                    }

                    message.innerHTML = messageContent;
                })
            }
        }
    }

    // Create a new MutationObserver and observe the document
    const observer = new MutationObserver(handleMutations);
    observer.observe(document, { childList: true, subtree: true });

    // Define a function to calculate the time elapsed between two dates
    function getTimeElapsed(date1, date2) {
        const diff = Math.abs(date2.getTime() - date1.getTime()) / 1000;
        const minutes = Math.floor(diff / 60);
        const hours = Math.floor(minutes / 60);
        const days = Math.floor(hours / 24);
        const months = Math.floor(days / 30);
        const years = Math.floor(months / 12);

        if (years > 0) {
            return `${years} year${years > 1 ? 's' : ''} ago`;
        } else if (months > 0) {
            return `${months} month${months > 1 ? 's' : ''} ago`;
        } else if (days > 0) {
            return `${days} day${days > 1 ? 's' : ''} ago`;
        } else if (hours > 0) {
            return `${hours} hour${hours > 1 ? 's' : ''} ago`;
        } else {
            return `${minutes} minute${minutes > 1 ? 's' : ''} ago`;
        }
    }
})();