MT Convert to Local Time

Override JavaScript's Date object to always use Beijing time (UTC+8), and convert all date and time values on a webpage from Beijing Time (GMT+8) to your local time zone.

目前为 2024-04-26 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/493489/1366429/MT%20Convert%20to%20Local%20Time.js

// ==UserScript==
// @name         MT Convert to Local Time
// @namespace    http://tampermonkey.net/
// @version      0.3.2
// @description  Override JavaScript's Date object to always use Beijing time (UTC+8), and convert all date and time values on a webpage from Beijing Time (GMT+8) to your local time zone.
// @author       tttsc, GPT4
// @match        https://*.m-team.cc/*
// @match        https://*.m-team.io/*
// @match        https://test2.m-team.cc/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const originalDate = Date;

    // Function to adjust the local time to Beijing time
    function toBeijingTime(original) {
        const localTime = new originalDate(original);
        const localTimeMs = localTime.getTime();
        const localOffset = localTime.getTimezoneOffset() * 60000; // Offset in milliseconds
        const beijingOffset = 8 * 3600 * 1000; // Beijing is UTC+8
        return new originalDate(localTimeMs + localOffset + beijingOffset);
    }

    // Override the Date object
    Date = function () {
        if (arguments.length === 0) {
            return toBeijingTime(originalDate.now());
        } else if (arguments.length === 1) {
            return new originalDate(arguments[0]);
        } else {
            return new originalDate(...arguments);
        }
    };

    Date.prototype = originalDate.prototype;
    Date.now = function() {
        return toBeijingTime(originalDate.now()).getTime();
    };
    Date.parse = originalDate.parse;
    Date.UTC = originalDate.UTC;
    Object.defineProperty(Date, 'prototype', { writable: false });
    // Log that the script is loaded and running
    console.log("Script is running");

    // Function to format dates into YYYY-MM-DD HH:MM:SS format
    function formatDateTime(date) {
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const day = date.getDate().toString().padStart(2, '0');
        const hours = date.getHours().toString().padStart(2, '0');
        const minutes = date.getMinutes().toString().padStart(2, '0');
        const seconds = date.getSeconds().toString().padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }

    // Convert time from Beijing to local time using the formatDateTime function
    function convertTimeToLocal(timeString) {
        const formattedTimeString = timeString.replace(' ', 'T') + '+08:00';
        const dateInBeijing = new Date(formattedTimeString);
        return formatDateTime(dateInBeijing);
    }

    // Process text nodes to find and replace date strings based on a regex
    function processTextNode(node) {
        const timePattern = /(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/g;
        if (node.nodeValue && node.nodeValue.match(timePattern)) {
            node.nodeValue = node.nodeValue.replace(timePattern, match => convertTimeToLocal(match));
            console.log(`Processed text node: `, node.nodeValue);
        }
    }

    // Traverse DOM elements to apply text node processing
    function traverseAndProcess(element) {
        element.childNodes.forEach(child => {
            if (child.nodeType === Node.TEXT_NODE) {
                processTextNode(child);
            } else if (child.nodeType === Node.ELEMENT_NODE) {
                traverseAndProcess(child);
            }
        });
    }

    // Setup a MutationObserver to monitor DOM changes and apply transformations to new elements
    const observer = new MutationObserver(mutations => {
        console.log("Mutations observed:", mutations); // Logs all mutations
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === Node.ELEMENT_NODE) {
                    traverseAndProcess(node);
                }
            });
        });
    });

    // Observe the entire body for changes in the child list or subtree
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // Process all elements immediately when the script runs initially
    document.querySelectorAll('body *').forEach(traverseAndProcess);

    setTimeout(() => {
        document.querySelectorAll('.ant-typography-secondary').forEach(traverseAndProcess);
        console.log("Script processed elements after delay");
    }, 1000); // Adjust time as necessary
})();

QingJ © 2025

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