Skip to content

Instantly share code, notes, and snippets.

@theamanbhargava
Created May 16, 2025 21:07
Show Gist options
  • Select an option

  • Save theamanbhargava/50cc074a0356b5ec0df220702e137ed5 to your computer and use it in GitHub Desktop.

Select an option

Save theamanbhargava/50cc074a0356b5ec0df220702e137ed5 to your computer and use it in GitHub Desktop.
Reddit Comments Add Absolute Datetime
// ==UserScript==
// @name Reddit Comment Timestamp Enhancer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Enhances Reddit comment timestamps to show both relative and exact time
// @author You
// @match https://*.reddit.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to format the date in a readable way
function formatDate(dateStr) {
const date = new Date(dateStr);
return date.toLocaleString();
}
// Function to enhance a timestamp element
function enhanceTimestamp(timeElement) {
if (!timeElement || !timeElement.getAttribute('datetime')) return;
// Skip if already enhanced
if (timeElement.dataset.enhanced) return;
const datetime = timeElement.getAttribute('datetime');
const relativeTime = timeElement.textContent.trim();
// Format the date
const formattedDate = formatDate(datetime);
// Update the text content to include both the relative and absolute time
timeElement.textContent = `${relativeTime} (${formattedDate})`;
// Mark as enhanced to avoid processing it again
timeElement.dataset.enhanced = "true";
}
// Function to scan for and enhance timestamps
function scanAndEnhance() {
// Try to find all time elements within comments
// Using a specific selector based on the provided XPath
const commentTimes = document.querySelectorAll('shreddit-comment time[datetime]');
// If none found with the specific selector, try a more generic approach
if (commentTimes.length === 0) {
const allTimes = document.querySelectorAll('time[datetime]');
allTimes.forEach(enhanceTimestamp);
} else {
commentTimes.forEach(enhanceTimestamp);
}
}
// Initial scan - wait a bit for Reddit to load content
setTimeout(scanAndEnhance, 2000);
// Set up a mutation observer to handle dynamically loaded content
const observer = new MutationObserver(function(mutations) {
let shouldScan = false;
// Check if we need to scan for new elements
for (const mutation of mutations) {
if (mutation.addedNodes.length) {
shouldScan = true;
break;
}
}
if (shouldScan) {
scanAndEnhance();
}
});
// Start observing the document body for changes
observer.observe(document.body, { childList: true, subtree: true });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment