MouseHunt - TEM Catch Stats

Add catch/crown statistics next to mouse names on the TEM

当前为 2019-01-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MouseHunt - TEM Catch Stats
  3. // @author Tran Situ (tsitu)
  4. // @namespace https://greasyfork.org/en/users/232363-tsitu
  5. // @version 1.1
  6. // @description Add catch/crown statistics next to mouse names on the TEM
  7. // @match http://www.mousehuntgame.com/*
  8. // @match https://www.mousehuntgame.com/*
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. // Observers are attached to a *specific* element (will DC if removed from DOM)
  13. const observerTarget = document.getElementById("tabbarContent_page");
  14. if (observerTarget) {
  15. MutationObserver =
  16. window.MutationObserver ||
  17. window.WebKitMutationObserver ||
  18. window.MozMutationObserver;
  19.  
  20. const observer = new MutationObserver(function() {
  21. // Callback
  22. const labels = document.getElementsByClassName(
  23. "campPage-trap-trapEffectiveness-difficultyGroup-label"
  24. );
  25.  
  26. // Render if difficulty labels are in DOM
  27. if (labels.length > 0) {
  28. // Disconnect and reconnect to prevent infinite mutation loop
  29. observer.disconnect();
  30. render();
  31. observer.observe(observerTarget, {
  32. childList: true,
  33. subtree: true
  34. });
  35. }
  36. });
  37.  
  38. observer.observe(observerTarget, {
  39. childList: true,
  40. subtree: true
  41. });
  42. }
  43.  
  44. function postReq(form) {
  45. return new Promise((resolve, reject) => {
  46. const xhr = new XMLHttpRequest();
  47. xhr.open(
  48. "POST",
  49. `https://www.mousehuntgame.com/managers/ajax/users/profiletabs.php?action=badges&snuid=${
  50. user.sn_user_id
  51. }`,
  52. true
  53. );
  54. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  55. xhr.onreadystatechange = function() {
  56. if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
  57. resolve(this);
  58. }
  59. };
  60. xhr.onerror = function() {
  61. reject(this);
  62. };
  63. xhr.send(form);
  64. });
  65. }
  66.  
  67. function render() {
  68. // Render crown image and catch number next to mouse name
  69. const rawStore = localStorage.getItem("mh-catch-stats");
  70. if (rawStore) {
  71. const stored = JSON.parse(rawStore);
  72. const rows = document.getElementsByClassName(
  73. "campPage-trap-trapEffectiveness-mouse"
  74. );
  75.  
  76. if (rows) {
  77. for (let row of rows) {
  78. const name = row.querySelector(
  79. ".campPage-trap-trapEffectiveness-mouse-name"
  80. ).innerText;
  81. const catches = stored[name];
  82.  
  83. const outer = document.createElement("span");
  84. outer.className = "mousebox";
  85. outer.style.cssFloat = "none";
  86. outer.style.marginLeft = "10px";
  87. outer.style.verticalAlign = "middle";
  88.  
  89. const inner = document.createElement("span");
  90. if (catches >= 10 && catches < 100) {
  91. inner.className = "numcatches bronze";
  92. } else if (catches >= 100 && catches < 500) {
  93. inner.className = "numcatches silver";
  94. } else if (catches >= 500) {
  95. inner.className = "numcatches gold";
  96. }
  97. inner.style.backgroundSize = "contain";
  98. inner.style.paddingRight = "20px";
  99. inner.innerText = catches;
  100.  
  101. outer.appendChild(inner);
  102. row.appendChild(outer);
  103. }
  104. }
  105. }
  106.  
  107. const oldButton = document.getElementById("tem-catches-refresh-button");
  108. if (oldButton) oldButton.remove();
  109.  
  110. // Render 'Refresh Data' button
  111. const refreshButton = document.createElement("button");
  112. refreshButton.id = "tem-catches-refresh-button";
  113. refreshButton.innerText = "Refresh Crown Data";
  114. refreshButton.addEventListener("click", function() {
  115. postReq("sn=Hitgrab&hg_is_ajax=1").then(res => {
  116. parseData(res);
  117. });
  118. });
  119.  
  120. const container = document.getElementsByClassName(
  121. "campPage-trap-trapEffectiveness-content"
  122. )[0];
  123. if (container) container.appendChild(refreshButton);
  124. }
  125.  
  126. /**
  127. * Parse badge endpoint response and write to localStorage
  128. * @param {string} res
  129. */
  130. function parseData(res) {
  131. let response = null;
  132. try {
  133. if (res) {
  134. response = JSON.parse(res.responseText);
  135. const badgeData = response["mouse_data"];
  136. const remainData = response["remaining_mice"];
  137. const catchData = {};
  138.  
  139. for (let key of Object.keys(badgeData)) {
  140. catchData[badgeData[key]["name"]] = badgeData[key]["num_catches"];
  141. }
  142.  
  143. for (let el of remainData) {
  144. const split = el["name"].split(" (");
  145. catchData[split[0]] = parseInt(split[1][0]);
  146. }
  147.  
  148. localStorage.setItem("mh-catch-stats", JSON.stringify(catchData));
  149.  
  150. // Close and reopen to update badges (prevents infinite render loop)
  151. app.pages.CampPage.closeBlueprintDrawer();
  152. app.pages.CampPage.toggleTrapEffectiveness(true);
  153. }
  154. } catch (error) {
  155. console.log("Error while processing POST response");
  156. console.error(error.stack);
  157. }
  158. }
  159. })();