Time on Tab Title

Display remaining hospital or travel time on the tab title.

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

  1. // ==UserScript==
  2. // @name Time on Tab Title
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Display remaining hospital or travel time on the tab title.
  6. // @author HesperCroft [2924630]
  7. // @match https://www.torn.com/*
  8. // @icon
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. const title = "[Time on Tab Title]: ";
  16.  
  17. const TEXT_TRAVEL = " Traveling | TORN";
  18. // const TEXT_TRAVEL = " Keep Calm & Code On.";
  19. const TEXT_HOSPITAL = " Hospital | TORN";
  20.  
  21. const URL_INDEX = "https://www.torn.com/index.php";
  22. const URL_HOSPITAL = "https://www.torn.com/hospitalview.php";
  23.  
  24.  
  25. function parseTime(timeString) {
  26. // Extract hours, minutes, and seconds from the string
  27. var hoursMatch = timeString.match(/(\d+)\s*hour/);
  28. var minutesMatch = timeString.match(/(\d+)\s*minute/);
  29. var secondsMatch = timeString.match(/(\d+)\s*second/);
  30.  
  31. // If hours, minutes, or seconds weren't found, default to 0
  32. var hours = hoursMatch ? parseInt(hoursMatch[1], 10) : 0;
  33. var minutes = minutesMatch ? parseInt(minutesMatch[1], 10) : 0;
  34. var seconds = secondsMatch ? parseInt(secondsMatch[1], 10) : 0;
  35.  
  36. // Add any minutes over 60 to the hours and keep the remainder as minutes
  37. hours += Math.floor(minutes / 60);
  38. minutes = minutes % 60;
  39.  
  40. // Pad the hours, minutes, and seconds with leading zeros if necessary
  41. hours = hours.toString().padStart(2, '0');
  42. minutes = minutes.toString().padStart(2, '0');
  43. seconds = seconds.toString().padStart(2, '0');
  44.  
  45. return hours + ':' + minutes + ':' + seconds;
  46. }
  47.  
  48. function startObservingHospital() {
  49. let span = document.getElementById('theCounter');
  50.  
  51. if (span) {
  52. let observer = new MutationObserver(function(mutations) {
  53. if (document.contains(span)) {
  54. document.title = parseTime(span.textContent) + TEXT_HOSPITAL;
  55. } else {
  56. observer.disconnect();
  57. console.log(title + 'Element with id "theCounter" not found. Observer disconnected.');
  58. }
  59. });
  60.  
  61. observer.observe(span, { characterData: true, childList: true, subtree: true });
  62. } else {
  63.  
  64. window.setTimeout(startObservingHospital, 500);
  65. }
  66. }
  67.  
  68.  
  69.  
  70.  
  71. if (window.location.href === URL_INDEX) {
  72. // For Travel Times
  73.  
  74. let span = document.getElementById('countrTravel');
  75.  
  76. if (span) {
  77. let observer = new MutationObserver(function(mutations) {
  78.  
  79. if (document.contains(span)) {
  80. document.title = span.textContent + TEXT_TRAVEL
  81.  
  82. } else {
  83. observer.disconnect();
  84. console.log(title + 'Element with id "countrTravel" not found. Observer disconnected.');
  85. }
  86. });
  87.  
  88. observer.observe(span, { characterData: true, childList: true, subtree: true });
  89. }
  90.  
  91.  
  92. } else if (window.location.href === URL_HOSPITAL){
  93. // For Hospital
  94. startObservingHospital();
  95.  
  96. }
  97.  
  98.  
  99.  
  100. })();