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.

目前為 2024-07-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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.3
// @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';

    // Function to format the timestamp with seconds
    function formatTimestampWithSeconds(datetime) {
        const date = new Date(datetime);
        const hours = date.getHours() % 12 || 12;
        const minutes = date.getMinutes().toString().padStart(2, '0');
        const seconds = date.getSeconds().toString().padStart(2, '0');
        const ampm = date.getHours() >= 12 ? 'PM' : 'AM';
        return `${hours}:${minutes}:${seconds} ${ampm}`;
    }

    // Function to calculate the relative time
    function getRelativeTime(datetime) {
        const now = new Date();
        const date = new Date(datetime);
        const diff = now - date;

        const seconds = Math.floor(diff / 1000);
        const minutes = Math.floor(seconds / 60);
        const hours = Math.floor(minutes / 60);
        const days = Math.floor(hours / 24);
        const weeks = Math.floor(days / 7);
        const months = Math.floor(days / 30);
        const years = Math.floor(days / 365);

        if (seconds < 60) {
            return `${seconds} second${seconds !== 1 ? 's' : ''} ago`;
        } else if (minutes < 60) {
            return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`;
        } else if (hours < 24) {
            return `${hours} hour${hours !== 1 ? 's' : ''} ago`;
        } else if (days < 7) {
            return `${days} day${days !== 1 ? 's' : ''} ago`;
        } else if (weeks < 4) {
            return `${weeks} week${weeks !== 1 ? 's' : ''} ago`;
        } else if (months < 12) {
            return `${months} month${months !== 1 ? 's' : ''} ago`;
        } else {
            return `${years} year${years !== 1 ? 's' : ''} ago`;
        }
    }

    // Function to update timestamps
    function updateTimestamps() {
        const timeElements = document.querySelectorAll('time');
        timeElements.forEach(timeElement => {
            const datetime = timeElement.getAttribute('datetime');
            if (datetime) {
                const originalLabel = timeElement.getAttribute('aria-label');
                const formattedTime = formatTimestampWithSeconds(datetime);
                const relativeTime = getRelativeTime(datetime);

                if (timeElement.parentElement.classList.contains('timestampVisibleOnHover_f9f2ca')) {
                    // For hover timestamps, only show time without relative time
                    timeElement.textContent = formattedTime;
                    timeElement.setAttribute('aria-label', formattedTime);
                } else {
                    // For normal timestamps, preserve the original format and add relative time
                    if (!originalLabel.includes(formattedTime)) {
                        timeElement.textContent = `${originalLabel.replace(/(\d+:\d+ [APM]{2})/, formattedTime)} (${relativeTime})`;
                        timeElement.setAttribute('aria-label', `${originalLabel.replace(/(\d+:\d+ [APM]{2})/, formattedTime)} (${relativeTime})`);
                    } else {
                        // Update only the relative time
                        const baseLabel = originalLabel.split(' (')[0];
                        timeElement.textContent = `${baseLabel} (${relativeTime})`;
                        timeElement.setAttribute('aria-label', `${baseLabel} (${relativeTime})`);
                    }
                }
            }
        });
    }

    // Observer to monitor DOM changes
    const observer = new MutationObserver(mutations => {
        for (const mutation of mutations) {
            if (mutation.type === 'childList' && mutation.addedNodes.length) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        const timeElements = node.querySelectorAll('time');
                        timeElements.forEach(timeElement => {
                            const datetime = timeElement.getAttribute('datetime');
                            const originalLabel = timeElement.getAttribute('aria-label');
                            if (datetime && originalLabel) {
                                const formattedTime = formatTimestampWithSeconds(datetime);
                                const relativeTime = getRelativeTime(datetime);
                                if (timeElement.parentElement.classList.contains('timestampVisibleOnHover_f9f2ca')) {
                                    // For hover timestamps, only show time without relative time
                                    timeElement.textContent = formattedTime;
                                    timeElement.setAttribute('aria-label', formattedTime);
                                } else {
                                    // For normal timestamps, preserve the original format and add relative time
                                    if (!originalLabel.includes(formattedTime)) {
                                        timeElement.textContent = `${originalLabel.replace(/(\d+:\d+ [APM]{2})/, formattedTime)} (${relativeTime})`;
                                        timeElement.setAttribute('aria-label', `${originalLabel.replace(/(\d+:\d+ [APM]{2})/, formattedTime)} (${relativeTime})`);
                                    } else {
                                        // Update only the relative time
                                        const baseLabel = originalLabel.split(' (')[0];
                                        timeElement.textContent = `${baseLabel} (${relativeTime})`;
                                        timeElement.setAttribute('aria-label', `${baseLabel} (${relativeTime})`);
                                    }
                                }
                            }
                        });
                    }
                });
            }
        }
    });

    // Start observing the body for changes
    observer.observe(document.body, { childList: true, subtree: true });

    // Refresh relative times every second
    setInterval(updateTimestamps, 1000);

    // Initial timestamp update
    updateTimestamps();

})();