Greasy Fork

来自缓存

Greasy Fork is available in English.

Units Converter

A library for unit conversions.

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/519002/1613477/Units%20Converter.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

作者
hacker09
版本
0.0.1.20250615084635
创建于
2024-11-27
更新于
2025-06-15
大小
2.5 KB
许可证
暂无
How to Install scripts? (Click here)

If you like my work, please consider supporting it! (Cryptos / Patreon / Ko-Fi / BuyMeACoffee https://cyber-sec0.github.io)


To always have the latest version, use:

// @require      https://update.greasyfork.icu/scripts/519002/Units%20Converter.js

Units Supported

The library supports 27 units of measurement that are:

  • Inches
  • Centimeters
  • Meters
  • Feet
  • Kilograms
  • Pounds
  • Ounces
  • Grams
  • Fluid ounces
  • Kilometers per hour
  • Miles per hour
  • °C
  • °F
  • Milliliters
  • Liters
  • Gallons
  • Yards
  • Millimeters
  • Kilometers
  • Miles
  • Kilowatts
  • Mechanical HorsePower
  • Miles per Gallon
  • Liters per 100 Kilometers
  • Liquid Quarts
  • Foot-Pounds
  • Newton-Meters

The conversions are:

Conversion Example Input Example Output
Inches ⇄ Centimeters 72in, 72″ 182.88 cm
Millimeters ➜ Inches 50mm 1.97in
Meters ⇄ Feet 6.50 ft 1.98 m
Yards ➜ Meters 239 yd 218.46 m
Ounces ⇄ Grams 1.76 oz 49.90 g
Kilometers ⇄ Miles 60mi 96.54 km
Kilometers per hour ⇄ Miles per hour 62.14mph 99.98 km/h
°F ⇄ °C 39°C 102.20°F
Milliliters ⇄ Fluid ounces 49.98 ml 1.69 fl
Liters ⇄ Gallons 2.06 gal 7.80 l
Kilowatts ⇄ Mechanical HorsePower 700kW 938.70 mhp
Miles per Gallon ⇄ Liters per 100 Kilometers 33.6mpg 7l/100km
Liquid Quarts ⇄ Liters 4 qt 3.78 l
Foot-Pounds ⇄ Newton-Meters 250lb ft 338.95 N·m

Unit Conversion Library Documentation

Overview

This library provides a simple framework to convert units of measurement for the script http://greasyfork.icu/en/scripts/419825. It supports various unit types (e.g., length, weight, volume, temperature) and handles conversions with single or compound values.

To see the latest const Units regex and a real usage scenario, check http://greasyfork.icu/en/scripts/419825.

Features

  1. Unit Conversion: Converts a numeric value from one unit to another.
  2. Compound Conversions: Handles cases with two values (e.g., "10x20 inches").
  3. Exponential Operations: Supports calculations like powers (e.g., "5^3").
  4. Unit Recognition: Uses a regex pattern to identify units and values from text input.

Usage

Example Input

"150 pounds" → Outputs: "68.03 kg"
"10x20 inches" → Outputs: "25.40 x 50.80 cm"
"5^3" → Outputs: "125 power"

Main Functions

  1. Regex Matching for Units

    const UnitType = "150 pounds".match(Units)[3].toLowerCase(); // Extracts the unit type
    
  2. Value Extraction

    const Units = new RegExp(
    /^[ \t\xA0]*(-?\d+(?:[., ]\d+)?)(?:[ \t\xA0]*x[ \t\xA0]*(-?\d+(?:[., ]\d+)?))?[ \t\xA0]*
    (in|inch|inches|"|”|″|cm|cms|centimeters? \ |m|mt|mts|meters?|ft|kg|lbs?|pounds?|kilograms?|ounces?|
    g|ozs?|fl oz|fl oz (us)|fluid ounces?|kphs?|km\/h|kilometers per hours?|mhp|mphs?|meters per hours?|
    (?:°\s?|º\s?|)(?:degrees?\s+)?(?:celsius|fahrenheit|[CF])|km\/hs?|ml|milliliters?|l|liters?|litres?|
    gal|gallons?|yards?|yd|Millimeter|millimetre|kilometers?|mi|mm|miles?|km|ft|fl|feets?|grams?|kilowatts?|
    kws?|brake horsepower|mechanical horsepower|hps?|bhps?|miles per gallons?|mpgs?|liters per 100 kilometers?|
    lt?\/100km|liquid quarts?|lqs?|qt|foot-? ?pounds?|ft-?lbs?|lb fts?|newton-? ?meters?|n·?m|\^\d+)
    [ \t\xA0]*(?:\(\w+\)[ \t\xA0]*)?$/i
    );
    
    const UnitValue = "150 pounds".match(Units)[1].replaceAll(',', '.'); // Extracts numeric value
    const SecondUnitValue = "150 pounds".match(Units)[2]?.replaceAll(',', '.') || 0; // Extracts second value if present
    
  3. Value Conversion

    const convertValue = (value, unitType) => {
       const { factor, convert } = window.UConv[unitType] || {};
       return convert ? convert(value) : value * factor; // Converts value using factor or custom function
    };
    
  4. New Unit Detection

    const NewUnit = window.UConv[UnitType]?.unit || UnitType; // Detects target unit
    
  5. Final Conversion Output

    const ConvertedUnit = `${convertValue(parseFloat(UnitValue), UnitType).toFixed(2)}${SecondUnitValue != 0 ? ` x ${convertValue(parseFloat(
    SecondUnitValue), UnitType).toFixed(2)}` : ''}`; // Formats output
    
  6. Special Cases

    const ConvertedUnit = "5^3".match(/\^(\d+\.?\d*)/) 
       ? (NewUnit = 'power', Math.pow(parseFloat(UnitValue), parseFloat("5^3".match(/\^(\d+\.?\d*)/)[1]))) 
       : ConvertedUnit; // Handles powers
    
  7. Display Conversion

    ShowConvertion('Units', NewUnit, ConvertedUnit); // Displays result
    

Adding New Units

To add new units, modify the addConversion function in the UConv library:

addConversion(['unit1', 'unit2'], 'targetUnit', conversionFactor, optionalConvertFunction);

Example:

addConversion(['pound', 'lb'], 'kg', 0.453592); // Adds "pound" conversion to kilograms

How It Works

  1. Input Parsing: Uses regex to extract unit type and numeric values from text input.
  2. Conversion: Fetches the appropriate conversion factor or custom function from UConv.
  3. Output: Returns the converted value in the desired format, supporting both compound and exponential cases.