Bandwidth Unit Converter (bits → bytes)

Converts Mbps, Gbps, Kbps into MB/s, GB/s, KB/s on webpages (works in real-time on fast.com, speedtest.net, ISP offers, etc.)

Versione datata 18/08/2025. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Bandwidth Unit Converter (bits → bytes)
// @namespace    spotlightforbugs.scripts.bandwidth
// @version      1.3
// @description  Converts Mbps, Gbps, Kbps into MB/s, GB/s, KB/s on webpages (works in real-time on fast.com, speedtest.net, ISP offers, etc.)
// @author       SpotlightForBugs
// @license      MIT
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  // Conversion factors (bits → bytes)
  const conversions = {
    Kbps: { factor: 1 / 8, unit: "KB/s" },
    Mbps: { factor: 1 / 8, unit: "MB/s" },
    Gbps: { factor: 1 / 8, unit: "GB/s" },
    Tbps: { factor: 1 / 8, unit: "TB/s" },
    "kbit/s": { factor: 1 / 8, unit: "KB/s" },
    "Mbit/s": { factor: 1 / 8, unit: "MB/s" },
    "Gbit/s": { factor: 1 / 8, unit: "GB/s" },
  };

  function convertTextNode(node) {
    let text = node.nodeValue;
    if (!text) return;

    // Match "100 Mbps" style
    const regex = new RegExp(
      "(\\d+(?:\\.\\d+)?)\\s*(" + Object.keys(conversions).join("|") + ")",
      "gi"
    );

    node.nodeValue = text.replace(regex, (match, value, unit) => {
      if (match.includes("(")) return match; // already converted
      const num = parseFloat(value);
      const { factor, unit: newUnit } = conversions[unit];
      const converted = (num * factor).toFixed(2);
      return `${match} (${converted} ${newUnit})`;
    });
  }

  function handleSeparatedUnits(node) {
    // Look for patterns like: <span>247.04</span><span>Mbps</span>
    if (
      node.nodeType === 1 &&
      node.nextSibling &&
      node.nextSibling.nodeType === 1
    ) {
      const numText = node.textContent.trim();
      const unitText = node.nextSibling.textContent.trim();

      if (conversions[unitText] && !node.nextSibling.dataset.converted) {
        const num = parseFloat(numText);
        if (!isNaN(num)) {
          const { factor, unit } = conversions[unitText];
          const converted = (num * factor).toFixed(2);
          node.nextSibling.textContent =
            unitText + ` (${converted} ${unit})`;
          node.nextSibling.dataset.converted = "true";
        }
      }
    }
  }

  function walk(node) {
    let child, next;

    switch (node.nodeType) {
      case 1: // Element
        handleSeparatedUnits(node);
      // fallthrough
      case 9: // Document
      case 11: // Document fragment
        child = node.firstChild;
        while (child) {
          next = child.nextSibling;
          walk(child);
          child = next;
        }
        break;

      case 3: // Text node
        convertTextNode(node);
        break;
    }
  }

  // Initial run
  walk(document.body);

  // Observe changes (for dynamic content like fast.com, speedtest.net)
  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      mutation.addedNodes.forEach((node) => {
        walk(node);
      });
      if (mutation.type === "characterData") {
        walk(mutation.target);
      }
    });
  });

  observer.observe(document.body, {
    childList: true,
    subtree: true,
    characterData: true,
  });
})();

/*
MIT License

Copyright (c) 2025 SpotlightForBugs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/