ET Summary Timer

Displays crafting timers in Summary menu. Settings available in console via window.et_summaryTimer

当前为 2019-04-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ET Summary Timer
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.6
  5. // @description Displays crafting timers in Summary menu. Settings available in console via window.et_summaryTimer
  6. // @author Aes Sedai
  7. // @match http*://*.eternitytower.net/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Default settings, use window.et_summaryTimer in console to change settings
  15. // showCrafting: BOOLEAN, default: true; if true, shows a timer for crafting in summary list
  16. // showInscription: BOOLEAN, default: true; if true, shows a timer for inscription in summary list
  17. // showAdventure: BOOLEAN, default: true; if true, shows a timer for adventures in summary list
  18. // showReforging: BOOLEAN, default: true; if true, shows a timer for reforging in summary list
  19. // interval: INTEGER, default: 1000; time in millisecons to wait before refreshing timer
  20. window.et_summaryTimer = {
  21. showCrafting: true,
  22. showInscription: true,
  23. showAdventure: true,
  24. showReforging: true,
  25. interval: 1000
  26. };
  27.  
  28. if(localStorage.getItem('et_summaryTimer')) window.et_summaryTimer = Object.assign({}, window.et_summaryTimer, JSON.parse(localStorage.getItem('et_summaryTimer')));
  29.  
  30. $(window).on("beforeunload", function() {
  31. localStorage.setItem('et_summaryTimer', JSON.stringify(window.et_summaryTimer));
  32. });
  33.  
  34. function getTimeRemaining(endtime) {
  35. let t = Date.parse(endtime) - Date.parse(new Date());
  36. let seconds = Math.floor( (t/1000) % 60 );
  37. let minutes = Math.floor( (t/1000/60) % 60 );
  38. let hours = Math.floor( (t/(1000*60*60)) % 24 );
  39. let days = Math.floor( t/(1000*60*60*24) );
  40. return {
  41. 'total': t,
  42. 'days': days,
  43. 'hours': hours.toString().padStart(2, '0'),
  44. 'minutes': minutes.toString().padStart(2, '0'),
  45. 'seconds': seconds.toString().padStart(2, '0')
  46. };
  47. }
  48.  
  49. function formatTimeRemaining(time) {
  50. if(time.days > 0) return time.days + ":" + time.hours + ":" + time.minutes + ":" + time.seconds;
  51. if(time.hours > 0) return time.hours + ":" + time.minutes + ":" + time.seconds;
  52. if(time.minutes > 0) return time.minutes + ":" + time.seconds;
  53. if(time.seconds > 0) return "00:" + time.seconds;
  54. }
  55.  
  56. function cleanup(summary) {
  57. if($(summary.classSelector).length > 0) $(summary.classSelector).remove();
  58. }
  59.  
  60. let summaries = [
  61. {classSelector: ".crafting-timer", type: 'crafting', redis: Meteor.connection._mongo_livedata_collections.crafting.find(), rowSelector: ".summary-crafting", active: window.et_summaryTimer.showCrafting},
  62. {classSelector: ".inscription-timer", type: 'crafting', redis: Meteor.connection._mongo_livedata_collections.inscription.find(), rowSelector: ".summary-inscription", active: window.et_summaryTimer.showInscription},
  63. {classSelector: ".adventures-timer", type: 'adventure', redis: Meteor.connection._mongo_livedata_collections.adventures.find(), rowSelector: ".summary-adventures", active: window.et_summaryTimer.showAdventure},
  64. {classSelector: ".reforging-timer", type: 'reforging', redis: Meteor.connection._mongo_livedata_collections.crafting.find(), rowSelector: ".summary-reforging", active: window.et_summaryTimer.showReforging},
  65. ];
  66.  
  67. let interval = setInterval(function() {
  68. summaries.forEach(function(summary) {
  69. if(!summary.active) return;
  70. let elems = null;
  71. if(summary.type == 'crafting') {
  72. let fetch = summary.redis.fetch();
  73. if(fetch.length === 0) {
  74. cleanup(summary);
  75. return;
  76. }
  77. let currentlyCrafting = fetch[0].currentlyCrafting;
  78. if (currentlyCrafting.length === 0) {
  79. cleanup(summary);
  80. return;
  81. }
  82. let firstCraft = currentlyCrafting[0];
  83. if(!firstCraft.hasOwnProperty('endDate')) {
  84. cleanup(summary);
  85. return;
  86. }
  87. elems = currentlyCrafting;
  88. } else if(summary.type == 'adventure') {
  89. let fetch = summary.redis.fetch();
  90. if(fetch.length === 0) {
  91. cleanup(summary);
  92. return;
  93. }
  94. elems = fetch[0].adventures.filter((a) => !!a.startDate);
  95. } else if(summary.type == 'reforging') {
  96. let fetch = summary.redis.fetch();
  97. if(fetch.length === 0) {
  98. cleanup(summary);
  99. return;
  100. }
  101. if(!fetch[0].hasOwnProperty('currentlyReforging')) {
  102. cleanup(summary);
  103. return;
  104. }
  105. elems = [fetch[0].currentlyReforging];
  106. }
  107.  
  108. if($(summary.classSelector).length > 0) {
  109. if(elems.length > 1) {
  110. $(summary.classSelector).html(formatTimeRemaining(getTimeRemaining(elems[0].endDate)) + " (" + formatTimeRemaining(getTimeRemaining(elems[elems.length - 1].endDate)) + ")");
  111. } else if(elems.length == 1) {
  112. $(summary.classSelector).html(formatTimeRemaining(getTimeRemaining(elems[0].endDate)));
  113. }
  114. } else if($(summary.rowSelector).length > 0) {
  115. let row = $(summary.rowSelector);
  116. let timeContainer = document.createElement("span");
  117. timeContainer.className = summary.classSelector.replace('.', '');
  118. timeContainer.style.cssText = 'padding-left: 6px;';
  119. if(elems.length > 1) {
  120. timeContainer.innerHTML = formatTimeRemaining(getTimeRemaining(elems[0].endDate) + " (" + formatTimeRemaining(getTimeRemaining(elems[elems.length - 1].endDate)) + ")");
  121. } else if(elems.length == 1) {
  122. timeContainer.innerHTML = formatTimeRemaining(getTimeRemaining(elems[0].endDate));
  123. }
  124.  
  125. row.after(timeContainer);
  126. }
  127. });
  128. }, window.et_summaryTimer.interval);
  129. })();