🐭️ MouseHunt - Metric

Convert mice weight to metric.

  1. // ==UserScript==
  2. // @name 🐭️ MouseHunt - Metric
  3. // @description Convert mice weight to metric.
  4. // @version 2.1.0
  5. // @license MIT
  6. // @author bradp
  7. // @namespace bradp
  8. // @match https://www.mousehuntgame.com/*
  9. // @icon https://i.mouse.rip/mh-improved/icon-64.png
  10. // @run-at document-end
  11. // @grant none
  12. // @require https://cdn.jsdelivr.net/npm/script-migration@1.1.1
  13. // ==/UserScript==
  14.  
  15. var mhui = (() => {
  16. var __defProp = Object.defineProperty;
  17. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  18. var __getOwnPropNames = Object.getOwnPropertyNames;
  19. var __hasOwnProp = Object.prototype.hasOwnProperty;
  20. var __export = (target, all) => {
  21. for (var name in all)
  22. __defProp(target, name, { get: all[name], enumerable: true });
  23. };
  24. var __copyProps = (to, from, except, desc) => {
  25. if (from && typeof from === "object" || typeof from === "function") {
  26. for (let key of __getOwnPropNames(from))
  27. if (!__hasOwnProp.call(to, key) && key !== except)
  28. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  29. }
  30. return to;
  31. };
  32. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  33. var __async = (__this, __arguments, generator) => {
  34. return new Promise((resolve, reject) => {
  35. var fulfilled = (value) => {
  36. try {
  37. step(generator.next(value));
  38. } catch (e) {
  39. reject(e);
  40. }
  41. };
  42. var rejected = (value) => {
  43. try {
  44. step(generator.throw(value));
  45. } catch (e) {
  46. reject(e);
  47. }
  48. };
  49. var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
  50. step((generator = generator.apply(__this, __arguments)).next());
  51. });
  52. };
  53.  
  54. // src/modules/metric/index.js
  55. var metric_exports = {};
  56. __export(metric_exports, {
  57. default: () => metric_default
  58. });
  59.  
  60. // src/utils/events.js
  61. var requestCallbacks = {};
  62. var onRequestHolder = null;
  63. var onRequest = (url = null, callback = null, skipSuccess = false, ignore = []) => {
  64. url = "*" === url ? "*" : `managers/ajax/${url}`;
  65. if (ignore.includes(url)) {
  66. return;
  67. }
  68. if (!callback) {
  69. return;
  70. }
  71. if (!requestCallbacks[url]) {
  72. requestCallbacks[url] = [];
  73. }
  74. requestCallbacks[url].push({
  75. callback,
  76. skipSuccess
  77. });
  78. if (onRequestHolder) {
  79. return;
  80. }
  81. const req = XMLHttpRequest.prototype.open;
  82. XMLHttpRequest.prototype.open = function() {
  83. this.addEventListener("load", function() {
  84. if (this.responseText) {
  85. let response = {};
  86. try {
  87. response = JSON.parse(this.responseText);
  88. } catch (e) {
  89. return;
  90. }
  91. Object.keys(requestCallbacks).forEach((key) => {
  92. if ("*" === key || this.responseURL.includes(key)) {
  93. requestCallbacks[key].forEach((item) => {
  94. if (item.callback && typeof item.callback === "function" && (item.skipSuccess || (response == null ? void 0 : response.success))) {
  95. item.callback(response);
  96. }
  97. });
  98. }
  99. });
  100. }
  101. });
  102. Reflect.apply(req, this, arguments);
  103. };
  104. onRequestHolder = true;
  105. };
  106. var getDialogMapping = () => {
  107. return {
  108. treasureMapPopup: "map",
  109. itemViewPopup: "item",
  110. mouseViewPopup: "mouse",
  111. largerImage: "image",
  112. convertibleOpenViewPopup: "convertible",
  113. adventureBookPopup: "adventureBook",
  114. marketplaceViewPopup: "marketplace",
  115. giftSelectorViewPopup: "gifts",
  116. supportPageContactUsForm: "support",
  117. MHCheckout: "premiumShop"
  118. };
  119. };
  120. var onDialogShow = (overlay = null, callback = null, once = false) => {
  121. const identifier = callback.toString().replaceAll(/[^\w-]/gi, "");
  122. eventRegistry.addEventListener("js_dialog_show", () => {
  123. if (!activejsDialog) {
  124. return;
  125. }
  126. const tokens = activejsDialog.getAllTokens();
  127. if (!tokens || !tokens["{*content*}"] || !tokens["{*content*}"].value || tokens["{*content*}"].value === "" || tokens["{*content*}"].value.includes('data-item-type=""') || // Item view.
  128. tokens["{*content*}"].value.includes('data-mouse-id=""')) {
  129. return;
  130. }
  131. const atts = activejsDialog.getAttributes();
  132. let dialogType = atts.className.replace("jsDialogFixed", "").replace("wide", "").replace("default", "").replaceAll(" ", " ").replaceAll(" ", ".").trim();
  133. if (dialogType.endsWith(".")) {
  134. dialogType = dialogType.slice(0, -1);
  135. }
  136. if ((!overlay || "all" === overlay) && "function" === typeof callback) {
  137. return callback();
  138. }
  139. const dialogMapping = getDialogMapping();
  140. if ("function" === typeof callback && (overlay === dialogType || overlay === dialogMapping[dialogType])) {
  141. return callback();
  142. }
  143. }, null, once, 0, identifier);
  144. };
  145.  
  146. // src/utils/messages.js
  147. hadAddedErrorStyles = false;
  148.  
  149. // src/modules/metric/index.js
  150. var imperialToMetric = (text) => {
  151. const lb = text.match(/(\d+? )lb./i);
  152. const oz = text.match(/(\d+? )oz./i);
  153. if (!(lb || oz)) {
  154. return;
  155. }
  156. const lbValue = lb ? lb[1] : 0;
  157. const ozValue = oz ? oz[1] : 0;
  158. const totalWeight = Number.parseInt(lbValue) + Number.parseInt(ozValue) / 16;
  159. const totalWeightMetric = (Math.round(totalWeight * 0.45359237 * 100) / 100).toString();
  160. return text.replace(/(\d+? lb.\s)?(\d+? oz.)/i, totalWeightMetric + " kg. ");
  161. };
  162. var convertInDialog = () => {
  163. const mouseViewWeights = document.querySelectorAll(".mouseView-statsContainer .mouseView-statsContainer-block-padding table tbody tr");
  164. if (mouseViewWeights.length) {
  165. mouseViewWeights.forEach((row) => {
  166. const firstCell = row.querySelector("td");
  167. const secondCell = firstCell.nextSibling;
  168. if (firstCell.innerText === "Avg. Weight:" || firstCell.innerText === "Heaviest:") {
  169. const converted = imperialToMetric(secondCell.innerText);
  170. if (converted) {
  171. secondCell.innerText = converted;
  172. }
  173. }
  174. });
  175. }
  176. };
  177. var replaceInJournal = () => {
  178. const entries = document.querySelectorAll(".journal .entry .journalbody .journaltext");
  179. if (!entries.length) {
  180. return;
  181. }
  182. entries.forEach((entry) => {
  183. const converted = imperialToMetric(entry.innerHTML);
  184. if (converted) {
  185. entry.innerHTML = converted;
  186. }
  187. });
  188. };
  189. var replaceOnMousePage = () => {
  190. const mouseWeightsStats = document.querySelectorAll(".mouseListView-categoryContent-subgroupContainer .mouseListView-categoryContent-subgroup-mouse-stats");
  191. if (!mouseWeightsStats.length) {
  192. return;
  193. }
  194. mouseWeightsStats.forEach((stat) => {
  195. if (stat.classList.contains("average_weight") || stat.classList.contains("heaviest_catch")) {
  196. const converted = imperialToMetric(stat.innerText);
  197. if (converted) {
  198. stat.innerText = converted;
  199. }
  200. }
  201. });
  202. };
  203. var convertOnPage = () => {
  204. replaceOnMousePage();
  205. replaceInJournal();
  206. };
  207. var init = () => __async(void 0, null, function* () {
  208. onDialogShow("all", convertInDialog);
  209. onRequest("*", convertOnPage);
  210. convertOnPage();
  211. });
  212. var metric_default = {
  213. id: "metric",
  214. name: "Metric Units",
  215. type: "feature",
  216. default: false,
  217. description: "Use metric units instead of imperial units.",
  218. load: init
  219. };
  220. return __toCommonJS(metric_exports);
  221. })();
  222. mhImprovedVersion = "0.0.0-userscript;"
  223. mhImprovedPlatform = "userscript";
  224. mhui.default.load();
  225. migrateUserscript('Metric', 'https://greasyfork.org/en/scripts/449840-mousehunt-metric');