Military Time Converter and Time Zone Adjuster with Preference

Convert military time to regular time and adjust MST to EST, CST, PST with user preference and storage

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Military Time Converter and Time Zone Adjuster with Preference
// @namespace    RyanCane26
// @version      1.0
// @description  Convert military time to regular time and adjust MST to EST, CST, PST with user preference and storage
// @author       RyanCane26
// @match        https://glb.warriorgeneral.com/game/user*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to convert military time to regular time
    function militaryToRegularTime(militaryTime) {
        let [hours, minutes] = militaryTime.split(':').map(Number);
        const period = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12 || 12; // Convert 0 to 12 for 12 AM
        return `${hours}:${minutes < 10 ? '0' : ''}${minutes} ${period}`;
    }

    // Function to convert MST to other time zones
    function convertMSTToTimeZones(militaryTime) {
        let [hours, minutes] = militaryTime.split(':').map(Number);

        // Create a Date object in MST
        const mstDate = new Date();
        mstDate.setHours(hours, minutes, 0, 0);

        // Function to format the time properly for conversion
        function formatTime(date) {
            const hrs = date.getHours();
            const mins = date.getMinutes();
            return `${hrs < 10 ? '0' : ''}${hrs}:${mins < 10 ? '0' : ''}${mins}`;
        }

        // Convert to EST, CST, PST by adjusting hours
        const estDate = new Date(mstDate.getTime() + (2 * 60 * 60 * 1000)); // MST + 2 hours = EST
        const cstDate = new Date(mstDate.getTime() + (1 * 60 * 60 * 1000)); // MST + 1 hour = CST
        const pstDate = new Date(mstDate.getTime() - (1 * 60 * 60 * 1000)); // MST - 1 hour = PST

        return {
            mst: militaryToRegularTime(formatTime(mstDate)),
            est: militaryToRegularTime(formatTime(estDate)),
            cst: militaryToRegularTime(formatTime(cstDate)),
            pst: militaryToRegularTime(formatTime(pstDate))
        };
    }

    // Function to get the preferred time zone from localStorage
    function getPreferredTimeZone() {
        return localStorage.getItem('preferredTimeZone') || 'mst';
    }

    // Function to set the preferred time zone in localStorage
    function setPreferredTimeZone(timeZone) {
        localStorage.setItem('preferredTimeZone', timeZone);
    }

    // Function to find and convert all military times on the page
    function convertTimesOnPage() {
        const preferredTimeZone = getPreferredTimeZone();
        const timeRegex = /\b([01]?\d|2[0-3]):([0-5]\d)\b/g;
        const textNodes = document.evaluate('//text()[contains(., ":")]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

        for (let i = 0; i < textNodes.snapshotLength; i++) {
            const node = textNodes.snapshotItem(i);
            const originalText = node.nodeValue;
            let newText = originalText;

            const matches = originalText.match(timeRegex);
            if (matches) {
                matches.forEach(militaryTime => {
                    const timeZones = convertMSTToTimeZones(militaryTime);
                    const replacementText = `${militaryTime} (${preferredTimeZone.toUpperCase()}: ${timeZones[preferredTimeZone]})`;
                    newText = newText.replace(militaryTime, replacementText);
                });

                node.nodeValue = newText;
            }
        }
    }

    // Function to create and display the time zone selector
    function createTimeZoneSelector() {
        const timeZoneSelector = document.createElement('select');
        const timeZones = ['mst', 'est', 'cst', 'pst'];
        timeZones.forEach(tz => {
            const option = document.createElement('option');
            option.value = tz;
            option.textContent = tz.toUpperCase();
            if (tz === getPreferredTimeZone()) {
                option.selected = true;
            }
            timeZoneSelector.appendChild(option);
        });

        timeZoneSelector.addEventListener('change', () => {
            setPreferredTimeZone(timeZoneSelector.value);
            window.location.reload(); // Reload the page to apply changes
        });

        timeZoneSelector.style.position = 'fixed';
        timeZoneSelector.style.top = '10px';
        timeZoneSelector.style.right = '10px';
        timeZoneSelector.style.zIndex = '9999';
        document.body.appendChild(timeZoneSelector);
    }

    // Run the function to convert times on page load
    window.addEventListener('load', () => {
        createTimeZoneSelector();
        convertTimesOnPage();
    });

})();