Stigs Torn Scripts

Adds options to request an assist on the attack page

目前为 2023-09-26 提交的版本。查看 最新版本

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