Time on Tab Title

Display remaining hospital or travel time on the tab title.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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();

    }



})();