Greasy Fork 还支持 简体中文。

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