Pjfry2184575

war time

Nov 17th, 2023
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Show War Local Time
  3. // @namespace    https://www.torn.com/profiles.php?XID=1936821
  4. // @version      1.2
  5. // @description  Shows the war end time in local time.
  6. // @author       TheFoxMan
  7. // @match        https://www.torn.com/factions.php*
  8. // @run-at       document-end
  9. // ==/UserScript==
  10.  
  11. // Made for Phillip_J_Fry [2184575].
  12. // DO NOT EDIT.
  13.  
  14. if (!Document.prototype.find)
  15.     Object.defineProperties(Document.prototype, {
  16.         find: {
  17.             value(selector) {
  18.                 return document.querySelector(selector);
  19.             },
  20.             enumerable: false
  21.         },
  22.         findAll: {
  23.             value(selector) {
  24.                 return document.querySelectorAll(selector);
  25.             },
  26.             enumerable: false
  27.         }
  28.     });
  29.  
  30. if (!Element.prototype.find)
  31.     Object.defineProperties(Element.prototype, {
  32.         find: {
  33.             value(selector) {
  34.                 return this.querySelector(selector);
  35.             },
  36.             enumerable: false
  37.         },
  38.         findAll: {
  39.             value(selector) {
  40.                 return this.querySelectorAll(selector);
  41.             },
  42.             enumerable: false
  43.         }
  44.     });
  45.  
  46. async function waitFor(sel, parent = document) {
  47.     return new Promise((resolve) => {
  48.         const intervalID = setInterval(() => {
  49.             const el = parent.find(sel);
  50.             if (el) {
  51.                 resolve(el);
  52.                 clearInterval(intervalID);
  53.             }
  54.         }, 500);
  55.     });
  56. }
  57.  
  58. (async () => {
  59.     showWarTimes();
  60.  
  61.     window.addEventListener("hashchange", showWarTimes);
  62. })();
  63.  
  64. async function showWarTimes() {
  65.     if (window.location.hash.includes("tab="))
  66.         return;
  67.  
  68.     const warList = await waitFor("#faction_war_list_id");
  69.  
  70.     if (window.location.hash.includes("tab="))
  71.         return;
  72.  
  73.     warList.findAll("[class*='warListItem__']").forEach((war) => {
  74.         if (!war.find("[data-warid]")) return;
  75.  
  76.         const bottomDiv = war.find("[class*='bottomBox__']");
  77.         const timeLeft = parseTime(bottomDiv.textContent);
  78.         bottomDiv.insertAdjacentHTML("beforeend", "<div>" + (new Date(Date.now() + timeLeft)).toLocaleString() + "</div>");
  79.     });
  80. }
  81.  
  82. function parseTime(str) {
  83.     const splits = str.split(":").map(x => parseInt(x));
  84.     let time = 0;
  85.     time += splits[0] * 24 * 60 * 60;
  86.     time += splits[1] * 60 * 60;
  87.     time += splits[2] * 60;
  88.     time += splits[3];
  89.     time = time * 1000;
  90.     return time;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment