Torn: Custom Race Setup

Customizable race setup for Torn City

目前为 2024-10-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Torn: Custom Race Setup
  3. // @namespace TornCustomRace
  4. // @description Customizable race setup for Torn City
  5. // @version 0.3.2
  6. // @license MIT
  7. // @match https://www.torn.com/loader.php?sid=racing*
  8. // @grant none
  9. // @update https://update.greasyfork.org/scripts/514608/Torn%3A%20Custom%20Race%20Setup.user.js
  10. // ==/UserScript==
  11.  
  12. /*
  13. ---- User Configuration Section ----
  14. Adjust the following variables to customize the race setup
  15. */
  16.  
  17. const carID = 'your_car_id_here'; // Car ID (e.g., found by hovering over "remove from enlisted" in My Cars, looks like '&carID=649295')
  18. const raceTitle = 'Custom Race'; // Title of the custom race, also displayed on the button
  19. const startHourUTC = 14; // Race start time in UTC (24-hour format). E.g., 14 for 14:00 UTC
  20. const laps = 100; // Number of laps for the race
  21. const trackID = 10; // Track ID (e.g., 10 for Docks)
  22. const minDrivers = 2; // Minimum and maximum drivers allowed for the race
  23. const maxDrivers = 100;
  24. const minClass = 5; // Class and car restrictions for the race
  25. const carsTypeAllowed = 1; // More details will be available in future updates
  26. const carsAllowed = 5;
  27. const betAmount = 0; // Amount to bet on the race (0 for no bet)
  28.  
  29. /* ---- End of User Configuration Section ---- */
  30.  
  31. function addButton() {
  32. function checkAndAddButton() {
  33. const targetContainer = document.querySelector('#racingAdditionalContainer .btn-wrap.silver.c-pointer');
  34. const buttonExists = document.querySelector('#customRaceButton');
  35.  
  36. if (targetContainer && !buttonExists) {
  37. // Create the button element
  38. const button = document.createElement('button');
  39. button.id = 'customRaceButton';
  40. button.className = 'btn btn-action-tab torn-btn btn-dark-bg';
  41. button.textContent = raceTitle;
  42.  
  43. // Add a result display span for feedback
  44. const resultSpan = document.createElement('span');
  45. resultSpan.id = 'customRaceResult';
  46. resultSpan.style.fontSize = '12px';
  47. resultSpan.style.fontWeight = '100';
  48.  
  49. // Append the button and span to the target container
  50. targetContainer.appendChild(button);
  51. targetContainer.appendChild(resultSpan);
  52.  
  53. // Attach click event listener to the button
  54. button.addEventListener('click', () => {
  55. resultSpan.textContent = '';
  56.  
  57. // Set the race time to the user-defined hour in UTC
  58. const now = new Date();
  59. const raceTimeUTC = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), startHourUTC, 0, 0));
  60. const unixTimecode = Math.floor(raceTimeUTC.getTime() / 1000);
  61.  
  62. // Construct the URL for the custom race
  63. const url = `https://torn.com/loader.php?sid=racing&tab=customrace&action=getInRace&step=getInRace` +
  64. `&id=&carID=${carID}&createRace=true&title=${encodeURIComponent(raceTitle)}` +
  65. `&minDrivers=${minDrivers}&maxDrivers=${maxDrivers}&trackID=${trackID}&laps=${laps}` +
  66. `&minClass=${minClass}&carsTypeAllowed=${carsTypeAllowed}&carsAllowed=${carsAllowed}` +
  67. `&betAmount=${betAmount}&waitTime=${unixTimecode}&rfcv=${getRFC()}`;
  68.  
  69. // Redirect to the constructed URL
  70. window.location = url;
  71. console.log(`${raceTitle} button clicked`);
  72. });
  73. }
  74. }
  75.  
  76. // Use MutationObserver to check when the target container is added to the DOM
  77. const observer = new MutationObserver(checkAndAddButton);
  78. observer.observe(document.body, { childList: true, subtree: true });
  79.  
  80. // Run an initial check in case the element is already loaded
  81. checkAndAddButton();
  82. }
  83.  
  84. // Ensure the function runs after the page is fully loaded
  85. window.addEventListener('load', addButton);