Discord Timestamp with Seconds

This script 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-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Discord Timestamp with Seconds
// @namespace    http://tampermonkey.net/
// @version      3.1
// @description  This script 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`;
        }
    }
})();