MT Convert to Local Time

Override JavaScript's Date object to always use Beijing time (UTC+8)

Tính đến 25-04-2024. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyforks.org/scripts/493489/1366389/MT%20Convert%20to%20Local%20Time.js

// ==UserScript==
// @name         MT Convert to Local Time
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Override JavaScript's Date object to always use Beijing time (UTC+8)
// @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 });
    console.log('Tampermonkey script loaded: Starting conversion...');

    // Function to convert time from Beijing to local
    function convertTimeToLocal(timeString) {
        const beijingTimeOffset = 8; // Beijing is UTC+8
        const dateInBeijing = new Date(timeString + ' UTC+0800'); // Parse the Beijing time
        return dateInBeijing.toLocaleString(); // Convert to local time string
    }

    // Function to process and replace date strings within an element's text
    function processTextNode(node) {
        const timePattern = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}/g; // Example patterns, modify as needed
        if (node.nodeValue.match(timePattern)) {
            node.nodeValue = node.nodeValue.replace(timePattern, match => convertTimeToLocal(match));
            console.log(`Processed text node: `, node.nodeValue);
        }
    }

    // Function to process and replace date strings within an element's title attribute
    function processTitleAttribute(element) {
        const timePattern = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}/g; // Example patterns, modify as needed
        if (element.title && element.title.match(timePattern)) {
            element.title = element.title.replace(timePattern, match => convertTimeToLocal(match));
            console.log(`Processed title attribute: `, element.title);
        }
    }

    // Observer to handle dynamic content
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === Node.TEXT_NODE) {
                    processTextNode(node);
                } else if (node.nodeType === Node.ELEMENT_NODE) {
                    processTitleAttribute(node);
                    node.querySelectorAll('*').forEach(child => {
                        processTitleAttribute(child);
                        child.childNodes.forEach(childNode => {
                            if (childNode.nodeType === Node.TEXT_NODE) {
                                processTextNode(childNode);
                            }
                        });
                    });
                }
            });
        });
    });

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

    // Initial processing on load
    document.querySelectorAll('time, span, div, p, a, *[title]').forEach(element => {
        processTitleAttribute(element);
        element.childNodes.forEach(node => {
            if (node.nodeType === Node.TEXT_NODE) {
                processTextNode(node);
            }
        });
    });
})();
长期地址
遇到问题?请前往 GitHub 提 Issues。