Revive Notifications

Displays revive requests inside Torn

当前为 2024-06-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Revive Notifications
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.0
  5. // @description Displays revive requests inside Torn
  6. // @match https://www.torn.com/*
  7. // @connect api.no1irishstig.co.uk
  8. // @run-at document-end
  9. // ==/UserScript==
  10.  
  11. "use strict";
  12.  
  13. console.log("Revive Notifications script loaded");
  14.  
  15. (function() {
  16. fetchRevives();
  17. setInterval(fetchRevives, 5000); // 5 seconds interval
  18.  
  19. document.addEventListener("visibilitychange", () => {
  20. console.log("Visibility changed, fetching revives...");
  21. fetchRevives();
  22. });
  23.  
  24. function fetchRevives() {
  25. console.log("Fetching revives...");
  26. const apiKey = localStorage.getItem("api_key");
  27. if (!apiKey) {
  28. console.log("No API key found");
  29. promptForApiKey();
  30. displayMessage("Please provide an API key to link - click the \"Setup API Key\" button.");
  31. return;
  32. }
  33. function promptForApiKey() {
  34. const apiKey = prompt("Please enter your API key:");
  35. if (apiKey) {
  36. localStorage.setItem("api_key", apiKey);
  37. fetchRevives();
  38. } else {
  39. displayMessage("Please provide an API key to link - refresh the page to try again.");
  40. }
  41. }
  42. const requestOptions = {
  43. method: "GET",
  44. headers: {
  45. "Content-Type": "application/json",
  46. },
  47. };
  48.  
  49. const url = `https://api.no1irishstig.co.uk/fetch?key=${apiKey}&vendor=The%20Wolverines%20Script%201.0&source=Script&type=revive`;
  50.  
  51. fetch(url, requestOptions)
  52. .then(response => {
  53. if (!response.ok) {
  54. throw new Error(`HTTP error! status: ${response.status}`);
  55. }
  56. return response.json();
  57. })
  58. .then(data => handleApiResponse({ status: 200, responseText: JSON.stringify(data) }))
  59. .catch(error => {
  60. console.error("Fetch error:", error);
  61. displayMessage(`Fetch error: ${error.message}`);
  62. });
  63. }
  64.  
  65. function handleApiResponse(response) {
  66. console.log("Handling API response", response);
  67. if (response.status && response.status !== 200) {
  68. displayMessage(`Error: ${response.status}`);
  69. return;
  70. }
  71.  
  72. const data = JSON.parse(response.responseText);
  73. console.log("API data received", data);
  74. const randoms = [];
  75. const contracts = [];
  76.  
  77. data.forEach(item => {
  78. if (item.contract) {
  79. contracts.push(item);
  80. } else {
  81. randoms.push(item);
  82. }
  83. });
  84.  
  85. displayRevives("revives-randoms", randoms, "Revive Requests", "rev-blue");
  86. displayRevives("revives-contracts", contracts, "Contract Requests", "rev-red");
  87. }
  88.  
  89. function displayRevives(elementId, revives, title, colorClass) {
  90. console.log(`Displaying revives in ${elementId}`, revives);
  91. const container = ensureContainer(elementId, title, colorClass);
  92. if (!container) return;
  93. let html = "";
  94.  
  95. if (revives.length === 0) {
  96. container.innerHTML = "None";
  97. } else {
  98. revives.forEach(user => {
  99. html += `
  100. <a href="https://www.torn.com/profiles.php?XID=${user.id}">
  101. ${user.name}&nbsp;[${user.id}]
  102. </a> |
  103. `;
  104. });
  105. container.innerHTML = html.slice(0, -2); // Remove the last ' | '
  106. }
  107. }
  108.  
  109. function displayMessage(message) {
  110. console.log("Displaying message:", message);
  111. const contractsContainer = ensureContainer("revives-contracts", "Contract Requests", "rev-red");
  112. const randomsContainer = ensureContainer("revives-randoms", "Revive Requests", "rev-blue");
  113. if (contractsContainer) contractsContainer.innerHTML = message;
  114. if (randomsContainer) randomsContainer.innerHTML = message;
  115. }
  116.  
  117. function ensureContainer(id, title, colorClass) {
  118. let container = document.getElementById(id);
  119. if (!container) {
  120. const div = document.createElement("div");
  121. div.className = `info-msg-cont ${colorClass} border-round m-top10`;
  122. div.innerHTML = `
  123. <div class="info-msg border-round">
  124. <div class="delimiter">
  125. <div class="msg right-round" tabindex="0" style="width: unset;">
  126. ${title}: <span id="${id}">Loading</span>
  127. </div>
  128. </div>
  129. </div>
  130. `;
  131. const targetElement = document.querySelector(".content-wrapper.spring[role='main']");
  132. if (targetElement) {
  133. targetElement.insertAdjacentElement('afterbegin', div);
  134. container = document.getElementById(id);
  135. }
  136. }
  137. return container;
  138. }
  139.  
  140. // Add custom styles for colors
  141. const style = document.createElement("style");
  142. style.innerHTML = `
  143. .rev-blue, .rev-red {
  144. background: linear-gradient(to bottom, #1d9da8 0%, #1da89a 100%) !important;
  145. margin: 10px 0;
  146. padding: 1px;
  147. }
  148. .rev-red {
  149. background: linear-gradient(to bottom, #a81d34 0%, #821729 100%) !important;
  150. }
  151. .info-msg-cont {
  152. width: calc(100% - 40px);
  153. margin-left: 20px;
  154. margin-right: 20px;
  155. }
  156. `;
  157. document.head.appendChild(style);
  158. })();