Discord Timestamp with Seconds

This feature provides users with the capability to view seconds in the timestamps of Discord messages, allows users to view the time that has elapsed since a message was sent, and enhances the date formatting.

当前为 2023-04-14 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Discord Timestamp with Seconds
// @namespace    http://tampermonkey.net/
// @version      2.9
// @description  This feature provides users with the capability to view seconds in the timestamps of Discord messages, allows users to view the time that has elapsed since a message was sent, and enhances the date formatting.
// @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`;
        }
    }
})();