Auto Send Att Unit Settings

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

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