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 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Discord Timestamp with Seconds
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.1
  5. // @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.
  6. // @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)
  7. // @match https://discord.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // Define a function to handle mutations
  16. function handleMutations(mutationsList, observer) {
  17. for (let mutation of mutationsList) {
  18. if (mutation.type === 'childList') {
  19. const messages = mutation.target.querySelectorAll('.timestamp-p1Df1m time');
  20. messages.forEach((message) => {
  21. const timestamp = message.getAttribute('datetime');
  22.  
  23. const date = new Date(timestamp);
  24. const now = new Date();
  25. const timeElapsed = getTimeElapsed(date, now);
  26. const formattedTime = date.toLocaleTimeString([], { hour: 'numeric', minute: 'numeric', second: 'numeric' });
  27. const formattedDate = date.toLocaleDateString([], { year: 'numeric', month: 'short', day: 'numeric' });
  28. const todayFormattedTime = `Today at ${formattedTime}`;
  29.  
  30. let messageContent;
  31. const dateElement = '<i class="separator-AebOhG" aria-hidden="true"> — </i>'
  32. if (now.toDateString() === date.toDateString()) {
  33. messageContent = message.parentElement.classList.contains('timestampVisibleOnHover-9PEuZS')
  34. ? `${dateElement} ${formattedTime} (${timeElapsed})`
  35. : `${dateElement} ${todayFormattedTime} (${timeElapsed})`;
  36. } else {
  37. messageContent = message.parentElement.classList.contains('timestampVisibleOnHover-9PEuZS')
  38. ? `${dateElement} ${formattedTime} (${timeElapsed})`
  39. : `${dateElement} ${formattedDate} ${formattedTime} (${timeElapsed})`;
  40. }
  41.  
  42. message.innerHTML = messageContent;
  43. })
  44. }
  45. }
  46. }
  47.  
  48. // Create a new MutationObserver and observe the document
  49. const observer = new MutationObserver(handleMutations);
  50. observer.observe(document, { childList: true, subtree: true });
  51.  
  52. // Define a function to calculate the time elapsed between two dates
  53. function getTimeElapsed(date1, date2) {
  54. const diff = Math.abs(date2.getTime() - date1.getTime()) / 1000;
  55. const minutes = Math.floor(diff / 60);
  56. const hours = Math.floor(minutes / 60);
  57. const days = Math.floor(hours / 24);
  58. const months = Math.floor(days / 30);
  59. const years = Math.floor(months / 12);
  60.  
  61. if (years > 0) {
  62. return `${years} year${years > 1 ? 's' : ''} ago`;
  63. } else if (months > 0) {
  64. return `${months} month${months > 1 ? 's' : ''} ago`;
  65. } else if (days > 0) {
  66. return `${days} day${days > 1 ? 's' : ''} ago`;
  67. } else if (hours > 0) {
  68. return `${hours} hour${hours > 1 ? 's' : ''} ago`;
  69. } else {
  70. return `${minutes} minute${minutes > 1 ? 's' : ''} ago`;
  71. }
  72. }
  73. })();