K2S Bitrate Display (Structure Fix)

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);

})();