Greasy Fork

Greasy Fork is available in English.

X (Twitter) 绝对时间显示

将推特相对时间改为绝对时间格式 (YYYY-MM-DD HH:mm)

当前为 2026-01-08 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         X (Twitter) 绝对时间显示
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  将推特相对时间改为绝对时间格式 (YYYY-MM-DD HH:mm)
// @author       Gemini
// @match        https://x.com/*
// @match        https://twitter.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    function formatTime() {
        // 查找 X 网页版中所有的 time 标签
        const timeElements = document.querySelectorAll('time');

        timeElements.forEach(timeEl => {
            // 如果已经处理过,则跳过,避免重复逻辑消耗性能
            if (timeEl.dataset.absoluteTimeReady) return;

            const datetime = timeEl.getAttribute('datetime');
            if (datetime) {
                const date = new Date(datetime);

                // 格式化时间:YYYY-MM-DD HH:mm
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                const hours = String(date.getHours()).padStart(2, '0');
                const minutes = String(date.getMinutes()).padStart(2, '0');

                const absoluteTime = `${year}-${month}-${day} ${hours}:${minutes}`;

                // 针对 X 的 DOM 结构,找到 time 标签内部的 span 进行替换
                const span = timeEl.querySelector('span');
                if (span) {
                    span.textContent = absoluteTime;
                } else {
                    timeEl.textContent = absoluteTime;
                }

                // 标记已处理,防止 MutationObserver 无限循环触发
                timeEl.dataset.absoluteTimeReady = "true";
            }
        });
    }

    // 监听网页动态加载(滚动加载新推文)
    const observer = new MutationObserver((mutations) => {
        // 简单优化:只有当有新节点加入时才执行
        for (let mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                formatTime();
                break;
            }
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 初始执行一次
    formatTime();
})();