Map Controls Hide

Toggle the display of the Torn map controls

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Map Controls Hide
// @namespace    https://lazerpent.com
// @version      1.0
// @description  Toggle the display of the Torn map controls
// @author       Lazerpent
// @match        https://www.torn.com/city.php*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const stateKey = 'hide-controls';

    const style = document.createElement('style');
    style.textContent = `
        .hidden-container .leaflet-control-container {
            display: none;
        }
    `;
    document.head.appendChild(style);

    function injectSwitch() {
        const mapContainer = document.getElementById('map-cont');
        if (mapContainer && !document.getElementById('controls-switcher')) {
            const toggleHTML = document.createElement('div');
            toggleHTML.className = 'territory-info-toggle white-grad border-round m-top10 p10 t-gray-6';
            toggleHTML.innerHTML = `
                <div class="info">
                    <span class="inactive-mode hide">
                        Enable to hide map controls.
                    </span>
                    <span class="active-mode">
                        Map controls are being hidden.
                    </span>
                </div>
                <div class="btn-toggle-wrap torn-switcher">
                    <input type="checkbox" id="controls-switcher" switch="" class="active">
                    <label for="controls-switcher" data-on-label="on" data-off-label="off"></label>
                </div>`;
            mapContainer.appendChild(toggleHTML);

            const switcher = toggleHTML.querySelector('#controls-switcher');

            const savedState = localStorage.getItem(stateKey);
            const isHidden = savedState === 'true';
            switcher.checked = isHidden;
            applyState(isHidden, mapContainer, toggleHTML);

            switcher.addEventListener('change', function() {
                const newState = this.checked;
                localStorage.setItem(stateKey, newState);
                applyState(newState, mapContainer, toggleHTML);
            });
        }
    }

    function applyState(isHidden, mapContainer, toggleHTML) {
        if (isHidden) {
            mapContainer.classList.add('hidden-container');
            toggleHTML.querySelector('.inactive-mode').classList.add('hide');
            toggleHTML.querySelector('.active-mode').classList.remove('hide');
        } else {
            mapContainer.classList.remove('hidden-container');
            toggleHTML.querySelector('.inactive-mode').classList.remove('hide');
            toggleHTML.querySelector('.active-mode').classList.add('hide');
        }
    }

    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList' || mutation.type === 'subtree') {
                injectSwitch();
            }
        });
    });

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

    injectSwitch();
})();