🐭️ MouseHunt - Metric

Convert mice weight to metric.

目前为 2024-01-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 🐭️ MouseHunt - Metric
  3. // @version 1.1.0
  4. // @description Convert mice weight to metric.
  5. // @license MIT
  6. // @author bradp
  7. // @namespace bradp
  8. // @match https://www.mousehuntgame.com/*
  9. // @icon https://i.mouse.rip/mouse.png
  10. // @grant none
  11. // @run-at document-end
  12. // @require https://cdn.jsdelivr.net/npm/script-migration@1.1.1
  13. // ==/UserScript==
  14.  
  15. ((function () {
  16. 'use strict';
  17.  
  18. /**
  19. * Do something when the overlay is shown or hidden.
  20. *
  21. * @param {Object} callbacks
  22. * @param {Function} callbacks.show The callback to call when the overlay is shown.
  23. * @param {Function} callbacks.hide The callback to call when the overlay is hidden.
  24. * @param {Function} callbacks.change The callback to call when the overlay is changed.
  25. */
  26. const onOverlayChange = (callbacks) => {
  27. const observer = new MutationObserver(() => {
  28. if (callbacks.change) {
  29. callbacks.change();
  30. }
  31.  
  32. if (document.getElementById('overlayBg').classList.length > 0) {
  33. if (callbacks.show) {
  34. callbacks.show();
  35. }
  36. } else if (callbacks.hide) {
  37. callbacks.hide();
  38. }
  39. });
  40. observer.observe(
  41. document.getElementById('overlayBg'),
  42. {
  43. attributes: true,
  44. attributeFilter: ['class']
  45. }
  46. );
  47. };
  48.  
  49. /**
  50. * Convert the text in the given element to metric.
  51. */
  52. const replaceImperialWithMetric = () => {
  53. const elements = document.querySelectorAll('.journal .entry .journalbody .journaltext');
  54. if (! elements) {
  55. return;
  56. }
  57.  
  58. elements.forEach((element) => {
  59. // Grab the lb. and oz. values.
  60. const lb = element.innerText.match(/(\d+? )lb./i);
  61. const oz = element.innerText.match(/(\d+? )oz./i);
  62. if (! (lb || oz)) {
  63. return;
  64. }
  65.  
  66. // Convert the lb. and oz. values to metric.
  67. const lbValue = lb ? lb[ 1 ] : 0;
  68. const ozValue = oz ? oz[ 1 ] : 0;
  69. const totalWeight = parseInt(lbValue) + (parseInt(ozValue) / 16);
  70. const totalWeightMetric = (Math.round((totalWeight * 0.45359237) * 100) / 100).toString();
  71.  
  72. // Replace the lb. and oz. values with the metric values.
  73. element.innerHTML = element.innerHTML.replace(/(\d+? lb.\s)?(\d+? oz.)/i, totalWeightMetric + ' kg. ');
  74. });
  75. };
  76.  
  77. /**
  78. * Replace text in the mouse overlay / mouse page.
  79. */
  80. const replaceMouseOverlay = () => {
  81. const elements = document.querySelectorAll('.mouseView-statsContainer-block-padding table tr td');
  82.  
  83. const avgIndex = Array.from(elements).findIndex((element) => element.innerText.match(/Avg. Weight:/i));
  84. const heaviestIndex = Array.from(elements).findIndex((element) => element.innerText.match(/Heaviest:/i));
  85.  
  86. if (avgIndex !== -1) {
  87. if (elements[ avgIndex + 1 ]) {
  88. replaceImperialWithMetric([elements[ avgIndex + 1 ]]);
  89. }
  90. }
  91. if (heaviestIndex !== -1) {
  92. if (elements[ heaviestIndex + 1 ]) {
  93. replaceImperialWithMetric([elements[ heaviestIndex + 1 ]]);
  94. }
  95. }
  96. };
  97.  
  98. replaceImperialWithMetric();
  99. onOverlayChange({
  100. show: () => {
  101. // Run immediately, then again in quick succession to make sure the weight is updated.
  102. replaceMouseOverlay();
  103. setTimeout(replaceMouseOverlay, 250);
  104. setTimeout(replaceMouseOverlay, 500);
  105. }
  106. });
  107.  
  108. migrateUserscript('🐭️ MouseHunt - Metric', 'https://greasyfork.org/en/scripts/449840-mousehunt-metric');
  109. })());