Reddit post time visible

Update time elements to include month, date, day, and time in UTC+5:30 on each Reddit post

目前為 2024-11-30 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Reddit post time visible
// @namespace    https://rant.li/boson
// @version      0.3
// @description  Update time elements to include month, date, day, and time in UTC+5:30 on each Reddit post
// @author       Your Name
// @match        *://*.reddit.com/*
// @grant        none
// @license      GNU AGPLv3
// ==/UserScript==

(function() {
    'use strict';
    function formatDate(date) {
        const options = {
            month: 'long',
            day: 'numeric',
            weekday: 'long',
            hour: '2-digit',
            minute: '2-digit',
            hour12: false
        };
        return date.toLocaleString('en-US', options);
    }

    function updateTime(element) {
        const datetime = element.getAttribute('datetime');
        if (datetime) {
            const utcDate = new Date(datetime);
            const offset = 5.5 * 60 * 60 * 1000; // 5.5 hours in milliseconds
            const localDate = new Date(utcDate.getTime() + offset);
            const formattedDate = formatDate(localDate);
            // Ensure we only add the formatted date if it's not already there
            if (!element.textContent.includes(formattedDate)) {
                element.textContent += ` | ${formattedDate}`;
            }
        }
    }

    function processTimeElements() {
        const timeElements = document.querySelectorAll('time.live-timestamp');
        timeElements.forEach(updateTime);
    }

    function processNewNodes(nodes) {
        nodes.forEach(node => {
            if (node.nodeType === Node.ELEMENT_NODE) {
                const timeElements = node.querySelectorAll ? node.querySelectorAll('time.live-timestamp') : [];
                timeElements.forEach(updateTime);
            }
        });
    }

    function handleMutations(mutationsList, observer) {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                processNewNodes(mutation.addedNodes);
            } else if (mutation.type === 'attributes' && mutation.attributeName === 'datetime') {
                updateTime(mutation.target);
            } else if (mutation.type === 'characterData') {
                // Handle direct text content changes
                if (mutation.target.parentElement && mutation.target.parentElement.classList.contains('live-timestamp')) {
                    updateTime(mutation.target.parentElement);
                }
            }
        }
    }

    function interceptRedditTimestampUpdates() {
        const originalSetAttribute = Element.prototype.setAttribute;

        Element.prototype.setAttribute = function(name, value) {
            if (name === 'datetime' && this.classList.contains('live-timestamp')) {
                originalSetAttribute.call(this, name, value);
                updateTime(this);
            } else {
                originalSetAttribute.call(this, name, value);
            }
        };
    }

    window.addEventListener('load', function() {
        interceptRedditTimestampUpdates();
        processTimeElements();
        const observer = new MutationObserver(handleMutations);
        const config = { childList: true, subtree: true, attributes: true, attributeFilter: ['datetime'], characterData: true, characterDataOldValue: true };
        observer.observe(document.body, config);
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址