Stigs Torn Scripts

Adds options to request an assist on the attack page

目前为 2024-06-16 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Stigs Torn Scripts
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2.1
  5. // @description Adds options to request an assist on the attack page
  6. // @author Stig [2648238]
  7. // @match https://www.torn.com/loader.php?sid=attack*
  8. // @grant GM_addStyle
  9. // @grant GM.xmlHttpRequest
  10. // @grant GM_registerMenuCommand
  11. // @connect api.no1irishstig.co.uk
  12. // ==/UserScript==
  13. 'use strict';
  14.  
  15. GM_registerMenuCommand('Select Assist Location', () => addLocID());
  16.  
  17. const source = 'Stigs Script';
  18.  
  19. function getCustomButtons() {
  20. GM.xmlHttpRequest({
  21. method: 'GET',
  22. url: `https:/api.no1irishstig.co.uk/abtns?locid=${localStorage['assistLocID']}`,
  23. headers: {
  24. 'Content-Type': 'application/json',
  25. },
  26. onload: function(response) {
  27. handleResponse(response, 'button');
  28. }
  29. });
  30. }
  31.  
  32. var buttonList = [];
  33. function handleResponse(response, actionType) {
  34. if (response?.status && response.status !== 200) {
  35. var responseText = response.responseText.replace(/^"|"$/g, '');
  36. if (actionType === 'button') {
  37. alert(`Error Code: ${response.status}\nMessage: ${responseText}` || `An unknown error has occurred - Please report this to ${owner} leadership.`);
  38. } else {
  39. this.classList.replace('arb-default', 'arb-red');
  40. setTimeout(() => alert(`Error Code: ${response.status}\nMessage: ${responseText}` || `An unknown error has occurred - Please report this to the Developer.`), 20);
  41. }
  42. return;
  43. }
  44.  
  45. if (actionType === 'button') {
  46. let json = JSON.parse(response.responseText);
  47. for (const [key, value] of Object.entries(json)) {
  48. buttonList.push({name: key, value: value});
  49. }
  50. } else {
  51. this.classList.replace('arb-default', 'arb-green');
  52. }
  53. }
  54.  
  55. if (localStorage['assistLocID'] !== undefined) {
  56. getCustomButtons();
  57. }
  58.  
  59. GM_addStyle(`
  60. .assist-request-top {
  61. justify-content: center !important;
  62. gap: 5px;
  63. flex-wrap: wrap;
  64. padding: 0 5px;
  65. }
  66.  
  67. .assist-request-button {
  68. width: unset !important;
  69. margin: 5px 0;
  70. cursor: pointer;
  71. }
  72.  
  73. .arb-default:hover {
  74. background: #AAAAAA !important;
  75. }
  76.  
  77. :root .dark-mode .arb-default:hover {
  78. background: #666666 !important;
  79. }
  80.  
  81. .arb-green {
  82. background: linear-gradient(180deg,#f8f2eb,#d2e8bc) !important;
  83. cursor: not-allowed !important;
  84. }
  85.  
  86. :root .dark-mode .arb-green {
  87. background: linear-gradient(180deg,#7e9160,#363636) !important;
  88. }
  89.  
  90. .arb-red {
  91. background: linear-gradient(180deg, #f8ebeb, #e8b3b3) !important;
  92. cursor: not-allowed !important;
  93. }
  94.  
  95. :root .dark-mode .arb-red {
  96. background: linear-gradient(180deg, #8e4343, #363636) !important;
  97. }
  98. `);
  99.  
  100. let interval;
  101. let bar = null;
  102.  
  103. window.addEventListener('load', function () {
  104. render();
  105. interval = setInterval(render, 100);
  106. }, false);
  107.  
  108. document.addEventListener('visibilitychange', render);
  109.  
  110.  
  111. function render() {
  112. bar = document.getElementById('assist-request');
  113. if (bar != null) {
  114. clearInterval(interval);
  115. return;
  116. }
  117.  
  118. const header = document.getElementById('react-root');
  119. const element = document.createElement('div');
  120. element.className = 'msg-info-wrap'
  121. header.parentNode.insertBefore(element, header);
  122.  
  123. if (localStorage['assistLocID'] === undefined) {
  124. element.innerHTML = `
  125. <div id='assist-request' class='info-msg-cont border-round m-top10 assist-request-top'>
  126. <div class='info-msg border-round assist-request-top'>
  127. <a class='assist-request-button arb-default msg border-round' tabindex='0' href='#' id='setup_location'>
  128. Set Assist Location
  129. </a>
  130. </div>
  131. </div>
  132. <hr class='page-head-delimiter m-top10 m-bottom10 '>
  133. `;
  134. document.getElementById("setup_location").addEventListener("click", addLocID);
  135. return;
  136. }
  137.  
  138. if (buttonList.length === 0) {
  139. return;
  140. }
  141.  
  142. let buttons = '';
  143. for (const b of buttonList) {
  144. buttons += `
  145. <div class='assist-request-button arb-default msg border-round' tabindex='0' data-assist-type='${b.value}' >
  146. ${b.name}
  147. </div>
  148. `;
  149. }
  150.  
  151. element.innerHTML = `
  152. <div id='assist-request' class='info-msg-cont border-round m-top10 assist-request-top'>
  153. <div class='info-msg border-round assist-request-top'>
  154. ${buttons}
  155. </div>
  156. </div>
  157. <hr class='page-head-delimiter m-top10 m-bottom10 '>
  158. `;
  159. bar = document.getElementById('assist-request');
  160. const buttonSet = document.getElementsByClassName('assist-request-button');
  161. for (let i = 0; i < buttonSet.length; i++) {
  162. buttonSet[i].addEventListener('click', request.bind(buttonSet[i]));
  163. }
  164. }
  165.  
  166.  
  167. async function request() {
  168. const mode = this.getAttribute('data-assist-type');
  169. const res = await fetch(`https://www.torn.com/loader.php?sid=attackData&mode=json&step=poll&user2ID=${window.location.href.match(/user2ID=(\d+)/)[1]}&rfcv=${document.cookie.split("; ").map(c=>c.split("=")).find(([k])=>k==="rfc_v")?.[1]}`, {
  170. headers: {
  171. 'X-Requested-With': 'XMLHttpRequest'
  172. }
  173. });
  174. const obj = await res.json();
  175.  
  176. if (obj.DB.hasOwnProperty('error')) {
  177. setTimeout(() => alert(obj.DB.error), 20);
  178. return;
  179. }
  180.  
  181. GM.xmlHttpRequest({
  182. method: 'POST',
  183. url: 'https://api.no1irishstig.co.uk/request',
  184. headers: {
  185. 'Content-Type': 'application/json',
  186. },
  187. data: JSON.stringify({
  188. 'tornid': obj.DB.attackerUser.userID,
  189. 'username': obj.DB.attackerUser.playername,
  190. 'targetid': obj.DB.defenderUser.userID,
  191. 'targetname': obj.DB.defenderUser.playername,
  192. 'vendor': `${source} ${GM_info.script.version}`,
  193. 'source': source,
  194. 'type': 'Assist',
  195. 'mode': mode,
  196. 'locid': localStorage['assistLocID'],
  197. }),
  198. onload: function(response) {
  199. handleResponse.call(this, response, 'status');
  200. }.bind(this)
  201. });
  202. }
  203.  
  204. function addLocID() {
  205. const locID = prompt("Please provide a Location, this should be provided by your Faction");
  206.  
  207. if (locID) {
  208. localStorage.setItem('assistLocID', locID);
  209. location.reload();
  210. }
  211. }