Discord Timestamp with Seconds and Date

Adds seconds, date, and time elapsed to Discord timestamps and displays "Today" for current-day messages

当前为 2023-03-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Discord Timestamp with Seconds and Date
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.0
  5. // @description Adds seconds, date, and time elapsed to Discord timestamps and displays "Today" for current-day messages
  6. // @author Sam (and ChatGPT lol; yes, ChatGPT helped me with this.
  7. // @match https://discord.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. let observer = new MutationObserver(function(mutations) {
  16. mutations.forEach(function(mutation) {
  17. if (mutation.type == "childList") {
  18. let messages = mutation.target.querySelectorAll(".timestamp-p1Df1m time");
  19. messages.forEach(function(message) {
  20. let timestamp = message.getAttribute("datetime");
  21. let date = new Date(timestamp);
  22. let now = new Date();
  23. let formattedTime = date.toLocaleTimeString([], { hour: 'numeric', minute: 'numeric', second: 'numeric' }).replace(" PM", " PM").replace(" AM", " AM");
  24. let formattedDate = date.toLocaleDateString([], { year: 'numeric', month: 'short', day: 'numeric' });
  25. let todayFormattedTime = "Today at " + formattedTime;
  26. let timeElapsed = getTimeElapsed(date, now);
  27.  
  28. if (now.toDateString() === date.toDateString()) {
  29. message.innerHTML = "<i class=\"separator-AebOhG\" aria-hidden=\"true\"> — </i>" + todayFormattedTime + " (" + timeElapsed + ")";
  30. } else {
  31. message.innerHTML = "<i class=\"separator-AebOhG\" aria-hidden=\"true\"> — </i>" + formattedDate + " " + formattedTime + " (" + timeElapsed + ")";
  32. }
  33. });
  34. }
  35. });
  36. });
  37.  
  38. observer.observe(document, { childList: true, subtree: true });
  39.  
  40. function getTimeElapsed(date1, date2) {
  41. let diff = Math.abs(date2.getTime() - date1.getTime()) / 1000;
  42. let minutes = Math.floor(diff / 60);
  43. let hours = Math.floor(minutes / 60);
  44. let days = Math.floor(hours / 24);
  45. let months = Math.floor(days / 30);
  46. let years = Math.floor(months / 12);
  47.  
  48. if (years > 0) {
  49. return years + " year" + (years > 1 ? "s" : "") + " ago";
  50. } else if (months > 0) {
  51. return months + " month" + (months > 1 ? "s" : "") + " ago";
  52. } else if (days > 0) {
  53. return days + " day" + (days > 1 ? "s" : "") + " ago";
  54. } else if (hours > 0) {
  55. return hours + " hour" + (hours > 1 ? "s" : "") + " ago";
  56. } else {
  57. return minutes + " minute" + (minutes > 1 ? "s" : "") + " ago";
  58. }
  59. }
  60. })();
  61.  
  62. // MIT License
  63. // Permission is hereby granted, free of charge, to any person obtaining a copy
  64. // of this software and associated documentation files (the "Software"), to deal
  65. // in the Software without restriction, including without limitation the rights
  66. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  67. // copies of the Software, and to permit persons to whom the Software is
  68. // furnished to do so, subject to the following conditions:
  69. //
  70. // The above copyright notice and this permission notice shall be included in all
  71. // copies or substantial portions of the Software.
  72. //
  73. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  74. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  75. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  76. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  77. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  78. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  79. // SOFTWARE.