🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Bandwidth Unit Converter (bits → bytes)

Converts Mbps, Gbps, Kbps into MB/s, GB/s, KB/s on webpages (optimized for speedtest.net, fast.com, ISP offers, etc.)

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

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.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

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

(function () {
  "use strict";

  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" },
  };

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

  function convertText(text) {
    return 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 processNode(node) {
    if (!node) return;

    // Text node
    if (node.nodeType === 3 && !node.parentNode?.dataset.converted) {
      const newText = convertText(node.nodeValue);
      if (newText !== node.nodeValue) {
        node.nodeValue = newText;
        node.parentNode.dataset.converted = "true";
      }
    }

    // Element node with number + unit split
    if (node.nodeType === 1 && !node.dataset.converted) {
      const next = node.nextSibling;
      if (next && next.nodeType === 1) {
        const num = parseFloat(node.textContent.trim());
        const unitText = next.textContent.trim();
        if (!isNaN(num) && conversions[unitText]) {
          const { factor, unit } = conversions[unitText];
          const converted = (num * factor).toFixed(2);
          next.textContent = unitText + ` (${converted} ${unit})`;
          next.dataset.converted = "true";
        }
      }
    }
  }

  // Initial scan (only visible text nodes)
  function initialScan() {
    document.querySelectorAll("body *:not([data-converted])").forEach((el) => {
      if (el.childNodes.length === 1 && el.childNodes[0].nodeType === 3) {
        processNode(el.childNodes[0]);
      }
    });
  }

  initialScan();

  // Mutation observer (optimized)
  const observer = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      if (mutation.type === "characterData") {
        processNode(mutation.target);
      } else if (mutation.type === "childList") {
        mutation.addedNodes.forEach((node) => {
          processNode(node);
          if (node.querySelectorAll) {
            node.querySelectorAll("*").forEach(processNode);
          }
        });
      }
    }
  });

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