Torn OC Travel Restrictions

Disables travel for individual countries based on flight type if you would be late for an organized crime. Includes a button to enable or disable the script. Combines PC and mobile view versions.

目前为 2024-12-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         Torn OC Travel Restrictions
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Disables travel for individual countries based on flight type if you would be late for an organized crime. Includes a button to enable or disable the script. Combines PC and mobile view versions.
// @author       Baccy
// @match        https://www.torn.com/travelagency.php
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @grant        none
// ==/UserScript==

(function() {

	let mobileView;
	if (document.querySelector('.travel-info-table-list.ui-accordion-header.ui-helper-reset.ui-state-default.ui-corner-all.ui-accordion-icons')) mobileView = true;

    const travelTimes = {
        "tab4-1": { mexico: 1560000 * 2, cayman: 2100000 * 2, canada: 2460000 * 2, hawaii: 8040000 * 2, uk: 9540000 * 2, argentina: 10020000 * 2, switzerland: 10500000 * 2, japan: 13500000 * 2, china: 14520000 * 2, uae: 16260000 * 2, "south-africa": 17820000 * 2 },
        "tab4-2": { mexico: 1080000 * 2, cayman: 1500000 * 2, canada: 1740000 * 2, hawaii: 5640000 * 2, uk: 6660000 * 2, argentina: 7020000 * 2, switzerland: 7380000 * 2, japan: 9480000 * 2, china: 10140000 * 2, uae: 11400000 * 2, "south-africa": 12480000 * 2 },
        "tab4-3": { mexico: 780000 * 2, cayman: 1080000 * 2, canada: 1200000 * 2, hawaii: 4020000 * 2, uk: 4800000 * 2, argentina: 4980000 * 2, switzerland: 5280000 * 2, japan: 6780000 * 2, china: 7260000 * 2, uae: 8100000 * 2, "south-africa": 8940000 * 2 },
        "tab4-4": { mexico: 480000 * 2, cayman: 660000 * 2, canada: 720000 * 2, hawaii: 2400000 * 2, uk: 2880000 * 2, argentina: 3000000 * 2, switzerland: 3180000 * 2, japan: 4080000 * 2, china: 4320000 * 2, uae: 4860000 * 2, "south-africa": 5340000 * 2 }
    };

    let isEnabled = JSON.parse(localStorage.getItem('ocTravelRestriction')) ?? true;

    const toggleButton = document.createElement('button');
    toggleButton.innerText = isEnabled ? 'Disable Travel Restriction' : 'Enable Travel Restriction';
    toggleButton.style = 'padding: 5px 10px; border-radius: 5px; background-color: #555555; color: white; border: none; cursor: pointer;';
    toggleButton.onmouseover = () => { toggleButton.style.backgroundColor = '#444444'; };
    toggleButton.onmouseout = () => { toggleButton.style.backgroundColor = '#555555'; };
    toggleButton.onclick = () => {
        isEnabled = !isEnabled;
        localStorage.setItem('ocTravelRestriction', isEnabled);
        toggleButton.innerText = isEnabled ? 'Disable Travel Restriction' : 'Enable Travel Restriction';
        if (isEnabled) applyTravelRestrictions();
        else if (!isEnabled) enableFlights();
    };
    if ($('div.content-title > h4').length > 0) $('div.content-title > h4').append(toggleButton);

    if (isEnabled) applyTravelRestrictions();

    function applyTravelRestrictions() {
        const warningElements = document.querySelectorAll('.t-red');
        if (warningElements.length === 0) {
            console.warn("No warning messages found.");
            return;
        }

        let validWarningFound = false;

        warningElements.forEach(warningElement => {
            const warningText = warningElement.innerText.trim();
            if (warningText.startsWith("Warning: An organized crime you're participating in will be ready in")) {
                validWarningFound = true;

                const timeMatch = warningText.match(/(\d+) hours? and (\d+) minutes?/);
                if (!timeMatch) {
                    console.error("Failed to parse time from warning: ", warningText);
                    return;
                }

                const hours = parseInt(timeMatch[1], 10);
                const minutes = parseInt(timeMatch[2], 10);
                const remainingTime = (hours * 60 + minutes) * 60 * 1000;

                disableFlights(remainingTime);
            } else if (warningText.startsWith("Warning: An organized crime you're participating in is ready for initiation")) {
                validWarningFound = true;
                disableFlights(Date.now());
            } else {
                disableFlights(12480000);
            }
        });

        if (!validWarningFound) console.warn("No relevant organized crime warnings found.");
    }

    function disableFlights(remainingTime) {
        Object.keys(travelTimes).forEach(tabId => {

            const travelTab = document.getElementById(tabId);
            const tabTravelTimes = travelTimes[tabId];

            if (travelTab && tabTravelTimes) {
                Object.entries(tabTravelTimes).forEach(([location, time]) => {
                    if (time > remainingTime) {
						if (mobileView) {
							const cityFlag = travelTab.querySelector(`.city-flag.${location}`);
							if (cityFlag) {
								const travelOption = cityFlag.closest('.travel-info-table-list.ui-accordion-header.ui-helper-reset.ui-state-default.ui-corner-all.ui-accordion-icons');
								if (travelOption) travelOption.setAttribute('style', 'display: none;');
							}
						} else if (!mobileView) {
							const button = travelTab.querySelector(`.raceway.${location}`);
							if (button) {
								button.style.pointerEvents = 'none';
								button.style.opacity = '0.5';
							}
						}
                    }
                });
            }
        });
        observer.disconnect(); 
    }

    function enableFlights() {
		if (mobileView) {
			document.querySelectorAll('.travel-info-table-list.ui-accordion-header.ui-helper-reset.ui-state-default.ui-corner-all.ui-accordion-icons').forEach((travelOption) => {
				travelOption.setAttribute('style', '');
			});
		} else {
			document.querySelectorAll('.raceway').forEach(button => {
				button.style.pointerEvents = 'auto';
				button.style.opacity = '1';
			});
		}
    }

    const observer = new MutationObserver((mutationsList, observer) => {
        const pcTravelButtons = document.querySelectorAll('.raceway');
		const mobileTravelButtons = document.querySelectorAll('.travel-info-table-list.ui-accordion-header.ui-helper-reset.ui-state-default.ui-corner-all.ui-accordion-icons');
        if (pcTravelButtons.length > 0 ) {
			mobileView = false;
            if (isEnabled) applyTravelRestrictions();
		} else if (mobileTravelButtons.length > 0) {
			mobileView = true;
			if (isEnabled) applyTravelRestrictions();
		}
    });

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

QingJ © 2025

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