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