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, etc.)

Stan na 18-08-2025. Zobacz najnowsza wersja.

// ==UserScript==
// @name         Bandwidth Unit Converter (bits → bytes)
// @namespace    spotlightforbugs.scripts.bandwidth
// @version      1.2
// @description  Converts Mbps, Gbps, Kbps into MB/s, GB/s, KB/s on webpages (works in real-time on fast.com, speedtest.net, 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" },
  };

  // Regex to find numbers followed by units (e.g., "100 Mbps")
  const regex = new RegExp(
    "\\b(\\d+(?:\\.\\d+)?)\\s*(" + Object.keys(conversions).join("|") + ")\\b",
    "gi"
  );

  function convertText(text) {
    return text.replace(regex, (match, value, unit) => {
      // Prevent double conversion (skip if already has parentheses)
      if (/\(.*\/s\)/.test(text)) return match;

      const num = parseFloat(value);
      const { factor, unit: newUnit } = conversions[unit];
      const converted = (num * factor).toFixed(2);
      return `${match} (${converted} ${newUnit})`;
    });
  }

  function walk(node) {
    let child, next;

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

      case 3: // Text node
        const newText = convertText(node.nodeValue);
        if (newText !== node.nodeValue) {
          node.nodeValue = newText;
        }
        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.
*/
长期地址
遇到问题?请前往 GitHub 提 Issues。