Faction Inactivity Highlight

Highlight inactive members of faction.

安装此脚本
作者推荐脚本

您可能也喜欢Company Inactivity Highlight

安装此脚本
  1. // ==UserScript==
  2. // @name Faction Inactivity Highlight
  3. // @namespace https://www.torn.com/profiles.php?XID=1936821
  4. // @version 1.5
  5. // @description Highlight inactive members of faction.
  6. // @author TheFoxMan [1936821]
  7. // @match https://www.torn.com/factions.php*
  8. // @run-at document-end
  9. // @license Apache License 2.0
  10. // ==/UserScript==
  11.  
  12. const APIKEY = "###PDA-APIKEY###";
  13.  
  14. const activityHighlights = [
  15. [1, 1, "#FFFFFF80"],
  16. [2, 4, "#ff990080"],
  17. [5, 6, "#FF000080"],
  18. [7, 999, "#cc00ff80"]
  19. ];
  20.  
  21. // SCRIPT BEYOND.
  22. // DO NOT EDIT.
  23.  
  24. const APICOMMENT = "FactionLastAction";
  25.  
  26. if (!document.find)
  27. Object.defineProperties(Document.prototype, {
  28. find: {
  29. value(selector) {
  30. return document.querySelector(selector);
  31. },
  32. enumerable: false
  33. },
  34. findAll: {
  35. value(selector) {
  36. return document.querySelectorAll(selector);
  37. },
  38. enumerable: false
  39. }
  40. });
  41.  
  42. if (!Element.prototype.find)
  43. Object.defineProperties(Element.prototype, {
  44. find: {
  45. value(selector) {
  46. return this.querySelector(selector);
  47. },
  48. enumerable: false
  49. },
  50. findAll: {
  51. value(selector) {
  52. return this.querySelectorAll(selector);
  53. },
  54. enumerable: false
  55. }
  56. });
  57.  
  58. async function waitFor(sel, parent = document) {
  59. return new Promise((resolve) => {
  60. const intervalID = setInterval(() => {
  61. const el = parent.find(sel);
  62. if (el) {
  63. resolve(el);
  64. clearInterval(intervalID);
  65. }
  66. }, 500);
  67. });
  68. }
  69.  
  70. (() => {
  71. document.head.insertAdjacentHTML(
  72. "beforeend",
  73. `<style>
  74. .faction-info-wrap .members-list .table-body > li::after {
  75. display: block;
  76. content: attr(data-last-action);
  77. color: #fff;
  78. }
  79. </style>`
  80. );
  81.  
  82. if (window.location.href.includes("step=your") && window.location.hash.includes("tab=info")) showLastAction();
  83.  
  84. if (window.location.href.includes("step=profile")) showLastAction();
  85.  
  86. window.addEventListener("hashchange", () => {
  87. if (window.location.href.includes("step=your") && window.location.hash.includes("tab=info")) showLastAction();
  88. });
  89. })();
  90.  
  91. async function showLastAction() {
  92. await waitFor(".faction-info-wrap .members-list .table-body");
  93.  
  94. if (document.find(".faction-info-wrap .members-list .table-body > li[data-last-action]")) return;
  95.  
  96. const factionID = document.find("#view-wars").parentElement.getAttribute("href")?.match(/\d+/)?.[0];
  97. const data = await (await fetch(`https://api.torn.com/faction/${factionID || ""}?selections=basic&key=${APIKEY}&comment=${APICOMMENT}`)).json();
  98.  
  99. document.findAll(".faction-info-wrap .members-list .table-body > li").forEach((row) => {
  100. const profileID = parseInt(row.find("a[href*='profiles.php']").getAttribute("href").split("XID=")[1]);
  101.  
  102. row.setAttribute("data-last-action", data.members[profileID].last_action.relative);
  103.  
  104. const numberOfDays = Math.floor((Date.now() / 1000 - data.members[profileID].last_action.timestamp) / (24 * 60 * 60));
  105.  
  106. let color;
  107. activityHighlights.forEach((rule) => {
  108. if (rule[0] <= numberOfDays && numberOfDays <= rule[1]) color = rule[2];
  109. });
  110.  
  111. if (!color) return;
  112.  
  113. row.style.backgroundColor = color;
  114. });
  115. }