DeKleineKobini / TORN: Gym Percentage

// ==UserScript==
// @name         TORN: Gym Percentage
// @version      1.0.1
// @author       DeKleineKobini
// @description  Instead of showing stats, show the percentage.
// @namespace    dekleinekobini.gympercentage
// @require      https://openuserjs.org/src/libs/DeKleineKobini/DKK_Torn_Utilities.js
// @match        https://www.torn.com/gym.php*
// @license      MIT
// @updateURL    https://openuserjs.org/meta/DeKleineKobini/TORN_Gym_Percentage.meta.js
// ==/UserScript==

setDebug(false);

/* --------------------
CODE - EDIT ON OWN RISK
-------------------- */
setPrefixEasy("GP");

observeMutations(document, "#gymroot", true, function(mut, obs) {
    if (isMobile()) showPercentageAll();

    observeMutations($("#gymroot > div").get(0), "#strength-val", false, function(mut, obs) {
        observeMutationsFull($("#strength-val").get(0), function(mut, obs) { showPercentageAll(); }, { characterData: true, subtree: true });
        observeMutationsFull($("#defense-val").get(0), function(mut, obs) { showPercentageAll(); }, { characterData: true, subtree: true });
        observeMutationsFull($("#speed-val").get(0), function(mut, obs) { showPercentageAll(); }, { characterData: true, subtree: true });
        observeMutationsFull($("#dexterity-val").get(0), function(mut, obs) { showPercentageAll(); }, { characterData: true, subtree: true });

        showPercentageAll();
    });
}, { childList: true, subtree: true });

function showPercentageAll() {
    showPercentage("strength");
    showPercentage("defense");
    showPercentage("speed");
    showPercentage("dexterity");
}

function showPercentage(type) {
    debug(`showPercentage(${type})`);
    let elValue = $(`#${type}-val`);
    let elParent = elValue.parent()
    let elTitle = elParent.find("h3");

    let value = parseInt(replaceAll(elValue.html(), ",", ""));
    let percent = value / getTotal();

    percent = (parseInt(Math.round(percent * 10000)) / 100); // rounding

    elTitle.html(`${getShort(type)} ${percent}%`);
}

function getShort(type) {
    switch (type) {
        case "strength":
            return "Str";
        case "speed":
            return "Sp";
        case "defense":
            return "Def";
        case "dexterity":
            return "Dex";
    }
    return "ERR";
}

function getTotal() {
    let strength = parseInt(replaceAll($("#strength-val").html(), ",", ""));
    let defense = parseInt(replaceAll($("#defense-val").html(), ",", ""));
    let speed = parseInt(replaceAll($("#speed-val").html(), ",", ""));
    let dexterity = parseInt(replaceAll($("#dexterity-val").html(), ",", ""));

    return strength + defense + speed + dexterity;
}

function isMobile() {
    return navigator.userAgent.match(/Android/i)
    || navigator.userAgent.match(/webOS/i)
    || navigator.userAgent.match(/iPhone/i)
    || navigator.userAgent.match(/iPad/i)
    || navigator.userAgent.match(/iPod/i)
    || navigator.userAgent.match(/BlackBerry/i)
    || navigator.userAgent.match(/Windows Phone/i);
}