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

  1. // ==UserScript==
  2. // @name Discord Timestamp with Seconds
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.2
  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. // Function to format the timestamp with seconds
  16. function formatTimestampWithSeconds(datetime, originalLabel) {
  17. const date = new Date(datetime);
  18. const hours = date.getHours() % 12 || 12;
  19. const minutes = date.getMinutes().toString().padStart(2, '0');
  20. const seconds = date.getSeconds().toString().padStart(2, '0');
  21. const ampm = date.getHours() >= 12 ? 'PM' : 'AM';
  22. const timeString = `${hours}:${minutes}:${seconds} ${ampm}`;
  23.  
  24. // Return the original label with the seconds added
  25. return originalLabel.replace(/(\d+:\d+\s*[APM]{2})/, timeString);
  26. }
  27.  
  28. // Function to calculate the relative time
  29. function getRelativeTime(datetime) {
  30. const now = new Date();
  31. const date = new Date(datetime);
  32. const diff = now - date;
  33.  
  34. const seconds = Math.floor(diff / 1000);
  35. const minutes = Math.floor(seconds / 60);
  36. const hours = Math.floor(minutes / 60);
  37. const days = Math.floor(hours / 24);
  38. const weeks = Math.floor(days / 7);
  39. const months = Math.floor(days / 30);
  40. const years = Math.floor(days / 365);
  41.  
  42. if (seconds < 60) {
  43. return `${seconds} second${seconds !== 1 ? 's' : ''} ago`;
  44. } else if (minutes < 60) {
  45. return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`;
  46. } else if (hours < 24) {
  47. return `${hours} hour${hours !== 1 ? 's' : ''} ago`;
  48. } else if (days < 7) {
  49. return `${days} day${days !== 1 ? 's' : ''} ago`;
  50. } else if (weeks < 4) {
  51. return `${weeks} week${weeks !== 1 ? 's' : ''} ago`;
  52. } else if (months < 12) {
  53. return `${months} month${months !== 1 ? 's' : ''} ago`;
  54. } else {
  55. return `${years} year${years !== 1 ? 's' : ''} ago`;
  56. }
  57. }
  58.  
  59. // Observer to monitor DOM changes
  60. const observer = new MutationObserver(mutations => {
  61. for (const mutation of mutations) {
  62. if (mutation.type === 'childList' && mutation.addedNodes.length) {
  63. mutation.addedNodes.forEach(node => {
  64. if (node.nodeType === Node.ELEMENT_NODE) {
  65. const timeElements = node.querySelectorAll('time');
  66. timeElements.forEach(timeElement => {
  67. const datetime = timeElement.getAttribute('datetime');
  68. const originalLabel = timeElement.getAttribute('aria-label');
  69. if (datetime && originalLabel) {
  70. const formattedTime = formatTimestampWithSeconds(datetime, originalLabel);
  71. const relativeTime = getRelativeTime(datetime);
  72. timeElement.textContent = `${formattedTime} (${relativeTime})`;
  73. timeElement.setAttribute('aria-label', `${formattedTime} (${relativeTime})`);
  74. }
  75. });
  76. }
  77. });
  78. }
  79. }
  80. });
  81.  
  82. // Start observing the body for changes
  83. observer.observe(document.body, { childList: true, subtree: true });
  84.  
  85. })();