您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Greasy Fork is available in English.
A library for unit conversions.
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/519002/1492039/Conversion%20Utility%20Library.js
To always have the latest version use // @require https://update.greasyfork.icu/scripts/519002/Units%20Converter.js
Here’s how you can write clear, concise documentation for your library and its usage:
This library provides a simple framework to convert units of measurement. It supports various unit types (e.g., length, weight, volume, temperature) and handles conversions with single or compound values.
"150 pounds" → Outputs: "68.03 kg"
"10x20 inches" → Outputs: "25.40 x 50.80 cm"
"5^3" → Outputs: "125 power"
Regex Matching for Units
const selectedUnitType = SelectedText.match(Units)[3].toLowerCase();
Value Extraction
const SelectedUnitValue = SelectedText.match(Units)[1].replaceAll(',', '.');
const SecondSelectedUnitValue = SelectedText.match(Units)[2]?.replaceAll(',', '.') || 0;
Value Conversion
const convertValue = (value, unitType) => {
const { factor, convert } = window.UConv[unitType] || {};
return convert ? convert(value) : value * factor;
};
New Unit Detection
var NewUnit = window.UConv[selectedUnitType]?.unit || selectedUnitType;
Final Conversion Output
var ConvertedUnit = `${convertValue(parseFloat(SelectedUnitValue), selectedUnitType).toFixed(2)}${SecondSelectedUnitValue != 0 ? ` x ${convertValue(parseFloat(SecondSelectedUnitValue), selectedUnitType).toFixed(2)}` : ''}`;
Special Cases
ConvertedUnit = SelectedText.match(/\^(\d+\.?\d*)/)
? (NewUnit = 'power', Math.pow(parseFloat(SelectedUnitValue), parseFloat(SelectedText.match(/\^(\d+\.?\d*)/)[1])))
: ConvertedUnit;
5^3
.Display Conversion
ShowConvertion('Units', NewUnit, ConvertedUnit);
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);
UConv
.