Torn - Big Chain Timer

Makes chain timer bigger and sets hit count red when close to milestone

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Torn - Big Chain Timer
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Makes chain timer bigger and sets hit count red when close to milestone
// @author       ChuckFlorist [3135868]
// @match        https://www.torn.com/factions.php*
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    const chainUrls = [
        "https://www.torn.com/factions.php?step=your&type=1#/",
        "https://www.torn.com/factions.php?step=your&type=1#/war/chain"
    ];
    const SPAN_TOP = "span.chain-box-top-stat";
    const SPAN_COUNT = "span.chain-box-center-stat";
    const SPAN_TIME = "span.chain-box-timeleft";
    const HITS_WARNING = 20; // hits before milestone

    const ranges = [
        { start: "04:00", end: "05:00", color: "#00FF00" }, // Lime
        { start: "03:00", end: "04:00", color: "#FFFF00" }, // Yellow
        { start: "02:00", end: "03:00", color: "#FF9900" }, // Orange
        { start: "00:00", end: "02:00", color: "#FF0000" } // Red
    ];

    const bonusHits = [100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000, 100000];

    function toSeconds(time) {
        const [mins, secs] = time.split(":").map(Number);
        return mins * 60 + secs;
    }

    function timeBasedColour(time) {
        const totalSecs = toSeconds(time);

        for (let range of ranges) {
            const min = toSeconds(range.start);
            const max = toSeconds(range.end);
            if (totalSecs >= min && totalSecs < max) {
                return range.color;
            }
        }
        return "#FF0000"; // default to red
    }

    function updateHitCount(cntSpan) {
        if (cntSpan){
            let nHits = Number(cntSpan.textContent.replace(/,/g, ''));
            let isClose = bonusHits.some(bh => (nHits >= bh - HITS_WARNING && nHits < bh));
            let colr = isClose ? 'red' : '#798C3D'; // red or normal torn green color
            cntSpan.style.setProperty('color', colr, 'important');
            cntSpan.style.setProperty('font-size', '20px', 'important');
        }
    }

    function updateTimer(tmrSpan) {
        if (tmrSpan) {
            let color = timeBasedColour(tmrSpan.textContent);
            tmrSpan.style.setProperty('font-size', '40px', 'important');
            tmrSpan.style.setProperty('color', color, 'important');
        }
    }

    function removeControl(target) {
        if (target) {
            target.remove();
        }
    }

    function runScript() {
        try {
            if (chainUrls.includes(window.location.href)) {
				var tmrSpan = document.querySelectorAll(SPAN_TIME)[0];
				if (toSeconds(tmrSpan.textContent) < 300)
				{
					removeControl(document.querySelectorAll(SPAN_TOP)[0]);
					updateTimer(document.querySelectorAll(SPAN_TIME)[0]);
					updateHitCount(document.querySelectorAll(SPAN_COUNT)[0]);
				}
            }
        } catch (ex) {
            alert(ex.message);
        }
    }

    setInterval(runScript, 500);
})();