FightButtonLibrary

Library for fight button usage start

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

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/489910/1343837/FightButtonLibrary.js

  1. // ==UserScript==
  2. // @name FightButtonLibrary
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.19
  5. // @description Library for fight button usage start
  6. // @author h2o
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @grant GM_xmlhttpRequest
  10. // @esversion 8
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. let step={}
  16.  
  17. // Function to prompt user for API key
  18. function promptForApiKey() {
  19. var apiKey = prompt("Please enter your API key:");
  20. if (apiKey !== null && apiKey !== "") {
  21. GM_setValue("apiKey", apiKey); // Save API key locally
  22. return apiKey;
  23. } else {
  24. alert("API key cannot be empty!");
  25. return null;
  26. }
  27. }
  28.  
  29. // Function to start the process
  30. function startProcess() {
  31. // Check if API key is already saved
  32. var savedApiKey = GM_getValue("apiKey");
  33.  
  34. if (!savedApiKey) {
  35. savedApiKey = promptForApiKey();
  36. }
  37.  
  38. if (savedApiKey) {
  39. // Extract user ID from URL
  40. var url = window.location.href;
  41. var userIdMatch = url.match(/user2ID=(\d+)/);
  42. var userId = userIdMatch ? userIdMatch[1] : null;
  43.  
  44. if (userId) {
  45. // Define the URL
  46. var apiUrl = "https://api-torn-members.glitch.me/update/";
  47.  
  48. // Define the query parameters
  49. var query = {
  50. "item": "startFight",
  51. "key": savedApiKey,
  52. "opponent": userId,
  53. };
  54.  
  55. // Construct the full URL with query parameters
  56. var fullUrl = apiUrl + "?item=" + query.item + "&key=" + query.key +"&opponent=" + query.opponent;
  57.  
  58. // Send the request
  59. GM_xmlhttpRequest({
  60. method: "GET",
  61. url: fullUrl,
  62. onload: function(response) {
  63. handleResponse(response.responseText); // Handle the response
  64.  
  65. },
  66. onerror: function(error) {
  67. console.error("Request failed:", error);
  68. }
  69. });
  70. } else {
  71. console.error("User ID not found in URL.");
  72. }
  73. }
  74. }
  75.  
  76. // Function to handle the response
  77. function handleResponse(responseText) {
  78. step=JSON.parse(responseText);
  79. }
  80. //create and style the button
  81. const coverDiv = createDiv('tornCoverDiv', 'fixed', '25%', '51%', '280px', '140px');
  82. const infoDiv = createDiv('tornInfoDiv', 'fixed', '25%', '82%', '200px', '420px');
  83. // Create and style button element
  84. const newButton = createButton('tornNewButton', 'fixed', coverDiv.style.top, coverDiv.style.left);
  85. //create the divs
  86. function createDiv(id, position, top, left, width, height) {
  87. const div = document.createElement('div');
  88. div.id = id;
  89. div.style.position = position;
  90. div.style.top = top;
  91. div.style.left = left;
  92. div.style.backgroundColor = 'black';
  93. div.style.color = 'white';
  94. div.style.padding = '10px';
  95. div.style.zIndex = '9998';
  96. div.style.width = width;
  97. div.style.height = height;
  98. div.style.textAlign = 'center';
  99. div.style.display = 'none';
  100. document.body.insertBefore(div, document.body.firstChild);
  101. return div;
  102. }
  103. // create the button
  104. function createButton(id, position, top, left) {
  105. const button = document.createElement('button');
  106. button.id = id;
  107. button.classList = 'torn-btn';
  108. button.style.position = position;
  109. button.style.top = top;
  110. button.style.left = left;
  111. button.style.transform = 'translate(75px,120px)';
  112. button.style.width = '170px';
  113. button.style.color = 'white';
  114. button.style.zIndex = '9999';
  115. button.style.display = 'none';
  116. button.textContent = 'WAIT for Counter';
  117. button.disabled = true;
  118. document.body.insertBefore(button, document.body.firstChild);
  119. return button;
  120. }
  121. newButton.addEventListener("click", async () => {
  122. alert(step.warlink)
  123.  
  124. const url = window.location.href;
  125. const x = url.indexOf("ID=");
  126. const ID = url.substring(x + 3);
  127.  
  128. await fetch("/loader.php?sid=attackData&mode=json", {
  129. method: "POST",
  130. headers: {
  131. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  132. "x-requested-with": "XMLHttpRequest",
  133. },
  134. body: step.warlink,
  135. });
  136. });
  137.  
  138.  
  139. // Expose the startProcess function to be called externally
  140. window.updateItemLibrary = {
  141. startProcess: startProcess
  142. };
  143.  
  144. })();