Time on Tab Title

Display hospital and travel time on tab title.

当前为 2023-12-01 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

    const IF_PARSE_HOSPITAL_TIME = false;


    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)) {
                if (IF_PARSE_HOSPITAL_TIME) {
                    document.title = parseTime(span.textContent) + TEXT_HOSPITAL;

                } else {
                    document.title = 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 Times
        startObservingHospital();
    
    } 



})();