Greasy Fork is available in English.

Medals

try to take over the world!

// ==UserScript==
// @name         Medals
// @namespace    http://tampermonkey.net/
// @version      2025-02-13a
// @description  try to take over the world!
// @author       brandwagen
// @match        https://osu.ppy.sh/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=ppy.sh
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const medalMax = 339;
    let changed = false;

    function main(medalElement) {
        const medalCount = medalElement.textContent;
        if (!medalCount.includes("%")) {
            const medalPercentage = (medalCount / medalMax * 100).toFixed(2);
            medalElement.textContent = `${medalPercentage}% (${medalCount})`;
            medalElement.style.color = getColor(medalPercentage);
            changed = true;
        }
    }

    function getColor(p) {
        if (p > 95) { return '#495afa'; }
        else if (p > 90) { return '#60edf4'; }
        else if (p > 80) { return '#b66aed'; }
        else if (p > 60) { return '#dd596f'; }
        else if (p > 40) { return '#ff8c68'; }
        return '#9dbece';
    }

    function findLabelIndex(elements) {
        for (let i = 0; i < elements.length; i++) {
            if (elements[i].textContent == "Medals") {
                return i;
            }
        }
        return null;
    }

    function waitForElement() {
        const elements = document.querySelectorAll('.value-display__label');

        if (elements!=null) {
            const element = document.querySelectorAll('.value-display__value')[findLabelIndex(elements)];
            main(element);
        } else {
            setTimeout(waitForElement, 100);
        }
    }

    var oldURL = "";
    var newURL = window.location.pathname;

    window.setInterval(function(){
        if(oldURL != newURL){
            oldURL = newURL;
            changed = false;
            setTimeout(waitForElement, 1000);
        }

        newURL = window.location.pathname;
    }, 250);


})();