FightButtonLibrary

Library for fight button usage start

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/489910/1344904/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 me
  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. let sfight="";
  17.  
  18.  
  19. // Function to prompt user for API key
  20. function promptForApiKey() {
  21. var apiKey = prompt("Please enter your API key:");
  22. if (apiKey !== null && apiKey !== "") {
  23. GM_setValue("apiKey", apiKey); // Save API key locally
  24. return apiKey;
  25. } else {
  26. alert("API key cannot be empty!");
  27. return null;
  28. }
  29. }
  30.  
  31. // Function to start the process
  32. function startProcess() {
  33. // Check if API key is already saved
  34. var savedApiKey = GM_getValue("apiKey");
  35.  
  36. if (!savedApiKey) {
  37. savedApiKey = promptForApiKey();
  38. }
  39.  
  40. if (savedApiKey) {
  41. // Extract user ID from URL
  42. var url = window.location.href;
  43. var userIdMatch = url.match(/user2ID=(\d+)/);
  44. var userId = userIdMatch ? userIdMatch[1] : null;
  45.  
  46. if (userId) {
  47. // Define the URL
  48. var apiUrl = "https://api-torn-members.glitch.me/update/";
  49.  
  50. // Define the query parameters
  51. var query = {
  52. "item": "startFight",
  53. "key": savedApiKey,
  54. "opponent": userId,
  55. };
  56.  
  57. // Construct the full URL with query parameters
  58. var fullUrl = apiUrl + "?item=" + query.item + "&key=" + query.key +"&opponent=" + query.opponent;
  59.  
  60. // Send the request
  61. GM_xmlhttpRequest({
  62. method: "GET",
  63. url: fullUrl,
  64. onload: function(response) {
  65. handleResponse(response.responseText); // Handle the response
  66.  
  67. },
  68. onerror: function(error) {
  69. console.error("Request failed:", error);
  70. }
  71. });
  72. } else {
  73. console.error("User ID not found in URL.");
  74. }
  75. }
  76. }
  77.  
  78. // Function to handle the response
  79. function handleResponse(responseText) {
  80. step=JSON.parse(responseText);
  81. sfight=step.warlink;
  82. //alert(sfight)
  83. }
  84. //create and style the button
  85. const coverDiv = createDiv('tornCoverDiv', 'fixed', '25%', '51%', '280px', '140px');
  86. const infoDiv = createDiv('tornInfoDiv', 'fixed', '25%', '86%', '170px', '420px');
  87. // Create and style button element
  88. const newButton = createButton('tornNewButton', 'fixed', coverDiv.style.top, coverDiv.style.left);
  89. //create the divs
  90. function createDiv(id, position, top, left, width, height) {
  91. const div = document.createElement('div');
  92. div.id = id;
  93. div.style.position = position;
  94. div.style.top = top;
  95. div.style.left = left;
  96. div.style.backgroundColor = 'black';
  97. div.style.color = 'white';
  98. div.style.padding = '10px';
  99. div.style.zIndex = '9998';
  100. div.style.width = width;
  101. div.style.height = height;
  102. div.style.textAlign = 'center';
  103. div.style.display = 'none';
  104. document.body.insertBefore(div, document.body.firstChild);
  105. return div;
  106. }
  107. // create the button
  108. function createButton(id, position, top, left) {
  109. const button = document.createElement('button');
  110. button.id = id;
  111. button.classList = 'torn-btn';
  112. button.style.position = position;
  113. button.style.top = top;
  114. button.style.left = left;
  115. button.style.transform = 'translate(75px,120px)';
  116. button.style.width = '170px';
  117. button.style.color = 'white';
  118. button.style.zIndex = '9999';
  119. button.style.display = 'none';
  120. button.textContent = 'WAIT for Counter';
  121. button.disabled = true;
  122. document.body.insertBefore(button, document.body.firstChild);
  123. return button;
  124. }
  125.  
  126.  
  127.  
  128.  
  129. // Expose the startProcess function to be called externally
  130. window.updateItemLibrary = {
  131. startProcess: startProcess
  132. };
  133. window.getSFight = function() {
  134. return sfight;
  135. };
  136.  
  137. })();