Guest User

Untitled

a guest
Apr 18th, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name Relative Time Tooltip (Auto-Update to seconds)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3
  5. // @description Relative time shown as an auto-updated hover popup
  6. // @match *://8chan.moe/*
  7. // @grant GM_addStyle
  8. // ==/UserScript==
  9.  
  10. (function () {
  11. 'use strict';
  12.  
  13. // Inject tooltip CSS
  14. GM_addStyle(`
  15. .relativeTime {
  16. visibility: hidden;
  17. opacity: 0;
  18. position: absolute;
  19. background-color: #333;
  20. color: #fff;
  21. font-size: 12px;
  22. padding: 4px 8px;
  23. border-radius: 4px;
  24. white-space: nowrap;
  25. z-index: 999;
  26. transition: opacity 0.2s ease;
  27. margin-top: -30px;
  28. margin-left: 10px;
  29. box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  30. pointer-events: none;
  31. }
  32.  
  33. .labelCreated {
  34. position: relative;
  35. cursor: pointer;
  36. }
  37.  
  38. .labelCreated:hover + .relativeTime {
  39. visibility: visible;
  40. opacity: 1;
  41. }
  42. `);
  43.  
  44. function parseDate(dateString) {
  45. const match = dateString.match(/(\d{2})\/(\d{2})\/(\d{4}).*?(\d{2}):(\d{2}):(\d{2})/);
  46. if (!match) return null;
  47. const [_, month, day, year, hour, minute, second] = match;
  48. return new Date(`${year}-${month}-${day}T${hour}:${minute}:${second}`);
  49. }
  50.  
  51. function getRelativeTimeString(date) {
  52. const now = new Date();
  53. const diffMs = now - date;
  54. const seconds = Math.floor(diffMs / 1000);
  55. const minutes = Math.floor(seconds / 60);
  56. const hours = Math.floor(minutes / 60);
  57. const days = Math.floor(hours / 24);
  58. const remainingHours = hours % 24;
  59.  
  60. if (seconds < 5) return 'just now';
  61. if (seconds < 60) return `${seconds} second${seconds !== 1 ? 's' : ''} ago`;
  62. if (minutes < 60) return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`;
  63. if (hours < 24) return `${hours} hour${hours !== 1 ? 's' : ''} ago`;
  64.  
  65. // Show "X days Y hours ago"
  66. return `${days} day${days !== 1 ? 's' : ''}` +
  67. (remainingHours > 0 ? ` ${remainingHours} hour${remainingHours !== 1 ? 's' : ''}` : '') +
  68. ' ago';
  69. }
  70.  
  71.  
  72. function updateRelativeTimes() {
  73. document.querySelectorAll('.labelCreated').forEach(label => {
  74. const timeText = label.textContent.trim();
  75. const postDate = parseDate(timeText);
  76. if (!postDate) return;
  77.  
  78. let relativeEl = label.nextElementSibling;
  79. if (!relativeEl || !relativeEl.classList.contains('relativeTime')) {
  80. relativeEl = document.createElement('span');
  81. relativeEl.className = 'relativeTime';
  82. label.after(relativeEl);
  83. }
  84.  
  85. relativeEl.textContent = getRelativeTimeString(postDate);
  86. });
  87. }
  88.  
  89. // Initial run and recurring every second for precision
  90. updateRelativeTimes();
  91. setInterval(updateRelativeTimes, 1000);
  92.  
  93. // Handle dynamically loaded posts
  94. const observer = new MutationObserver(updateRelativeTimes);
  95. observer.observe(document.body, { childList: true, subtree: true });
  96. })();
  97.  
Advertisement
Add Comment
Please, Sign In to add comment