GL_helper

Выбор существа ГЛ по текстовому вводу, воскресить всех по 1 клику

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

  1. // ==UserScript==
  2. // @name GL_helper
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.02
  5. // @description Выбор существа ГЛ по текстовому вводу, воскресить всех по 1 клику
  6. // @author Something begins
  7. // @license joe mamma
  8. // @match https://www.heroeswm.ru/leader_*
  9. // @match https://my.lordswm.com/leader_*
  10. // @match https://lordswm.com/leader_*
  11. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  12. // @grant none
  13. // ==/UserScript==
  14. const duration = 500;
  15. // Без паузы сервер гвд не обрабатывает все запросы по ресу существ
  16. const fetchWithDelay = async (url) => {
  17. await new Promise(resolve => setTimeout(resolve, 1000));
  18. return fetch(url).then(response => console.log(response.status));
  19. };
  20. function recruitPage(){
  21. if (location.href.includes("leader_army_exchange")) return;
  22. let chosen_creatures = [];
  23. let available_creatures = {}
  24. let army_pos_i = 1
  25. function add_new_cre(cre_index, cre_count){
  26. let index;
  27. if (obj_army.filter(cre => cre.link == 0).length>=1) index = obj_army.filter(cre => cre.link != 0).length+1
  28. else {
  29. index = army_pos_i;
  30. army_pos_i++;
  31. if (army_pos_i>=8) army_pos_i = 1
  32. }
  33. obj_army[index].link = cre_index;
  34. obj_army[index].count = 1;
  35. show_details(cre_index)
  36. if (chosen_creatures.length>7){
  37. chosen_creatures = []
  38. }
  39. }
  40. document.querySelector("#hwm_no_zoom").insertAdjacentHTML("beforebegin",
  41. `<div id="cre_select_div" style = " position: absolute;
  42. left: 10%;
  43. top: 15%;
  44. transform: translate(-50%, -50%);">
  45. <label for="cre_select_input">Выбрать существо:</label>
  46. <input type="text" name="creature_choice" list="cre_select" id = "cre_select_input">
  47. <datalist name="Выбрать существо" id="cre_select"></datalist>
  48. </div>`
  49. )
  50. const datalist = document.querySelector("#cre_select")
  51. const input = document.querySelector("#cre_select_input")
  52. for (const creature of obj){
  53. if (creature) {
  54. available_creatures[creature.name] = obj.indexOf(creature)
  55. datalist.insertAdjacentHTML(`beforeend`, `<option id = "cre_choice${obj.indexOf(creature)}" value="${creature.name}"></option>`);
  56. }
  57. }
  58. let eventSource = null;
  59. input.addEventListener('keydown', (event) => {
  60. eventSource = event.key ? 'input' : 'list';
  61. });
  62. function creRecruited(creLink){
  63. const links = [];
  64. for (const cre of obj_army){
  65. if (!cre) continue;
  66. links.push(parseInt(cre.link));
  67. }
  68. return links.includes(parseInt(creLink)) ? true : false;
  69. }
  70. input.addEventListener('input', (event)=>{
  71. let chosen_creature;
  72. let possible_options = [...datalist.children].filter(option => option.value.toLowerCase().includes(input.value.toLowerCase()));
  73. if (eventSource === 'list') chosen_creature = available_creatures[event.target.value.trim()]
  74. if (possible_options.length === 1) chosen_creature = possible_options[0].id.match(/cre_choice(\d+)/)[1];
  75. if (chosen_creature !== undefined){
  76. if (creRecruited(chosen_creature)) return;
  77. add_new_cre(chosen_creature, 1)
  78. chosen_creatures.push(chosen_creature)
  79. }
  80. });
  81.  
  82. const targetElement = document.querySelector('#reset_div');
  83. console.log(targetElement);
  84.  
  85. const observer = new MutationObserver((mutationsList) => {
  86. for (let mutation of mutationsList) {
  87. if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
  88. targetElement.style.display = "inline";
  89. }
  90. }
  91. });
  92.  
  93. observer.observe(targetElement, { attributes: true, attributeFilter: ['style'] });
  94. }
  95. function resPage(){
  96. const match = document.body.innerHTML.match(/sign=(.*?)\"/);
  97. if (!match) throw new Error("sign not found");
  98. const sign = match[1];
  99. document.querySelector("#set_mobile_max_width > div:nth-child(2)").insertAdjacentHTML("beforeend",
  100. `
  101. <div class="home_button2 btn_hover2" style="width: 10em; margin: auto; margin-top: 0.5em; margin-bottom: 1em;">
  102. <a id = "script_res" style="text-decoration: none;"><div style="width: 100%;">Воскресить всех</div></a>
  103. </div>
  104. `);
  105. const resButton = document.querySelector("#script_res");
  106. const tbody = document.querySelector("#set_mobile_max_width > table > tbody");
  107. const urls = [];
  108. for (const tr of tbody.children){
  109. const all_a = Array.from(tr.querySelectorAll("a"));
  110. const war_a = all_a.filter(a => {return a.href.includes("war")})[0];
  111. const cre_a = all_a.filter(a => {return a.href.includes("army_info")})[0];
  112. if (!war_a || !cre_a) continue;
  113. const warid = war_a.href.match(/\d+/)[0];
  114. const creId = cre_a.href.match(/name=(.*)/)[1];
  115. urls.push(`${location.origin}/leader_guild.php?action=res_old&warid=${warid}&sign=${sign}&mon_id=${creId}`);
  116.  
  117. }
  118. console.log(urls);
  119.  
  120. resButton.addEventListener("click", event=>{
  121. event.preventDefault();
  122. resButton.parentElement.classList.add("home_disabled");
  123. function fetchAll(i = 0){
  124. if (i >= urls.length) location.reload(); // Stop when all URLs are processed
  125. fetch(urls[i])
  126. .then(response => {
  127. //console.log(`Response from ${urls[i]}:`, response.status);
  128. })
  129. .catch(error => {
  130. console.error(`Error fetching ${urls[i]}:`, error);
  131. })
  132. .finally(() => {
  133. resButton.firstChild.innerHTML = `Воскрешение ${i+1}/${urls.length}`;
  134. setTimeout(() => {
  135. fetchAll(i + 1);
  136. }, duration);
  137. });
  138. }
  139. fetchAll();
  140.  
  141. });
  142. }
  143. if (location.href.includes("leader_army")) recruitPage();
  144. if (location.href.includes("leader_ressurect_old")) resPage();