K2S Bitrate Display (Structure Fix)

Calculates bitrate by targeting the specific sibling structure of K2S metadata.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         K2S Bitrate Display (Structure Fix)
// @namespace    
// @version      5.0
// @description  Calculates bitrate by targeting the specific sibling structure of K2S metadata.
// @author       Gemini
// @license MIT
// @match        
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function parseDuration(timeStr) {
        if (!timeStr) return 0;
        const parts = timeStr.trim().split(':').map(Number);
        if (parts.length === 3) return (parts[0] * 3600) + (parts[1] * 60) + parts[2];
        if (parts.length === 2) return (parts[0] * 60) + parts[1];
        return 0;
    }

    function parseSize(sizeStr) {
        if (!sizeStr) return 0;
        // Matches "2.53 GB" or "855 MB"
        const match = sizeStr.match(/([\d\.]+)\s*(GB|MB|KB)/i);
        if (!match) return 0;

        let bits = parseFloat(match[1]);
        const unit = match[2].toUpperCase();

        if (unit === 'GB') bits *= 1073741824;
        else if (unit === 'MB') bits *= 1048576;
        else if (unit === 'KB') bits *= 1024;

        return bits * 8;
    }

    function run() {
        // 1. Find all elements that strictly contain the text "size" or "duration"
        // We use a broad selector but filter by exact text content to find the labels
        const allDivs = document.querySelectorAll('div[type="label"]');

        let sizeValueElement = null;
        let durationStr = null;

        // Iterate through all labels to find our targets
        allDivs.forEach(el => {
            const text = el.textContent.trim().toLowerCase();

            // A. Find the SIZE value
            if (text === 'size') {
                // The structure is: <div>Value</div> <div>Label</div>
                // So the value is the PREVIOUS sibling of the label
                const valueEl = el.previousElementSibling;
                if (valueEl) {
                    sizeValueElement = valueEl;
                }
            }

            // B. Find the DURATION value
            if (text === 'duration') {
                const valueEl = el.previousElementSibling;
                if (valueEl) {
                    durationStr = valueEl.textContent.trim();
                }
            }
        });

        // 2. If we found both and haven't processed this yet...
        if (sizeValueElement && durationStr && !sizeValueElement.dataset.bitrateAdded) {

            const seconds = parseDuration(durationStr);
            const bits = parseSize(sizeValueElement.textContent);

            if (seconds > 0 && bits > 0) {
                const mbps = ((bits / seconds) / 1000000).toFixed(2);

                // 3. Create the Badge
                const span = document.createElement('span');
                // Styling to match the dark theme, using the green accent color
                span.style.marginLeft = "8px";
                span.style.color = "#4caf50";
                span.style.fontWeight = "bold";
                span.innerText = `(${mbps} Mbps)`;

                // 4. Append inside the "2.53 GB" div
                sizeValueElement.appendChild(span);
                sizeValueElement.dataset.bitrateAdded = "true";
            }
        }
    }

    // Run repeatedly to handle dynamic loading (React/Vue)
    setInterval(run, 1000);

})();