Show War Local Time

Shows the war end time in local time.

  1. // ==UserScript==
  2. // @name Show War Local Time
  3. // @namespace https://www.torn.com/profiles.php?XID=1936821
  4. // @version 1.6
  5. // @description Shows the war end time in local time.
  6. // @author TheFoxMan
  7. // @owner Phillip_J_Fry [2184575]
  8. // @license Apache License 2.0
  9. // @match https://www.torn.com/factions.php*
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. // Made for Phillip_J_Fry [2184575].
  14. // DO NOT EDIT.
  15.  
  16. if (!Document.prototype.find)
  17. Object.defineProperties(Document.prototype, {
  18. find: {
  19. value(selector) {
  20. return document.querySelector(selector);
  21. },
  22. enumerable: false
  23. },
  24. findAll: {
  25. value(selector) {
  26. return document.querySelectorAll(selector);
  27. },
  28. enumerable: false
  29. }
  30. });
  31.  
  32. if (!Element.prototype.find)
  33. Object.defineProperties(Element.prototype, {
  34. find: {
  35. value(selector) {
  36. return this.querySelector(selector);
  37. },
  38. enumerable: false
  39. },
  40. findAll: {
  41. value(selector) {
  42. return this.querySelectorAll(selector);
  43. },
  44. enumerable: false
  45. }
  46. });
  47.  
  48. async function waitFor(sel, parent = document) {
  49. return new Promise((resolve) => {
  50. const intervalID = setInterval(() => {
  51. const el = parent.find(sel);
  52. if (el) {
  53. resolve(el);
  54. clearInterval(intervalID);
  55. }
  56. }, 500);
  57. });
  58. }
  59.  
  60. (async () => {
  61. showWarTimes();
  62.  
  63. window.addEventListener("hashchange", showWarTimes);
  64. })();
  65.  
  66. async function showWarTimes() {
  67. if (window.location.hash.includes("tab=")) return;
  68.  
  69. const warList = await waitFor("#faction_war_list_id");
  70.  
  71. if (window.location.hash.includes("tab=")) return;
  72.  
  73. document.findAll(".war-end-time").forEach((x) => x.remove());
  74.  
  75. warList.findAll("[class*='warListItem__']").forEach((war) => {
  76. if (war.find(".timer")) {
  77. // Territory War
  78. const timer = war.find(".timer");
  79. const timeLeft = parseTime(timer.textContent);
  80. let date = Date.now();
  81. // date -= timeLeft;
  82. date += timeLeft;
  83. // date += 3 * 24 * 60 * 60 * 1000;
  84. date = (new Date(date)).toLocaleString()
  85. timer.insertAdjacentHTML("afterend", "<div class='war-end-time'>" + date + "</div>");
  86. return;
  87. }
  88.  
  89. // RW
  90. if (!war.find("[data-warid]")) return;
  91.  
  92. const bottomDiv = war.find("[class*='bottomBox__']");
  93. const timer = bottomDiv.find("[class*='timer__']");
  94. if (bottomDiv.textContent.includes("WINNER")) return;
  95.  
  96. if (!parseTime(bottomDiv.textContent)) return;
  97.  
  98. // console.log(Date.now() - parseTime(timer.textContent));
  99. const date = (new Date(Date.now() - parseTime(timer.textContent) + 123 * 60 * 60 * 1000)).toLocaleString();
  100. bottomDiv.insertAdjacentHTML(
  101. "beforeend",
  102. "<div class='war-end-time'>" + date + "</div>"
  103. );
  104. });
  105. }
  106.  
  107. function parseTime(str) {
  108. const splits = str.split(":").map((x) => parseInt(x));
  109. // console.log(splits);
  110. let time = 0;
  111. time += splits[0] * 1000 * 60 * 60 * 24;
  112. time += splits[1] * 1000 * 60 * 60;
  113. time += splits[2] * 1000 * 60;
  114. time += splits[3] * 1000;
  115. return time;
  116. }