Auto Send Att Unit Settings

Set the desired arrival time and the script will automatically send the attack. Added unit settings modal.

当前为 2024-12-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Auto Send Att Unit Settings
  3. // @version 1.3
  4. // @description Set the desired arrival time and the script will automatically send the attack. Added unit settings modal.
  5. // @include https://*/game.php?*&screen=place&try=confirm
  6. // @namespace https://greasyfork.org/users/1388899
  7. // ==/UserScript==
  8.  
  9. // Function to get the current page identifier (e.g., pathname or other unique identifiers)
  10. function getPageIdentifier() {
  11. return window.location.pathname; // You can use other unique identifiers here if needed
  12. }
  13.  
  14. // Create a button to open the settings modal
  15. const button = document.createElement('button');
  16. button.id = 'openSettings';
  17. button.innerHTML = 'Set Units';
  18. button.style.position = 'fixed';
  19. button.style.bottom = '20px';
  20. button.style.left = '20px';
  21. button.style.padding = '10px 20px';
  22. button.style.backgroundColor = '#4CAF50';
  23. button.style.color = 'white';
  24. button.style.border = 'none';
  25. button.style.cursor = 'pointer';
  26. document.body.appendChild(button);
  27.  
  28. // Create the settings modal
  29. const modal = document.createElement('div');
  30. modal.id = 'settingsModal';
  31. modal.style.display = 'none';
  32. modal.style.position = 'fixed';
  33. modal.style.top = '50%';
  34. modal.style.left = '50%';
  35. modal.style.transform = 'translate(-50%, -50%)';
  36. modal.style.backgroundColor = 'white';
  37. modal.style.border = '1px solid #ccc';
  38. modal.style.padding = '20px';
  39. modal.style.boxShadow = '0px 4px 8px rgba(0,0,0,0.2)';
  40. modal.style.zIndex = '1000';
  41. modal.innerHTML = `
  42. <h3>Set Unit Quantities</h3>
  43. <label for="spear">Spear:</label><input type="number" id="spear" value="0"><br>
  44. <label for="sword">Sword:</label><input type="number" id="sword" value="0"><br>
  45. <label for="axe">Axe:</label><input type="number" id="axe" value="0"><br>
  46. <label for="spy">Spy:</label><input type="number" id="spy" value="0"><br>
  47. <label for="light">Light:</label><input type="number" id="light" value="0"><br>
  48. <label for="heavy">Heavy:</label><input type="number" id="heavy" value="0"><br>
  49. <label for="ram">Ram:</label><input type="number" id="ram" value="0"><br>
  50. <label for="catapult">Catapult:</label><input type="number" id="catapult" value="0"><br>
  51. <label for="knight">Knight:</label><input type="number" id="knight" value="0"><br>
  52. <label for="snob">Snob:</label><input type="number" id="snob" value="0"><br>
  53. <label for="numAttack">Num attack:</label><input type="number" id="numAttack" value="0" min="0"><br>
  54. <button id="applySettings" style="margin-top: 20px;">Apply</button>
  55. <button id="saveSettings" style="margin-top: 20px; background-color: #007bff;">Save</button>
  56. <button id="closeModal" style="margin-top: 20px; background-color: red;">Close</button>
  57. `;
  58. document.body.appendChild(modal);
  59.  
  60. // Open the modal when the settings button is clicked
  61. document.getElementById('openSettings').addEventListener('click', function() {
  62. const pageIdentifier = getPageIdentifier(); // Get the current page's identifier (e.g., pathname)
  63. // Load values from localStorage specific to the page
  64. const savedUnits = JSON.parse(localStorage.getItem(`units_${pageIdentifier}`)) || {};
  65.  
  66. // Populate the inputs with saved values
  67. document.getElementById('spear').value = savedUnits.spear || 0;
  68. document.getElementById('sword').value = savedUnits.sword || 0;
  69. document.getElementById('axe').value = savedUnits.axe || 0;
  70. document.getElementById('spy').value = savedUnits.spy || 0;
  71. document.getElementById('light').value = savedUnits.light || 0;
  72. document.getElementById('heavy').value = savedUnits.heavy || 0;
  73. document.getElementById('ram').value = savedUnits.ram || 0;
  74. document.getElementById('catapult').value = savedUnits.catapult || 0;
  75. document.getElementById('knight').value = savedUnits.knight || 0;
  76. document.getElementById('snob').value = savedUnits.snob || 0;
  77. document.getElementById('numAttack').value = savedUnits.numAttack || 0;
  78.  
  79. document.getElementById('settingsModal').style.display = 'block';
  80. });
  81.  
  82. // Close the modal when the close button is clicked
  83. document.getElementById('closeModal').addEventListener('click', function() {
  84. document.getElementById('settingsModal').style.display = 'none';
  85. });
  86.  
  87. // Apply the unit values to the input fields and click the button multiple times when the "Apply" button is clicked
  88. document.getElementById('applySettings').addEventListener('click', function() {
  89. // Get the values from the modal inputs
  90. const spear = document.getElementById('spear').value;
  91. const sword = document.getElementById('sword').value;
  92. const axe = document.getElementById('axe').value;
  93. const spy = document.getElementById('spy').value;
  94. const light = document.getElementById('light').value;
  95. const heavy = document.getElementById('heavy').value;
  96. const ram = document.getElementById('ram').value;
  97. const catapult = document.getElementById('catapult').value;
  98. const knight = document.getElementById('knight').value;
  99. const snob = document.getElementById('snob').value;
  100. const numAttack = parseInt(document.getElementById('numAttack').value); // Get the number of attacks
  101.  
  102. // Save the values to localStorage using the current page identifier as a key
  103. const units = {
  104. spear,
  105. sword,
  106. axe,
  107. spy,
  108. light,
  109. heavy,
  110. ram,
  111. catapult,
  112. knight,
  113. snob,
  114. numAttack
  115. };
  116.  
  117. const pageIdentifier = getPageIdentifier(); // Get the current page's identifier (e.g., pathname)
  118. localStorage.setItem(`units_${pageIdentifier}`, JSON.stringify(units)); // Use page-specific key
  119.  
  120. // Click the button 'numAttack' times with a delay of 200ms between clicks
  121. const element = document.getElementById('troop_confirm_train');
  122. for (let i = 0; i < numAttack; i++) {
  123. setTimeout(() => {
  124. if (element) {
  125. element.click(); // Simulate the click
  126. }
  127. }, 200 * i); // Delay of 200ms between clicks
  128. }
  129.  
  130. // Update the corresponding input values on the page after clicking
  131. setTimeout(() => {
  132. const inputs = document.querySelectorAll('.units-row input[data-unit]');
  133. inputs.forEach(input => {
  134. const unitType = input.dataset.unit;
  135. // Set value for each unit according to the modal input value
  136. if (unitType === 'spear') {
  137. input.value = spear;
  138. } else if (unitType === 'sword') {
  139. input.value = sword;
  140. } else if (unitType === 'axe') {
  141. input.value = axe;
  142. } else if (unitType === 'spy') {
  143. input.value = spy;
  144. } else if (unitType === 'light') {
  145. input.value = light;
  146. } else if (unitType === 'heavy') {
  147. input.value = heavy;
  148. } else if (unitType === 'ram') {
  149. input.value = ram;
  150. } else if (unitType === 'catapult') {
  151. input.value = catapult;
  152. } else if (unitType === 'knight') {
  153. input.value = knight;
  154. } else if (unitType === 'snob') {
  155. input.value = snob;
  156. }
  157. });
  158.  
  159. // Close the modal after applying the settings
  160. document.getElementById('settingsModal').style.display = 'none';
  161.  
  162. // Delay of 200ms before enabling the submit button
  163. setTimeout(() => {
  164. // Get the input element with the ID 'troop_confirm_submit'
  165. const submitButton = document.getElementById('troop_confirm_submit');
  166.  
  167. // Remove the 'disabled' attribute to enable the button
  168. if (submitButton) {
  169. submitButton.removeAttribute('disabled');
  170. }
  171. }, 200);
  172. }, 200 * numAttack); // Ensure values are updated after the last click
  173. });
  174.  
  175. // Save the unit values to localStorage without applying or clicking the button, specific to the page
  176. document.getElementById('saveSettings').addEventListener('click', function() {
  177. // Get the values from the modal inputs
  178. const spear = document.getElementById('spear').value;
  179. const sword = document.getElementById('sword').value;
  180. const axe = document.getElementById('axe').value;
  181. const spy = document.getElementById('spy').value;
  182. const light = document.getElementById('light').value;
  183. const heavy = document.getElementById('heavy').value;
  184. const ram = document.getElementById('ram').value;
  185. const catapult = document.getElementById('catapult').value;
  186. const knight = document.getElementById('knight').value;
  187. const snob = document.getElementById('snob').value;
  188. const numAttack = parseInt(document.getElementById('numAttack').value); // Get the number of attacks
  189.  
  190. // Save the values to localStorage using the current page identifier as a key
  191. const units = {
  192. spear,
  193. sword,
  194. axe,
  195. spy,
  196. light,
  197. heavy,
  198. ram,
  199. catapult,
  200. knight,
  201. snob,
  202. numAttack
  203. };
  204.  
  205. const pageIdentifier = getPageIdentifier(); // Get the current page's identifier (e.g., pathname)
  206. localStorage.setItem(`units_${pageIdentifier}`, JSON.stringify(units)); // Use page-specific key
  207. });