BCT Defaultor

Set BCT as the default flight method

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         BCT Defaultor
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Set BCT as the default flight method
// @author       Titanic_
// @license      MIT
// @match        https://www.torn.com/page.php?sid=travel*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const bctSelector = 'input[name="travelType"][value="business"]';
    let hasCompleted = false;

    function selectBusinessClass() {
        const bctRadio = document.querySelector(bctSelector);

        if (!bctRadio) return false;

        if (!bctRadio.disabled && !bctRadio.checked) {
            bctRadio.click();
            hasCompleted = true;
            return true;
        }

        if (bctRadio.disabled || bctRadio.checked) {
            hasCompleted = true;
            return true;
        }

        if (bctRadio.disabled && !bctRadio.checked) console.error('[BCT] You dont have any BCT');

        return false;
    }

    const observer = new MutationObserver((mutations, obs) => {
        if (selectBusinessClass()) {
            obs.disconnect();
        }
    });

    const root = document.getElementById('travel-root');
    if (root) {
        observer.observe(root, {
            childList: true,
            subtree: true
        });

        selectBusinessClass();
    } else console.error('[BCT] Couldnt find #travel-root');

    setTimeout(() => {
        if (!hasCompleted) {
            observer.disconnect();
            console.log('[BCT] Timed out');
        }
    }, 10000);

})();