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.11
  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.  
  105. const header = document.getElementById('react-root');
  106. const element = document.createElement('div');
  107. element.className = 'msg-info-wrap'
  108. header.parentNode.insertBefore(element, header);
  109.  
  110. if (localStorage['assistLocID'] === undefined) {
  111. element.innerHTML = `
  112. <div id='assist-request' class='info-msg-cont border-round m-top10 assist-request-top'>
  113. <div class='info-msg border-round assist-request-top'>
  114. <a class='assist-request-button arb-default msg border-round' tabindex='0' href='#' id='setup_location'>
  115. Set Assist Location
  116. </a>
  117. </div>
  118. </div>
  119. <hr class='page-head-delimiter m-top10 m-bottom10 '>
  120. `;
  121. document.getElementById("setup_location").addEventListener("click", addLocID);
  122. return;
  123. }
  124.  
  125. if (buttonList.length === 0) {
  126. return;
  127. }
  128.  
  129. let buttons = '';
  130. for (const b of buttonList) {
  131. buttons += `
  132. <div class='assist-request-button arb-default msg border-round' tabindex='0' data-assist-type='${b.value}' >
  133. ${b.name}
  134. </div>
  135. `;
  136. }
  137.  
  138. element.innerHTML = `
  139. <div id='assist-request' class='info-msg-cont border-round m-top10 assist-request-top'>
  140. <div class='info-msg border-round assist-request-top'>
  141. ${buttons}
  142. </div>
  143. </div>
  144. <hr class='page-head-delimiter m-top10 m-bottom10 '>
  145. `;
  146. bar = document.getElementById('assist-request');
  147. const buttonSet = document.getElementsByClassName('assist-request-button');
  148. for (let i = 0; i < buttonSet.length; i++) {
  149. buttonSet[i].addEventListener('click', request.bind(buttonSet[i]));
  150. }
  151. }
  152.  
  153.  
  154. async function request() {
  155. const mode = this.getAttribute('data-assist-type');
  156. 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"}});
  157. const obj = await res.json();
  158.  
  159. if (obj.DB.hasOwnProperty('error')) {
  160. setTimeout(() => alert(obj.DB.error), 20);
  161. return;
  162. }
  163.  
  164. GM.xmlHttpRequest({
  165. method: 'POST',
  166. url: 'https://api.no1irishstig.co.uk/request',
  167. headers: {
  168. 'Content-Type': 'application/json',
  169. },
  170. data: JSON.stringify({
  171. 'tornid': obj.DB.attackerUser.playername,
  172. 'username': obj.DB.attackerUser.playername,
  173. 'targetid': obj.DB.defenderUser.userID,
  174. 'targetname': obj.DB.defenderUser.playername,
  175. 'vendor': `${source} ${GM_info.script.version}`,
  176. 'source': source,
  177. 'type': 'Assist',
  178. 'mode': mode,
  179. 'locID': localStorage['assistLocID'],
  180. }),
  181. onload: handleResponse.bind(this),
  182. });
  183. }
  184.  
  185. function handleResponse(response) {
  186. const errors = {
  187. 401: 'Request denied - Please contact leadership.',
  188. 429: 'You have already submitted a request.',
  189. 499: 'Outdated Script - Please update.',
  190. 500: 'An unknown error has occurred - Please report this to the Developer.',
  191. };
  192.  
  193. if (response?.status && response.status !== 200) {
  194. this.classList.replace('arb-default', 'arb-red');
  195. setTimeout(() => alert(errors[response.status] || errors[500]), 20);
  196. return;
  197. }
  198. this.classList.replace('arb-default', 'arb-green');
  199. }
  200.  
  201.  
  202. function addLocID() {
  203. const s = prompt("Please provide a Location, this should be provided by your Faction");
  204. if (s.length !== 0) {
  205. localStorage['assistLocID'] = s;
  206. }
  207. }