Greasy Fork

Greasy Fork is available in English.

MAM Ratio Prettify

11/22/23 Reduce the precision of the main menu ratio to prettify it for larger ratios

当前为 2023-11-22 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         MAM Ratio Prettify
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  11/22/23 Reduce the precision of the main menu ratio to prettify it for larger ratios
// @author       studioninja
// @match        https://www.myanonamouse.net/*
// @icon         https://cdn.myanonamouse.net/imagebucket/218812/square_root_icon_512x512_i31wd3km.png
// @grant        none
// @license      MIT
// ==/UserScript==

/** ------ CONFIG ------ **/
const RATIO_PRECISION = 4;
/** ------ CONFIG ------ **/

const DEBUG = 1;

(function() {
    'use strict';
    const ratioEl = document.querySelector("#tmR").firstChild;
    updateRatio(ratioEl);

    // utilize an observer to handle the site or other scripts changing the value
    const callback = (mutationList, observer) => {
        DEBUG && console.debug('[MAM-Ratio-Prettify] Observer callback triggered', mutationList, observer);

        // try to execute after other scripts
        setTimeout(() => {
            if(!mutationList?.[0]?.target) return;

            // disconnect the observer first to prevent causing an infinite loop since we are modifying what we are observing
            observer.disconnect();

            DEBUG && console.debug('[MAM-Ratio-Prettify] Observer updating ratio in element: ', mutationList[0].target);
            updateRatio(mutationList[0].target);

            // reconnect the observer to listen again for changes caused by other scripts or xhr changes
            observer.observe(mutationList[0].target, { characterData: true });
            DEBUG && console.debug('[MAM-Ratio-Prettify] Observer reconnected on element: ', mutationList[0].target);
        }, 100)
    };

    const observer = new MutationObserver(callback);
    observer.observe(ratioEl, { characterData: true });
    DEBUG && console.debug('[MAM-Ratio-Prettify] Observer initiated on element: ', ratioEl);
})();

function updateRatio(el) {
    if(!el) {
        DEBUG && console.error('[MAM-Ratio-Prettify] Attempted to update the ratio of a nonexistent element');
        return;
    }

    const ratioText = el.textContent;
    const ratio = Number(ratioText.replaceAll(',', ''));

    if(isNaN(ratio)) {
        DEBUG && console.debug(`[MAM-Ratio-Prettify] Invalid ratio value '${ratio}', skipping prettification`);
        return;
    }

    const roundedRatio = Number(ratio.toPrecision(RATIO_PRECISION));
    const prettyRatio = roundedRatio.toLocaleString();
    el.textContent = prettyRatio;
    console.log(`[MAM-Ratio-Prettify] Updated ratio from '${ratioText}' to ${prettyRatio}`);
}