OC - Prevent Joining Low %

Prevents joining OCs if success rate is too low

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         OC - Prevent Joining Low %
// @namespace    Titanic_
// @version      1.3
// @description  Prevents joining OCs if success rate is too low
// @author       Titanic_ [2968477]
// @license      MIT
// @match        https://www.torn.com/factions.php?step=your*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // minimum oc level (disables join btn if oc level is below this)
    const MIN_LEVEL = 5;

    // minimum % for each level (default is used if % is undefined)
    // default must be set to either Red, Orange or Green
    const LEVEL_REQUIREMENTS = {
        7:  70,
        8:  65,
        9:  0,
        10: 0,
        default: "Green"
    };

    const COLOR_ORDER = ["Red", "Orange", "Green"];

    function observe() {
        if (!window.location.href.includes("tab=crimes")) return;
        clearInterval(window.OCCheckInterval);
        window.OCCheckInterval = setInterval(() => {
            if (document.querySelector("[class*=joinButton_]")) process();
        }, 500);
    }

    function process() {
        document.querySelectorAll("#faction-crimes-root div[class*=contentLayer_]:not([data-checked]").forEach(row => {
            const ocLevel = parseInt(row.querySelector("span[class*=levelValue_]")?.textContent)
            if (!ocLevel) return console.warn("Failed to find OC Level");

            row.querySelectorAll("[class*=joinButton_]").forEach(btn => {
                const wrapper = btn.closest("[class*=wrapper]");
                const minSuccessRate = LEVEL_REQUIREMENTS[ocLevel];
                const successRate = parseInt(wrapper?.querySelector("[class*=successChance]")?.textContent);

                if (!successRate) return console.warn("Failed to find OC Success Rate");
                
                btn.disabled = ocLevel < MIN_LEVEL || (minSuccessRate !== undefined
                    ? successRate < minSuccessRate
                    : !COLOR_ORDER.slice(COLOR_ORDER.indexOf(LEVEL_REQUIREMENTS.default)).some(c => wrapper.className.includes(c)));
            });

            row.dataset.checked = "checked";
        });
    }

    window.addEventListener("hashchange", observe);
    observe();
})();