MZ Tactics Selector

Adds a dropdown menu to automatically set up tactics.

目前为 2023-06-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MZ Tactics Selector
  3. // @namespace essenfc
  4. // @version 0.2
  5. // @description Adds a dropdown menu to automatically set up tactics.
  6. // @author Douglas Vieira
  7. // @match https://www.managerzone.com/?p=tactics
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=managerzone.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. "use strict";
  15.  
  16. window.addEventListener("load", function () {
  17. let formationContainer = document.getElementById("formation-container");
  18. formationContainer.style.display = "flex";
  19. formationContainer.style.alignItems = "center";
  20. formationContainer.style.justifyContent = "space-between";
  21.  
  22. let dropdown = document.createElement("select");
  23. dropdown.id = "tacticsDropdown";
  24. dropdown.style.padding = "1px";
  25. dropdown.style.fontSize = "12px";
  26. dropdown.style.borderRadius = "2px";
  27.  
  28. let dropdownDescription = document.createElement("span");
  29. dropdownDescription.textContent = "Select a custom tactic: ";
  30. dropdownDescription.style.marginRight = "1px";
  31.  
  32. let placeholderOption = document.createElement("option");
  33. placeholderOption.value = "";
  34. placeholderOption.text = "@";
  35. placeholderOption.disabled = true;
  36. placeholderOption.selected = true;
  37. dropdown.appendChild(placeholderOption);
  38.  
  39. let tacticsDataUrl =
  40. "https://raw.githubusercontent.com/douglasdotv/tactics-selector/main/data.json?callback=?";
  41. fetch(tacticsDataUrl)
  42. .then((response) => response.json())
  43. .then((data) => {
  44. for (const tactic of data.tactics) {
  45. let option = document.createElement("option");
  46. option.value = tactic.name;
  47. option.text = tactic.name;
  48. dropdown.appendChild(option);
  49. }
  50.  
  51. let neededButton = document.createElement("button");
  52. neededButton.textContent = "";
  53. neededButton.style.margin = "1px";
  54. neededButton.style.visibility = "hidden";
  55. neededButton.addEventListener("click", function () {
  56. let presetDropdown = document.getElementById("tactics_preset");
  57. presetDropdown.value = "4-4-2";
  58. let e = new Event("change");
  59. presetDropdown.dispatchEvent(e);
  60. });
  61.  
  62. formationContainer.appendChild(dropdownDescription);
  63. formationContainer.appendChild(dropdown);
  64. formationContainer.appendChild(neededButton);
  65.  
  66. dropdown.addEventListener("change", function () {
  67. let tactic = this.value;
  68.  
  69. let outfieldPlayers = Array.from(
  70. document.querySelectorAll(
  71. ".fieldpos.fieldpos-ok.ui-draggable:not(.substitute):not(.substitute.goalkeeper):not(.goalkeeper)"
  72. )
  73. );
  74.  
  75. let rearrangePlayers = function () {
  76. outfieldPlayers = Array.from(
  77. document.querySelectorAll(
  78. ".fieldpos.fieldpos-ok.ui-draggable:not(.substitute):not(.substitute.goalkeeper):not(.goalkeeper)"
  79. )
  80. );
  81.  
  82. let coordinates = {};
  83. const selectedTactic = data.tactics.find(
  84. (tacticData) => tacticData.name === tactic
  85. );
  86.  
  87. if (selectedTactic) {
  88. coordinates = selectedTactic.coordinates;
  89. }
  90.  
  91. for (let i = 0; i < outfieldPlayers.length; ++i) {
  92. outfieldPlayers[i].style.left = coordinates[i][0] + "px";
  93. outfieldPlayers[i].style.top = coordinates[i][1] + "px";
  94. }
  95. };
  96.  
  97. if (outfieldPlayers.length < 10) {
  98. neededButton.click();
  99. setTimeout(rearrangePlayers, 1);
  100. } else {
  101. rearrangePlayers();
  102. }
  103. });
  104. })
  105. .catch((error) => {
  106. console.error('Epic fail: ', error);
  107. });
  108. });
  109. })();