Get Stuffed Revive Requests

Adds a button to request a revive from Get Stuffed

当前为 2024-03-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Get Stuffed Revive Requests
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3.1
  5. // @description Adds a button to request a revive from Get Stuffed
  6. // @author Stig [2648238]
  7. // @match https://www.torn.com/*
  8. // @grant GM_addStyle
  9. // @grant GM.xmlHttpRequest
  10. // @grant GM_registerMenuCommand
  11. // @connect api.no1irishstig.co.uk
  12. // ==/UserScript==
  13. 'use strict';
  14. const owner = "Get Stuffed";
  15. const source = "Get Stuffed Script";
  16. const buttonLabel = "Get Revived!";
  17. const colour = "#33B3F8";
  18. const API_URL = 'https://api.no1irishstig.co.uk/request';
  19.  
  20. setInterval(checkButton, 500);
  21. GM_addStyle(`
  22. .revive-text {
  23. color: ${colour};
  24. }
  25. .revive-icon {
  26. fill: ${colour} !important;
  27. }
  28. `);
  29. let btn;
  30. function getButton() {
  31. btn = document.createElement('a');
  32. if (document.body.classList.contains('dark-mode')) {
  33. btn.classList.add('custom-dark-mode');
  34. }
  35. btn.id = 'custom-btn';
  36. btn.className = "custom-revive t-clear h c-pointer m-icon line-h24 right last"
  37. btn.innerHTML = `
  38. <span class="icon-wrap svg-icon-wrap">
  39. <span class="link-icon-svg">
  40. ${icon}
  41. </span>
  42. </span>
  43. <span class="revive-text">${buttonLabel}</span>
  44. `;
  45. btn.href = '#custom-revive';
  46. btn.addEventListener('click', submitRequest);
  47. btn.addEventListener("mouseenter", () => {
  48. document.getElementsByClassName("revive-text")[0].style.color = colour;
  49. document.getElementsByClassName("revive-icon")[0].setAttribute("style", `fill: ${colour} !important`);
  50. });
  51. btn.addEventListener("mouseleave", () => {
  52. document.getElementsByClassName("revive-text")[0].style.color = null;
  53. document.getElementsByClassName("revive-icon")[0].removeAttribute("style");
  54. });
  55. return btn;
  56. }
  57. function checkButton() {
  58. if(inCloudflareChallenge()) return;
  59. const {hospital} = getSessionData();
  60. const exists = document.getElementById('custom-btn');
  61. if (!hospital) {
  62. if (btn) {
  63. btn.remove();
  64. }
  65. return;
  66. }
  67. if (exists) {
  68. return;
  69. }
  70. const location = document.getElementById("top-page-links-list");
  71. if (location != null) {
  72. const issues = document.getElementsByClassName("tt-revive");
  73. if (issues.length !== 0) {
  74. issues[0].remove();
  75. }
  76. location.children[location.children.length - 1].insertAdjacentElement('afterend', getButton());
  77. }
  78. }
  79. function handleResponse(response) {
  80. if (response?.status && response.status !== 200) {
  81. var responseText = response.responseText.replace(/^"|"$/g, '');
  82. alert(`Error Code: ${response.status}\nMessage: ${responseText}` || `An unknown error has occurred - Please report this to ${owner} leadership.`);
  83. return;
  84. }
  85. let contract = false;
  86. try {
  87. contract = !!JSON.parse(response.responseText).contract;
  88. } catch (e) {
  89. }
  90. if (contract) {
  91. alert(`Contract request has been sent to ${owner}. Thank you!`)
  92. } else {
  93. alert(`Request has been sent to ${owner}. Please pay your reviver a Xanax or $1m. Thank you!`);
  94. }
  95. }
  96. function submitRequest(e) {
  97. e?.preventDefault();
  98. const sessionData = getSessionData();
  99. if (!sessionData.hospital) {
  100. alert('You are not in the hospital.');
  101. return;
  102. }
  103. btn.setAttribute('disabled', true);
  104. btn.setAttribute('aria-pressed', true);
  105. GM.xmlHttpRequest({
  106. method: 'POST',
  107. url: API_URL,
  108. headers: {
  109. 'Content-Type': 'application/json',
  110. },
  111. data: JSON.stringify({
  112. 'tornid': parseInt(sessionData.userID),
  113. 'username': '' + sessionData.userName,
  114. "vendor": owner,
  115. 'source': `${source} ${GM_info.script.version}`,
  116. 'type': 'revive'
  117. }),
  118. onload: handleResponse,
  119. });
  120. }
  121. function getSessionData() {
  122. const sidebar = Object.keys(sessionStorage).find((k) => /sidebarData\d+/.test(k));
  123. const data = JSON.parse(sessionStorage.getItem(sidebar));
  124. return {
  125. userID: data.user.userID,
  126. userName: data.user.name,
  127. hospital: data.statusIcons?.icons?.hospital,
  128. };
  129. }
  130. function inCloudflareChallenge() {
  131. return document.getElementsByClassName('iAmUnderAttack').length;
  132. }
  133. GM_registerMenuCommand(`Request revive from ${owner}`, () => submitRequest());
  134. const icon = `
  135. <svg xmlns="http://www.w3.org/2000/svg"
  136. width="15" height="15" viewBox="0 0 20 20" id="svg3" >
  137. <path id="revive-icon" fill="none"
  138. class="revive-icon"
  139. d="M18.737,9.691h-5.462c-0.279,0-0.527,0.174-0.619,0.437l-1.444,4.104L8.984,3.195c-0.059-0.29-0.307-0.506-0.603-0.523C8.09,2.657,7.814,2.838,7.721,3.12L5.568,9.668H1.244c-0.36,0-0.655,0.291-0.655,0.655c0,0.36,0.294,0.655,0.655,0.655h4.8c0.281,0,0.532-0.182,0.621-0.45l1.526-4.645l2.207,10.938c0.059,0.289,0.304,0.502,0.595,0.524c0.016,0,0.031,0,0.046,0c0.276,0,0.524-0.174,0.619-0.437L13.738,11h4.999c0.363,0,0.655-0.294,0.655-0.655C19.392,9.982,19.1,9.691,18.737,9.691z"/>
  140. </svg>`;