SamGitHub1807 / Discord-Timestamp-with-Seconds-and-Improved-Date-Format

0 stars 0 forks source link

(FIXED; Will keep open for a little longer) When hovering on a message, there's too much text to display that it is sometimes covered up by the profile picture of a Discord user. #1

Open SamGitHub1807 opened 1 year ago

SamGitHub1807 commented 1 year ago
Issue 1
sapondanaisriwan commented 1 year ago

You need to give more context and what you expected. if you want the results to look like this, you need to change the query selector to h3 .timestamp-p1Df1m time I hope this meets your expectations. msedge_5wvFnlL8Ht

SamGitHub1807 commented 1 year ago

Omg, thank you so much ahahah I wasn't expecting anyone to notice this xD-

SamGitHub1807 commented 1 year ago

About the issue:

Yes, that's the results I wanted to achieve! All I wanted to do was to remove the "date" and "today at" when hovering therefore, it would only display the time with the seconds.

EDIT: It is now fixed. Again, thank you!

EDIT 2: Not sure why I did this lol, it was supposed to be an only for myself thing because I wanted to see when exactly I sent a message lol. Somehow I ended up sharing it for everyone's use.

EDIT 3: I took a little bit of time to see if the code was working properly lol, just noticed that the "pinned messages" no longer apply the timestamp updates, it's just Discord's default timestamp, example: 9/04/2021 8:43 PM. I downgraded to the older versions and it works just fine lol (Sep 4, 2021 8:43:05 PM (1 year ago))

sapondanaisriwan commented 1 year ago

Try this msedge_L3st0EIA7Q msedge_O6vc477g07

// ==UserScript==
// @name         Discord Timestamp with Seconds and Date
// @namespace    http://tampermonkey.net/
// @version      2.6
// @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`;
        }
    }
})();
SamGitHub1807 commented 1 year ago

Omg, again, thank you, for taking the time out of your day to help ❤️ Your expertise in Discord + YouTube + even more script code is truly impressive. I appreciate you for sharing your knowledge with me and helping me solve the problem.

EDIT 1: Yes, it works now. Everything has been updated and changed. Once again, thank you, for the help on such a silly project lol :/