FightButtonLibrary

Library for fight button usage start

目前为 2024-03-15 提交的版本,查看 最新版本

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

  1. // ==UserScript==
  2. // @name FightButtonLibrary
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.10
  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. //alert(fullUrl)
  55.  
  56. // Send the request
  57. GM_xmlhttpRequest({
  58. method: "GET",
  59. url: fullUrl,
  60. onload: function(response) {
  61. handleResponse(response.responseText); // Handle the response
  62. },
  63. onerror: function(error) {
  64. console.error("Request failed:", error);
  65. }
  66. });
  67. } else {
  68. console.error("User ID not found in URL.");
  69. }
  70. }
  71. }
  72.  
  73. // Function to handle the response
  74. function handleResponse(responseText) {
  75. let resp=responseText
  76. window.sharedVariable = resp;
  77. alert("shared:",window.sharedVariable)
  78.  
  79.  
  80. // Additional functions can be added here to process the response
  81. // For example, you can call a function like startFight() here
  82. // startFight();
  83. }
  84.  
  85. // Expose the startProcess function to be called externally
  86. window.updateItemLibrary = {
  87. startProcess: startProcess
  88. };
  89.  
  90. })();