FightButtonLibrary

Library for fight button usage start

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

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

  1. // ==UserScript==
  2. // @name FightButtonLibrary
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.18
  5. // @description Library for fight button usage start
  6. // @author h2o
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @grant GM_xmlhttpRequest
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. // Function to prompt user for API key
  15. function promptForApiKey() {
  16. var apiKey = prompt("Please enter your API key:");
  17. if (apiKey !== null && apiKey !== "") {
  18. GM_setValue("apiKey", apiKey); // Save API key locally
  19. return apiKey;
  20. } else {
  21. alert("API key cannot be empty!");
  22. return null;
  23. }
  24. }
  25.  
  26. // Function to start the process
  27. function startProcess() {
  28. // Check if API key is already saved
  29. var savedApiKey = GM_getValue("apiKey");
  30.  
  31. if (!savedApiKey) {
  32. savedApiKey = promptForApiKey();
  33. }
  34.  
  35. if (savedApiKey) {
  36. // Extract user ID from URL
  37. var url = window.location.href;
  38. var userIdMatch = url.match(/user2ID=(\d+)/);
  39. var userId = userIdMatch ? userIdMatch[1] : null;
  40.  
  41. if (userId) {
  42. // Define the URL
  43. var apiUrl = "https://api-torn-members.glitch.me/update/";
  44.  
  45. // Define the query parameters
  46. var query = {
  47. "item": "startFight",
  48. "key": savedApiKey,
  49. "opponent": userId,
  50. };
  51.  
  52. // Construct the full URL with query parameters
  53. var fullUrl = apiUrl + "?item=" + query.item + "&key=" + query.key +"&opponent=" + query.opponent;
  54.  
  55. // Send the request
  56. GM_xmlhttpRequest({
  57. method: "GET",
  58. url: fullUrl,
  59. onload: function(response) {
  60. handleResponse(response.responseText); // Handle the response
  61. },
  62. onerror: function(error) {
  63. console.error("Request failed:", error);
  64. }
  65. });
  66. } else {
  67. console.error("User ID not found in URL.");
  68. }
  69. }
  70. }
  71.  
  72. // Function to handle the response
  73. function handleResponse(responseText) {
  74. alert(responseText)
  75. }
  76.  
  77. // Expose the startProcess function to be called externally
  78. window.updateItemLibrary = {
  79. startProcess: startProcess
  80. };
  81.  
  82. })();