Time on Tab Title

Display travel time on tab title.

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

  1. // ==UserScript==
  2. // @name Time on Tab Title
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description Display travel time on 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. const URL_INDEX = "https://www.torn.com/index.php";
  22. const URL_HOSPITAL = "https://www.torn.com/hospitalview.php";
  23.  
  24. const IF_PARSE_HOSPITAL_TIME = false;
  25.  
  26.  
  27. function parseTime(timeString) {
  28. // Extract hours, minutes, and seconds from the string
  29. var hoursMatch = timeString.match(/(\d+)\s*hour/);
  30. var minutesMatch = timeString.match(/(\d+)\s*minute/);
  31. var secondsMatch = timeString.match(/(\d+)\s*second/);
  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. // 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. // Pad the hours, minutes, and seconds with leading zeros if necessary
  40. hours = hours.toString().padStart(2, '0');
  41. minutes = minutes.toString().padStart(2, '0');
  42. seconds = seconds.toString().padStart(2, '0');
  43. return hours + ':' + minutes + ':' + seconds;
  44. }
  45.  
  46. function startObservingHospital() {
  47. let span = document.getElementById('theCounter');
  48. if (span) {
  49. let observer = new MutationObserver(function(mutations) {
  50. if (document.contains(span)) {
  51. if (IF_PARSE_HOSPITAL_TIME) {
  52. document.title = parseTime(span.textContent) + TEXT_HOSPITAL;
  53.  
  54. } else {
  55. document.title = span.textContent + TEXT_HOSPITAL;
  56. }
  57. } else {
  58. observer.disconnect();
  59. console.log(title + 'Element with id "theCounter" not found. Observer disconnected.');
  60. }
  61. });
  62. observer.observe(span, { characterData: true, childList: true, subtree: true });
  63. } else {
  64. window.setTimeout(startObservingHospital, 500);
  65. }
  66. }
  67.  
  68.  
  69. if (window.location.href === URL_INDEX) {
  70. // For Travel Times
  71.  
  72. let span = document.getElementById('countrTravel');
  73.  
  74. if (span) {
  75. let observer = new MutationObserver(function(mutations) {
  76. if (document.contains(span)) {
  77. document.title = span.textContent + TEXT_TRAVEL
  78. } else {
  79. observer.disconnect();
  80. console.log(title + 'Element with id "countrTravel" not found. Observer disconnected.');
  81. }
  82. });
  83. observer.observe(span, { characterData: true, childList: true, subtree: true });
  84. }
  85. } else if (window.location.href === URL_HOSPITAL){
  86.  
  87. // For Hospital Times
  88. startObservingHospital();
  89. }
  90.  
  91.  
  92.  
  93. })();