GoldDerby Odds to Percentage

Converts odds on GoldDerby to percentages

// ==UserScript==
// @name         GoldDerby Odds to Percentage
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Converts odds on GoldDerby to percentages
// @author       https://greasyfork.org/users/1320826-polachek
// @match        *://*.goldderby.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function oddsToPercentage(odds) {
        const [numerator, denominator] = odds.split('/').map(Number);
        const percentage = 1 / (numerator / denominator + 1) * 100;
        return percentage.toFixed(2) + '%';
    }

    function processOddsElements() {
        const oddsElements = document.querySelectorAll('.predictions-odds.predictions-experts.gray');

        oddsElements.forEach(el => {
            // Procurar apenas pelo texto de odds diretamente no elemento
            const oddsText = el.childNodes[0]?.nodeValue.trim();
            if (oddsText && /^\d+\/\d+$/.test(oddsText)) {
                const percentage = oddsToPercentage(oddsText);
                el.childNodes[0].nodeValue = `${oddsText} (${percentage})`;

                // Adicionar padding ao elemento
                el.style.padding = '0 5px';
                console.log(`Converted ${oddsText} to ${oddsText} (${percentage})`);
            }
        });
    }

    function addGlobalStyle(css) {
        const head = document.getElementsByTagName('head')[0];
        if (!head) return;
        const style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = css;
        head.appendChild(style);
    }

    addGlobalStyle('.predictions-odds.predictions-experts.gray { padding: 0 5px; }');

    processOddsElements();

    const observer = new MutationObserver(processOddsElements);
    observer.observe(document.body, { childList: true, subtree: true });
})();