Greasy Fork is available in English.

Time on Tab Title

Display remaining travel time, hospital time, raceway time, and time left for chain on tab title.

// ==UserScript==
// @name         Time on Tab Title
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Display remaining travel time, hospital time, raceway time, and time left for chain on tab title.
// @author       HesperCroft [2924630]
// @match   https://www.torn.com/index.php
// @match   https://www.torn.com/hospitalview.php
// @include   https://www.torn.com/loader.php?sid=racing*
// @include   https://www.torn.com/factions.php*
// @icon
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const title = "[Time on Tab Title]: ";

    const TEXT_TRAVEL = " Traveling | TORN";
    // const TEXT_TRAVEL = " Keep Calm & Code On.";
    const TEXT_HOSPITAL = " Hospital | TORN";
    const TEXT_RACEWAY = " Racing | TORN";
    const TEXT_CHAIN = " Chain | TORN";

    const ID_TRAVEL = 'countrTravel';
    const ID_HOSPITAL = 'theCounter';
    const ID_RACEWAY = 'infoSpot';

    const SPAN_CHAIN_TITLE = 'span.chain-box-title';
    const IF_CHAIN_TITLE_ACTIVE = 'Chain active';
    // const SPAN_CHAIN_LENGTH = 'span.chain-box-center-stat'
    const SPAN_CHAIN_LENGTH = 'p.bar-value___uxnah';
    // const SPAN_CHAIN_TIME = "span.chain-box-timeleft";
    const SPAN_CHAIN_TIME = "p.bar-timeleft___B9RGV";

    const URL_INDEX = "https://www.torn.com/index.php";
    const URL_HOSPITAL = "https://www.torn.com/hospitalview.php";
    const URL_RACEWAY = "https://www.torn.com/loader.php?sid=racing";
    // const URL_CHAIN = "https://www.torn.com/factions.php?step=your&type=1";
    const URL_CHAIN = 'https://www.torn.com/factions.php';

    const IF_PARSE_HOSPITAL_TIME = false;
    const IF_PARSE_RACEWAY_TIME = false;


    function parseTime(timeString) {
        // Extract hours, minutes, and seconds from the string
        let hoursMatch = timeString.match(/(\d+)\s*hour/);
        let minutesMatch = timeString.match(/(\d+)\s*minute/);
        let secondsMatch = timeString.match(/(\d+)\s*second/);

        // If hours, minutes, or seconds weren't found, default to 0
        let hours = hoursMatch ? parseInt(hoursMatch[1], 10) : 0;
        let minutes = minutesMatch ? parseInt(minutesMatch[1], 10) : 0;
        let seconds = secondsMatch ? parseInt(secondsMatch[1], 10) : 0;

        // Add any minutes over 60 to the hours and keep the remainder as minutes
        hours += Math.floor(minutes / 60);
        minutes = minutes % 60;

        // Pad the hours, minutes, and seconds with leading zeros if necessary
        hours = hours.toString().padStart(2, '0');
        minutes = minutes.toString().padStart(2, '0');
        seconds = seconds.toString().padStart(2, '0');

        return hours + ':' + minutes + ':' + seconds;
    }


    function startObserving(spanId, tabText) {
        let span = document.getElementById(spanId);


        if (span) {

            let observer = new MutationObserver(function(mutations) {
                if (document.contains(span)) {
                    let text = null;
                    if (spanId === ID_RACEWAY) {
                        if (span.textContent.startsWith("Race")) {
                            text = span.textContent;
                        } else {
                            text = span.querySelector("span").textContent;
                            if (IF_PARSE_RACEWAY_TIME) {
                                text = parseTime(text);
                            }

                        }
                    } else {
                        text = span.textContent;
                    }

                    // If raceway or hospital check for if parse time is enabled
                    if (spanId === ID_HOSPITAL && IF_PARSE_HOSPITAL_TIME) {
                        text = parseTime(text);
                    } else {
                        text = span.textContent;
                    }
                    let newTitle = text + tabText;
                    document.title = newTitle;
                } else {
                    observer.disconnect();
                    console.log(title + 'Element with id "' + spanId + '" not found. Observer disconnected.');
                }
            });

            observer.observe(span, { characterData: true, childList: true, subtree: true });
        } else {
            window.setTimeout(() => startObserving(spanId, tabText), 500);
        }

    }

    function startObservingChain() {
        let span = document.querySelector(SPAN_CHAIN_TIME);
        if (span) {

            let observer = new MutationObserver(function(mutations) {

                if (document.contains(span)) {

                    let length = document.querySelector(SPAN_CHAIN_LENGTH).textContent;
                    let time = document.querySelector(SPAN_CHAIN_TIME).textContent;

                    document.title = time + " left " + length + " " + TEXT_CHAIN;

                } else {
                    observer.disconnect();
                    console.log(title + 'Element with class ' + SPAN_CHAIN_TITLE + ' not found or chain inactive. Observer disconnected.');
                }
            });

            observer.observe(span, { characterData: true, childList: true, subtree: true });
        } else {

            window.setTimeout(startObservingChain, 500);
        }


    }



    if (window.location.href === URL_INDEX) {
        // For Travel Times. Electing not to use a timeout for this one for optimization reasons.

        let span = document.getElementById(ID_TRAVEL);

        if (span) {
            let observer = new MutationObserver(function(mutations) {

                if (document.contains(span)) {
                    document.title = span.textContent + TEXT_TRAVEL

                } else {
                    observer.disconnect();
                    console.log(title + 'Element with id "countrTravel" not found. Observer disconnected.');
                }
            });

            observer.observe(span, { characterData: true, childList: true, subtree: true });
        }


    } else if (window.location.href === URL_HOSPITAL){
        // For Hospital Times
        startObserving(ID_HOSPITAL, TEXT_HOSPITAL);

    } else if (window.location.href.startsWith(URL_RACEWAY)) {
        // For Raceway
        startObserving(ID_RACEWAY, TEXT_RACEWAY);

    } else if (window.location.href.startsWith(URL_CHAIN)) {
        // For Chain
        startObservingChain();
    }



})();