Time on Tab Title

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

  1. // ==UserScript==
  2. // @name Time on Tab Title
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.8
  5. // @description Display remaining travel time, hospital time, raceway time, and time left for chain on tab title.
  6. // @author HesperCroft [2924630]
  7. // @match https://www.torn.com/hospitalview.php
  8. // @include https://www.torn.com/loader.php?sid=racing*
  9. // @include https://www.torn.com/factions.php*
  10. // @include https://www.torn.com/page.php?sid=travel*
  11. // @icon
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. const title = "[Time on Tab Title]: ";
  19.  
  20. const TEXT_TRAVEL = " Traveling | TORN";
  21. const TEXT_HOSPITAL = " Hospital | TORN";
  22. const TEXT_RACEWAY = " Racing | TORN";
  23. const TEXT_CHAIN = " Chain | TORN";
  24.  
  25.  
  26. const ID_HOSPITAL = 'theCounter';
  27. const ID_RACEWAY = 'infoSpot';
  28.  
  29. const SPAN_CHAIN_TITLE = 'span.chain-box-title';
  30. const SPAN_CHAIN_LENGTH = 'p.bar-value___uxnah';
  31. const SPAN_CHAIN_TIME = "p.bar-timeleft___B9RGV";
  32.  
  33. const URL_HOSPITAL = "https://www.torn.com/hospitalview.php";
  34. const URL_RACEWAY = "https://www.torn.com/loader.php?sid=racing";
  35. const URL_CHAIN = 'https://www.torn.com/factions.php';
  36. const URL_TRAVEL = "https://www.torn.com/page.php?sid=travel";
  37.  
  38.  
  39. const IF_PARSE_HOSPITAL_TIME = false;
  40. const IF_PARSE_RACEWAY_TIME = false;
  41.  
  42.  
  43. function parseTime(timeString) {
  44. // Extract hours, minutes, and seconds from the string
  45. let hoursMatch = timeString.match(/(\d+)\s*hour/);
  46. let minutesMatch = timeString.match(/(\d+)\s*minute/);
  47. let secondsMatch = timeString.match(/(\d+)\s*second/);
  48.  
  49. // If hours, minutes, or seconds weren't found, default to 0
  50. let hours = hoursMatch ? parseInt(hoursMatch[1], 10) : 0;
  51. let minutes = minutesMatch ? parseInt(minutesMatch[1], 10) : 0;
  52. let seconds = secondsMatch ? parseInt(secondsMatch[1], 10) : 0;
  53.  
  54. // Add any minutes over 60 to the hours and keep the remainder as minutes
  55. hours += Math.floor(minutes / 60);
  56. minutes = minutes % 60;
  57.  
  58. // Pad the hours, minutes, and seconds with leading zeros if necessary
  59. hours = hours.toString().padStart(2, '0');
  60. minutes = minutes.toString().padStart(2, '0');
  61. seconds = seconds.toString().padStart(2, '0');
  62.  
  63. return hours + ':' + minutes + ':' + seconds;
  64. }
  65.  
  66.  
  67. function startObserving(spanId, tabText) {
  68. let span = document.getElementById(spanId);
  69.  
  70.  
  71. if (span) {
  72.  
  73. let observer = new MutationObserver(function(mutations) {
  74. if (document.contains(span)) {
  75. let text = null;
  76. if (spanId === ID_RACEWAY) {
  77. if (span.textContent.startsWith("Race")) {
  78. text = span.textContent;
  79. } else {
  80. text = span.querySelector("span").textContent;
  81. if (IF_PARSE_RACEWAY_TIME) {
  82. text = parseTime(text);
  83. }
  84.  
  85. }
  86. } else {
  87. text = span.textContent;
  88. }
  89.  
  90. // If raceway or hospital check for if parse time is enabled
  91. if (spanId === ID_HOSPITAL && IF_PARSE_HOSPITAL_TIME) {
  92. text = parseTime(text);
  93. } else {
  94. text = span.textContent;
  95. }
  96. let newTitle = text + tabText;
  97. document.title = newTitle;
  98. } else {
  99. observer.disconnect();
  100. console.log(title + 'Element with id "' + spanId + '" not found. Observer disconnected.');
  101. }
  102. });
  103.  
  104. observer.observe(span, { characterData: true, childList: true, subtree: true });
  105. } else {
  106. window.setTimeout(() => startObserving(spanId, tabText), 500);
  107. }
  108.  
  109. }
  110.  
  111. function startObservingChain() {
  112. let span = document.querySelector(SPAN_CHAIN_TIME);
  113. if (span) {
  114.  
  115. let observer = new MutationObserver(function(mutations) {
  116.  
  117. if (document.contains(span)) {
  118.  
  119. let length = document.querySelector(SPAN_CHAIN_LENGTH).textContent;
  120. let time = document.querySelector(SPAN_CHAIN_TIME).textContent;
  121.  
  122. document.title = time + " left " + length + " " + TEXT_CHAIN;
  123.  
  124. } else {
  125. observer.disconnect();
  126. console.log(title + 'Element with class ' + SPAN_CHAIN_TITLE + ' not found or chain inactive. Observer disconnected.');
  127. }
  128. });
  129.  
  130. observer.observe(span, { characterData: true, childList: true, subtree: true });
  131. } else {
  132.  
  133. window.setTimeout(startObservingChain, 500);
  134. }
  135.  
  136.  
  137. }
  138.  
  139.  
  140. function startObservingTravel() {
  141.  
  142. let span = document.querySelector("#travel-root > div.flightProgressSection___fhrD5 > div.progressText___qJFfY > span > span:nth-child(2) > time");
  143. if (span) {
  144. let observer = new MutationObserver(function(mutations) {
  145. if (document.contains(span)) {
  146. let text = span.textContent;
  147.  
  148. document.title = text + TEXT_TRAVEL;
  149.  
  150. } else {
  151. observer.disconnect();
  152. console.log(title + 'Element with id "travel-root" not found. Observer disconnected.');
  153. }
  154. });
  155.  
  156. observer.observe(span, { characterData: true, childList: true, subtree: true });
  157. } else {
  158. window.setTimeout(startObservingTravel, 500);
  159. }
  160.  
  161. }
  162.  
  163.  
  164.  
  165. if (window.location.href === URL_TRAVEL) {
  166. startObservingTravel();
  167.  
  168. } else if (window.location.href === URL_HOSPITAL){
  169. // For Hospital Times
  170. startObserving(ID_HOSPITAL, TEXT_HOSPITAL);
  171.  
  172. } else if (window.location.href.startsWith(URL_RACEWAY)) {
  173. // For Raceway
  174. startObserving(ID_RACEWAY, TEXT_RACEWAY);
  175.  
  176. } else if (window.location.href.startsWith(URL_CHAIN)) {
  177. // For Chain
  178. startObservingChain();
  179. }
  180.  
  181.  
  182.  
  183. })();