Greasy Fork

来自缓存

Greasy Fork is available in English.

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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
    });
})();