Time on Tab Title

Display remaining hospital or travel time on the tab title.

目前為 2023-12-01 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Time on Tab Title
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Display remaining hospital or travel time on the tab title.
// @author       HesperCroft [2924630]
// @match        https://www.torn.com/*
// @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 URL_INDEX = "https://www.torn.com/index.php";
    const URL_HOSPITAL = "https://www.torn.com/hospitalview.php";


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

        // If hours, minutes, or seconds weren't found, default to 0
        var hours = hoursMatch ? parseInt(hoursMatch[1], 10) : 0;
        var minutes = minutesMatch ? parseInt(minutesMatch[1], 10) : 0;
        var 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 startObservingHospital() {
        let span = document.getElementById('theCounter');

        if (span) {
        let observer = new MutationObserver(function(mutations) {
            if (document.contains(span)) {
                document.title = parseTime(span.textContent) + TEXT_HOSPITAL;
            } else {
                observer.disconnect();
                console.log(title + 'Element with id "theCounter" not found. Observer disconnected.');
            }
        });

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

            window.setTimeout(startObservingHospital, 500);
        }
    }




    if (window.location.href === URL_INDEX) {
        // For Travel Times

        let span = document.getElementById('countrTravel');

        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
        startObservingHospital();

    }



})();