为 Telegram Channel 的时间显示添加日期

在Telegram消息时间显示中添加日期

// ==UserScript==
// @name         为 Telegram Channel 的时间显示添加日期
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description  在Telegram消息时间显示中添加日期
// @author       YourName
// @match        https://t.me/s/*
// @icon         https://telegram.org/favicon.ico
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 格式化日期时间为 MM-DD HH:mm
    function formatDateTime(datetime) {
        const date = new Date(datetime);
        return [
            ('0' + (date.getMonth() + 1)).slice(-2),  // 月份补零
            ('0' + date.getDate()).slice(-2),          // 日期补零
        ].join('-') + ' ' +
        [
            ('0' + date.getHours()).slice(-2),         // 小时补零
            ('0' + date.getMinutes()).slice(-2)        // 分钟补零
        ].join(':');
    }

    // 更新所有时间显示
    function updateTimes() {
        document.querySelectorAll('time.time').forEach(timeEl => {
            const datetime = timeEl.getAttribute('datetime');
            if (datetime) {
                timeEl.textContent = formatDateTime(datetime);
            }
        });
    }

    // 使用MutationObserver监听动态内容
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.addedNodes.length) {
                updateTimes();
            }
        });
    });

    // 初始化执行
    updateTimes();

    // 开始观察整个文档
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();

QingJ © 2025

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