@sv443-network/userutils 7.2.2 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +89 -16
- package/dist/index.global.js +30 -20
- package/dist/index.js +29 -19
- package/dist/lib/colors.d.ts +11 -8
- package/dist/lib/translation.d.ts +4 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @sv443-network/userutils
|
|
2
2
|
|
|
3
|
+
## 8.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 54ee0ce: Changed `hexToRgb()` and `rgbToHex()` to support `#RGBA` and `#RRGGBBAA` color codes (with an alpha channel).
|
|
8
|
+
Both functions now have an `alpha` value immediately after `blue`, which can be set to `undefined` to restore the old behavior.
|
|
9
|
+
|
|
10
|
+
### Minor Changes
|
|
11
|
+
|
|
12
|
+
- 54ee0ce: Added parameter `upperCase` (false by default) to `lightenColor()` and `darkenColor()`
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- 54ee0ce: Consolidated behavior of `lightenColor()` and `darkenColor()` when using non-number values
|
|
17
|
+
|
|
18
|
+
## 7.3.0
|
|
19
|
+
|
|
20
|
+
### Minor Changes
|
|
21
|
+
|
|
22
|
+
- 7e8e147: - Added `tr.forLang()` to get the translation for a specified language code
|
|
23
|
+
- Added `tr.getTranslations()` to return the translations object for the given or currently active language
|
|
24
|
+
|
|
3
25
|
## 7.2.2
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
## UserUtils
|
|
5
5
|
Lightweight library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and more.
|
|
6
6
|
|
|
7
|
-
Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and
|
|
7
|
+
Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via `@require`
|
|
8
8
|
If you like using this library, please consider [supporting the development ❤️](https://github.com/sponsors/Sv443)
|
|
9
9
|
|
|
10
10
|
<br>
|
|
@@ -63,12 +63,14 @@ View the documentation of previous major releases:
|
|
|
63
63
|
- [`randomizeArray()`](#randomizearray) - returns a copy of the array with its items in a random order
|
|
64
64
|
- [**Translation:**](#translation)
|
|
65
65
|
- [`tr()`](#tr) - simple translation of a string to another language
|
|
66
|
+
- [`tr.forLang()`](#trforlang) - specify a language besides the currently set one for the translation
|
|
66
67
|
- [`tr.addLanguage()`](#traddlanguage) - add a language and its translations
|
|
67
68
|
- [`tr.setLanguage()`](#trsetlanguage) - set the currently active language for translations
|
|
68
69
|
- [`tr.getLanguage()`](#trgetlanguage) - returns the currently active language
|
|
70
|
+
- [`tr.getTranslations()`](#trgettranslations) - returns the translations for the given language or the currently active one
|
|
69
71
|
- [**Colors:**](#colors)
|
|
70
|
-
- [`hexToRgb()`](#hextorgb) - convert a hex color string to an RGB
|
|
71
|
-
- [`rgbToHex()`](#rgbtohex) - convert RGB
|
|
72
|
+
- [`hexToRgb()`](#hextorgb) - convert a hex color string to an RGB or RGBA value tuple
|
|
73
|
+
- [`rgbToHex()`](#rgbtohex) - convert RGB or RGBA values to a hex color string
|
|
72
74
|
- [`lightenColor()`](#lightencolor) - lighten a CSS color string (hex, rgb or rgba) by a given percentage
|
|
73
75
|
- [`darkenColor()`](#darkencolor) - darken a CSS color string (hex, rgb or rgba) by a given percentage
|
|
74
76
|
- [**Utility types for TypeScript:**](#utility-types)
|
|
@@ -1960,6 +1962,39 @@ console.log(tr("welcome_name", "John")); // "Willkommen, John"
|
|
|
1960
1962
|
|
|
1961
1963
|
<br>
|
|
1962
1964
|
|
|
1965
|
+
### tr.forLang()
|
|
1966
|
+
Usage:
|
|
1967
|
+
```ts
|
|
1968
|
+
tr.forLang(language: string, key: string, ...values: Stringifiable[]): string
|
|
1969
|
+
```
|
|
1970
|
+
|
|
1971
|
+
Returns the translation of the passed key in the specified language. Otherwise behaves exactly like [`tr()`](#tr)
|
|
1972
|
+
This function does not change the currently active language set by [`tr.setLanguage()`](#trsetlanguage)
|
|
1973
|
+
|
|
1974
|
+
<details><summary><b>Example - click to view</b></summary>
|
|
1975
|
+
|
|
1976
|
+
```ts
|
|
1977
|
+
import { tr } from "@sv443-network/userutils";
|
|
1978
|
+
|
|
1979
|
+
tr.addLanguage("en", {
|
|
1980
|
+
"welcome_name": "Welcome, %1",
|
|
1981
|
+
});
|
|
1982
|
+
|
|
1983
|
+
tr.addLanguage("de", {
|
|
1984
|
+
"welcome_name": "Willkommen, %1",
|
|
1985
|
+
});
|
|
1986
|
+
|
|
1987
|
+
// the language is set to "en"
|
|
1988
|
+
tr.setLanguage("en");
|
|
1989
|
+
|
|
1990
|
+
console.log(tr("welcome_name", "John")); // "Welcome"
|
|
1991
|
+
// the language doesn't need to be changed:
|
|
1992
|
+
console.log(tr.forLang("de", "welcome_name", "John")); // "Willkommen, John"
|
|
1993
|
+
```
|
|
1994
|
+
</details>
|
|
1995
|
+
|
|
1996
|
+
<br>
|
|
1997
|
+
|
|
1963
1998
|
### tr.addLanguage()
|
|
1964
1999
|
Usage:
|
|
1965
2000
|
```ts
|
|
@@ -2080,6 +2115,37 @@ tr.getLanguage(): string | undefined
|
|
|
2080
2115
|
Returns the currently active language set by [`tr.setLanguage()`](#trsetlanguage)
|
|
2081
2116
|
If no language has been set yet, it will return undefined.
|
|
2082
2117
|
|
|
2118
|
+
<br>
|
|
2119
|
+
|
|
2120
|
+
### tr.getTranslations()
|
|
2121
|
+
Usage:
|
|
2122
|
+
```ts
|
|
2123
|
+
tr.getTranslations(language?: string): Record<string, string> | undefined
|
|
2124
|
+
```
|
|
2125
|
+
|
|
2126
|
+
Returns the translations of the specified language.
|
|
2127
|
+
If no language is specified, it will return the translations of the currently active language set by [`tr.setLanguage()`](#trsetlanguage)
|
|
2128
|
+
If no translations are found, it will return undefined.
|
|
2129
|
+
|
|
2130
|
+
<details><summary><b>Example - click to view</b></summary>
|
|
2131
|
+
|
|
2132
|
+
```ts
|
|
2133
|
+
import { tr } from "@sv443-network/userutils";
|
|
2134
|
+
|
|
2135
|
+
tr.addLanguage("en", {
|
|
2136
|
+
"welcome": "Welcome",
|
|
2137
|
+
});
|
|
2138
|
+
|
|
2139
|
+
console.log(tr.getTranslations()); // undefined
|
|
2140
|
+
tr.setLanguage("en");
|
|
2141
|
+
console.log(tr.getTranslations()); // { "welcome": "Welcome" }
|
|
2142
|
+
|
|
2143
|
+
console.log(tr.getTranslations("en")); // { "welcome": "Welcome" }
|
|
2144
|
+
|
|
2145
|
+
console.log(tr.getTranslations("de")); // undefined
|
|
2146
|
+
```
|
|
2147
|
+
</details>
|
|
2148
|
+
|
|
2083
2149
|
<br><br>
|
|
2084
2150
|
|
|
2085
2151
|
## Colors:
|
|
@@ -2088,20 +2154,23 @@ The color functions are used to manipulate and convert colors in various formats
|
|
|
2088
2154
|
### hexToRgb()
|
|
2089
2155
|
Usage:
|
|
2090
2156
|
```ts
|
|
2091
|
-
hexToRgb(hex: string): [red: number, green: number, blue: number]
|
|
2157
|
+
hexToRgb(hex: string): [red: number, green: number, blue: number, alpha?: number]
|
|
2092
2158
|
```
|
|
2093
2159
|
|
|
2094
|
-
Converts a hex color string to an RGB color tuple array.
|
|
2095
|
-
|
|
2160
|
+
Converts a hex color string to an RGB or RGBA color tuple array.
|
|
2161
|
+
The values of R, G and B will be in the range of 0-255, while the alpha value will be in the range of 0-1.
|
|
2162
|
+
Accepts the formats `#RRGGBB`, `#RRGGBBAA`, `#RGB` and `#RGBA`, with or without the hash symbol.
|
|
2096
2163
|
|
|
2097
2164
|
<details><summary><b>Example - click to view</b></summary>
|
|
2098
2165
|
|
|
2099
2166
|
```ts
|
|
2100
2167
|
import { hexToRgb } from "@sv443-network/userutils";
|
|
2101
2168
|
|
|
2102
|
-
hexToRgb("#
|
|
2103
|
-
hexToRgb("
|
|
2104
|
-
hexToRgb("
|
|
2169
|
+
hexToRgb("#aaff85aa"); // [170, 255, 133, 0.6666666666666666]
|
|
2170
|
+
hexToRgb("#ff0000"); // [255, 0, 0, undefined]
|
|
2171
|
+
hexToRgb("0032ef"); // [0, 50, 239, undefined]
|
|
2172
|
+
hexToRgb("#0f0"); // [0, 255, 0, undefined]
|
|
2173
|
+
hexToRgb("0f0f"); // [0, 255, 0, 1]
|
|
2105
2174
|
```
|
|
2106
2175
|
</details>
|
|
2107
2176
|
|
|
@@ -2110,10 +2179,10 @@ hexToRgb("#0f0"); // [0, 255, 0]
|
|
|
2110
2179
|
### rgbToHex()
|
|
2111
2180
|
Usage:
|
|
2112
2181
|
```ts
|
|
2113
|
-
rgbToHex(red: number, green: number, blue: number, withHash?: boolean, upperCase?: boolean): string
|
|
2182
|
+
rgbToHex(red: number, green: number, blue: number, alpha?: number, withHash?: boolean, upperCase?: boolean): string
|
|
2114
2183
|
```
|
|
2115
2184
|
|
|
2116
|
-
Converts RGB color values to a hex color string.
|
|
2185
|
+
Converts RGB or RGBA color values to a hex color string.
|
|
2117
2186
|
The `withHash` parameter determines if the hash symbol should be included in the output (true by default).
|
|
2118
2187
|
The `upperCase` parameter determines if the output should be in uppercase (false by default).
|
|
2119
2188
|
|
|
@@ -2122,9 +2191,9 @@ The `upperCase` parameter determines if the output should be in uppercase (false
|
|
|
2122
2191
|
```ts
|
|
2123
2192
|
import { rgbToHex } from "@sv443-network/userutils";
|
|
2124
2193
|
|
|
2125
|
-
rgbToHex(255, 0, 0);
|
|
2126
|
-
rgbToHex(255, 0, 0, false);
|
|
2127
|
-
rgbToHex(255, 0, 0, true, true); // "#FF0000"
|
|
2194
|
+
rgbToHex(255, 0, 0); // "#ff0000" (with hash symbol, lowercase)
|
|
2195
|
+
rgbToHex(255, 0, 0, 0.5, false); // "ff000080" (with alpha, no hash symbol, lowercase)
|
|
2196
|
+
rgbToHex(255, 0, 0, undefined, true, true); // "#FF0000" (no alpha, with hash symbol, uppercase)
|
|
2128
2197
|
```
|
|
2129
2198
|
</details>
|
|
2130
2199
|
|
|
@@ -2133,11 +2202,12 @@ rgbToHex(255, 0, 0, true, true); // "#FF0000"
|
|
|
2133
2202
|
### lightenColor()
|
|
2134
2203
|
Usage:
|
|
2135
2204
|
```ts
|
|
2136
|
-
lightenColor(color: string, percent: number): string
|
|
2205
|
+
lightenColor(color: string, percent: number, upperCase?: boolean): string
|
|
2137
2206
|
```
|
|
2138
2207
|
|
|
2139
2208
|
Lightens a CSS color value (in hex, RGB or RGBA format) by a given percentage.
|
|
2140
2209
|
Will not exceed the maximum range (00-FF or 0-255).
|
|
2210
|
+
If the `upperCase` parameter is set to true (default is false), the output will be in uppercase.
|
|
2141
2211
|
Throws an error if the color format is invalid or not supported.
|
|
2142
2212
|
|
|
2143
2213
|
<details><summary><b>Example - click to view</b></summary>
|
|
@@ -2146,6 +2216,7 @@ Throws an error if the color format is invalid or not supported.
|
|
|
2146
2216
|
import { lightenColor } from "@sv443-network/userutils";
|
|
2147
2217
|
|
|
2148
2218
|
lightenColor("#ff0000", 20); // "#ff3333"
|
|
2219
|
+
lightenColor("#ff0000", 20, true); // "#FF3333"
|
|
2149
2220
|
lightenColor("rgb(0, 255, 0)", 50); // "rgb(128, 255, 128)"
|
|
2150
2221
|
lightenColor("rgba(0, 255, 0, 0.5)", 50); // "rgba(128, 255, 128, 0.5)"
|
|
2151
2222
|
```
|
|
@@ -2156,11 +2227,12 @@ lightenColor("rgba(0, 255, 0, 0.5)", 50); // "rgba(128, 255, 128, 0.5)"
|
|
|
2156
2227
|
### darkenColor()
|
|
2157
2228
|
Usage:
|
|
2158
2229
|
```ts
|
|
2159
|
-
darkenColor(color: string, percent: number): string
|
|
2230
|
+
darkenColor(color: string, percent: number, upperCase?: boolean): string
|
|
2160
2231
|
```
|
|
2161
2232
|
|
|
2162
2233
|
Darkens a CSS color value (in hex, RGB or RGBA format) by a given percentage.
|
|
2163
2234
|
Will not exceed the maximum range (00-FF or 0-255).
|
|
2235
|
+
If the `upperCase` parameter is set to true (default is false), the output will be in uppercase.
|
|
2164
2236
|
Throws an error if the color format is invalid or not supported.
|
|
2165
2237
|
|
|
2166
2238
|
<details><summary><b>Example - click to view</b></summary>
|
|
@@ -2169,6 +2241,7 @@ Throws an error if the color format is invalid or not supported.
|
|
|
2169
2241
|
import { darkenColor } from "@sv443-network/userutils";
|
|
2170
2242
|
|
|
2171
2243
|
darkenColor("#ff0000", 20); // "#cc0000"
|
|
2244
|
+
darkenColor("#ff0000", 20, true); // "#CC0000"
|
|
2172
2245
|
darkenColor("rgb(0, 255, 0)", 50); // "rgb(0, 128, 0)"
|
|
2173
2246
|
darkenColor("rgba(0, 255, 0, 0.5)", 50); // "rgba(0, 128, 0, 0.5)"
|
|
2174
2247
|
```
|
package/dist/index.global.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// ==UserLibrary==
|
|
9
9
|
// @name UserUtils
|
|
10
10
|
// @description Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and more
|
|
11
|
-
// @version
|
|
11
|
+
// @version 8.0.0
|
|
12
12
|
// @license MIT
|
|
13
13
|
// @copyright Sv443 (https://github.com/Sv443)
|
|
14
14
|
|
|
@@ -130,21 +130,26 @@ var UserUtils = (function (exports) {
|
|
|
130
130
|
|
|
131
131
|
// lib/colors.ts
|
|
132
132
|
function hexToRgb(hex) {
|
|
133
|
-
hex = hex.trim();
|
|
134
|
-
const
|
|
133
|
+
hex = (hex.startsWith("#") ? hex.slice(1) : hex).trim();
|
|
134
|
+
const a = hex.length === 8 || hex.length === 4 ? parseInt(hex.slice(-(hex.length / 4)), 16) / (hex.length === 8 ? 255 : 15) : void 0;
|
|
135
|
+
if (!isNaN(Number(a)))
|
|
136
|
+
hex = hex.slice(0, -(hex.length / 4));
|
|
137
|
+
if (hex.length === 3 || hex.length === 4)
|
|
138
|
+
hex = hex.split("").map((c) => c + c).join("");
|
|
139
|
+
const bigint = parseInt(hex, 16);
|
|
135
140
|
const r = bigint >> 16 & 255;
|
|
136
141
|
const g = bigint >> 8 & 255;
|
|
137
142
|
const b = bigint & 255;
|
|
138
|
-
return [clamp(r, 0, 255), clamp(g, 0, 255), clamp(b, 0, 255)];
|
|
143
|
+
return [clamp(r, 0, 255), clamp(g, 0, 255), clamp(b, 0, 255), typeof a === "number" ? clamp(a, 0, 1) : void 0];
|
|
139
144
|
}
|
|
140
|
-
function rgbToHex(red, green, blue, withHash = true, upperCase = false) {
|
|
145
|
+
function rgbToHex(red, green, blue, alpha, withHash = true, upperCase = false) {
|
|
141
146
|
const toHexVal = (n) => clamp(Math.round(n), 0, 255).toString(16).padStart(2, "0")[upperCase ? "toUpperCase" : "toLowerCase"]();
|
|
142
|
-
return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}`;
|
|
147
|
+
return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}${alpha ? toHexVal(alpha * 255) : ""}`;
|
|
143
148
|
}
|
|
144
|
-
function lightenColor(color, percent) {
|
|
145
|
-
return darkenColor(color, percent * -1);
|
|
149
|
+
function lightenColor(color, percent, upperCase = false) {
|
|
150
|
+
return darkenColor(color, percent * -1, upperCase);
|
|
146
151
|
}
|
|
147
|
-
function darkenColor(color, percent) {
|
|
152
|
+
function darkenColor(color, percent, upperCase = false) {
|
|
148
153
|
var _a;
|
|
149
154
|
color = color.trim();
|
|
150
155
|
const darkenRgb = (r2, g2, b2, percent2) => {
|
|
@@ -154,23 +159,21 @@ var UserUtils = (function (exports) {
|
|
|
154
159
|
return [r2, g2, b2];
|
|
155
160
|
};
|
|
156
161
|
let r, g, b, a;
|
|
157
|
-
const isHexCol = color.match(/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/);
|
|
162
|
+
const isHexCol = color.match(/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/);
|
|
158
163
|
if (isHexCol)
|
|
159
|
-
[r, g, b] = hexToRgb(color);
|
|
164
|
+
[r, g, b, a] = hexToRgb(color);
|
|
160
165
|
else if (color.startsWith("rgb")) {
|
|
161
166
|
const rgbValues = (_a = color.match(/\d+(\.\d+)?/g)) == null ? void 0 : _a.map(Number);
|
|
162
|
-
if (rgbValues)
|
|
163
|
-
[r, g, b, a] = rgbValues;
|
|
164
|
-
else
|
|
167
|
+
if (!rgbValues)
|
|
165
168
|
throw new Error("Invalid RGB/RGBA color format");
|
|
169
|
+
[r, g, b, a] = rgbValues;
|
|
166
170
|
} else
|
|
167
171
|
throw new Error("Unsupported color format");
|
|
168
172
|
[r, g, b] = darkenRgb(r, g, b, percent);
|
|
169
|
-
const upperCase = color.match(/[A-F]/) !== null;
|
|
170
173
|
if (isHexCol)
|
|
171
|
-
return rgbToHex(r, g, b, color.startsWith("#"), upperCase);
|
|
174
|
+
return rgbToHex(r, g, b, a, color.startsWith("#"), upperCase);
|
|
172
175
|
else if (color.startsWith("rgba"))
|
|
173
|
-
return `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
176
|
+
return `rgba(${r}, ${g}, ${b}, ${a != null ? a : NaN})`;
|
|
174
177
|
else if (color.startsWith("rgb"))
|
|
175
178
|
return `rgb(${r}, ${g}, ${b})`;
|
|
176
179
|
else
|
|
@@ -1392,18 +1395,22 @@ Has: ${checksum}`);
|
|
|
1392
1395
|
// lib/translation.ts
|
|
1393
1396
|
var trans = {};
|
|
1394
1397
|
var curLang;
|
|
1395
|
-
|
|
1398
|
+
var trLang = (language, key, ...args) => {
|
|
1396
1399
|
var _a;
|
|
1397
|
-
if (!
|
|
1400
|
+
if (!language)
|
|
1398
1401
|
return key;
|
|
1399
|
-
const trText = (_a = trans[
|
|
1402
|
+
const trText = (_a = trans[language]) == null ? void 0 : _a[key];
|
|
1400
1403
|
if (!trText)
|
|
1401
1404
|
return key;
|
|
1402
1405
|
if (args.length > 0 && trText.match(/%\d/)) {
|
|
1403
1406
|
return insertValues(trText, ...args);
|
|
1404
1407
|
}
|
|
1405
1408
|
return trText;
|
|
1409
|
+
};
|
|
1410
|
+
function tr(key, ...args) {
|
|
1411
|
+
return trLang(curLang, key, ...args);
|
|
1406
1412
|
}
|
|
1413
|
+
tr.forLang = trLang;
|
|
1407
1414
|
tr.addLanguage = (language, translations) => {
|
|
1408
1415
|
trans[language] = translations;
|
|
1409
1416
|
};
|
|
@@ -1413,6 +1420,9 @@ Has: ${checksum}`);
|
|
|
1413
1420
|
tr.getLanguage = () => {
|
|
1414
1421
|
return curLang;
|
|
1415
1422
|
};
|
|
1423
|
+
tr.getTranslations = (language) => {
|
|
1424
|
+
return trans[language != null ? language : curLang];
|
|
1425
|
+
};
|
|
1416
1426
|
|
|
1417
1427
|
exports.DataStore = DataStore;
|
|
1418
1428
|
exports.DataStoreSerializer = DataStoreSerializer;
|
package/dist/index.js
CHANGED
|
@@ -110,21 +110,26 @@ function randomizeArray(array) {
|
|
|
110
110
|
|
|
111
111
|
// lib/colors.ts
|
|
112
112
|
function hexToRgb(hex) {
|
|
113
|
-
hex = hex.trim();
|
|
114
|
-
const
|
|
113
|
+
hex = (hex.startsWith("#") ? hex.slice(1) : hex).trim();
|
|
114
|
+
const a = hex.length === 8 || hex.length === 4 ? parseInt(hex.slice(-(hex.length / 4)), 16) / (hex.length === 8 ? 255 : 15) : void 0;
|
|
115
|
+
if (!isNaN(Number(a)))
|
|
116
|
+
hex = hex.slice(0, -(hex.length / 4));
|
|
117
|
+
if (hex.length === 3 || hex.length === 4)
|
|
118
|
+
hex = hex.split("").map((c) => c + c).join("");
|
|
119
|
+
const bigint = parseInt(hex, 16);
|
|
115
120
|
const r = bigint >> 16 & 255;
|
|
116
121
|
const g = bigint >> 8 & 255;
|
|
117
122
|
const b = bigint & 255;
|
|
118
|
-
return [clamp(r, 0, 255), clamp(g, 0, 255), clamp(b, 0, 255)];
|
|
123
|
+
return [clamp(r, 0, 255), clamp(g, 0, 255), clamp(b, 0, 255), typeof a === "number" ? clamp(a, 0, 1) : void 0];
|
|
119
124
|
}
|
|
120
|
-
function rgbToHex(red, green, blue, withHash = true, upperCase = false) {
|
|
125
|
+
function rgbToHex(red, green, blue, alpha, withHash = true, upperCase = false) {
|
|
121
126
|
const toHexVal = (n) => clamp(Math.round(n), 0, 255).toString(16).padStart(2, "0")[upperCase ? "toUpperCase" : "toLowerCase"]();
|
|
122
|
-
return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}`;
|
|
127
|
+
return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}${alpha ? toHexVal(alpha * 255) : ""}`;
|
|
123
128
|
}
|
|
124
|
-
function lightenColor(color, percent) {
|
|
125
|
-
return darkenColor(color, percent * -1);
|
|
129
|
+
function lightenColor(color, percent, upperCase = false) {
|
|
130
|
+
return darkenColor(color, percent * -1, upperCase);
|
|
126
131
|
}
|
|
127
|
-
function darkenColor(color, percent) {
|
|
132
|
+
function darkenColor(color, percent, upperCase = false) {
|
|
128
133
|
var _a;
|
|
129
134
|
color = color.trim();
|
|
130
135
|
const darkenRgb = (r2, g2, b2, percent2) => {
|
|
@@ -134,23 +139,21 @@ function darkenColor(color, percent) {
|
|
|
134
139
|
return [r2, g2, b2];
|
|
135
140
|
};
|
|
136
141
|
let r, g, b, a;
|
|
137
|
-
const isHexCol = color.match(/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/);
|
|
142
|
+
const isHexCol = color.match(/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/);
|
|
138
143
|
if (isHexCol)
|
|
139
|
-
[r, g, b] = hexToRgb(color);
|
|
144
|
+
[r, g, b, a] = hexToRgb(color);
|
|
140
145
|
else if (color.startsWith("rgb")) {
|
|
141
146
|
const rgbValues = (_a = color.match(/\d+(\.\d+)?/g)) == null ? void 0 : _a.map(Number);
|
|
142
|
-
if (rgbValues)
|
|
143
|
-
[r, g, b, a] = rgbValues;
|
|
144
|
-
else
|
|
147
|
+
if (!rgbValues)
|
|
145
148
|
throw new Error("Invalid RGB/RGBA color format");
|
|
149
|
+
[r, g, b, a] = rgbValues;
|
|
146
150
|
} else
|
|
147
151
|
throw new Error("Unsupported color format");
|
|
148
152
|
[r, g, b] = darkenRgb(r, g, b, percent);
|
|
149
|
-
const upperCase = color.match(/[A-F]/) !== null;
|
|
150
153
|
if (isHexCol)
|
|
151
|
-
return rgbToHex(r, g, b, color.startsWith("#"), upperCase);
|
|
154
|
+
return rgbToHex(r, g, b, a, color.startsWith("#"), upperCase);
|
|
152
155
|
else if (color.startsWith("rgba"))
|
|
153
|
-
return `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
156
|
+
return `rgba(${r}, ${g}, ${b}, ${a != null ? a : NaN})`;
|
|
154
157
|
else if (color.startsWith("rgb"))
|
|
155
158
|
return `rgb(${r}, ${g}, ${b})`;
|
|
156
159
|
else
|
|
@@ -1352,18 +1355,22 @@ var SelectorObserver = class {
|
|
|
1352
1355
|
// lib/translation.ts
|
|
1353
1356
|
var trans = {};
|
|
1354
1357
|
var curLang;
|
|
1355
|
-
|
|
1358
|
+
var trLang = (language, key, ...args) => {
|
|
1356
1359
|
var _a;
|
|
1357
|
-
if (!
|
|
1360
|
+
if (!language)
|
|
1358
1361
|
return key;
|
|
1359
|
-
const trText = (_a = trans[
|
|
1362
|
+
const trText = (_a = trans[language]) == null ? void 0 : _a[key];
|
|
1360
1363
|
if (!trText)
|
|
1361
1364
|
return key;
|
|
1362
1365
|
if (args.length > 0 && trText.match(/%\d/)) {
|
|
1363
1366
|
return insertValues(trText, ...args);
|
|
1364
1367
|
}
|
|
1365
1368
|
return trText;
|
|
1369
|
+
};
|
|
1370
|
+
function tr(key, ...args) {
|
|
1371
|
+
return trLang(curLang, key, ...args);
|
|
1366
1372
|
}
|
|
1373
|
+
tr.forLang = trLang;
|
|
1367
1374
|
tr.addLanguage = (language, translations) => {
|
|
1368
1375
|
trans[language] = translations;
|
|
1369
1376
|
};
|
|
@@ -1373,5 +1380,8 @@ tr.setLanguage = (language) => {
|
|
|
1373
1380
|
tr.getLanguage = () => {
|
|
1374
1381
|
return curLang;
|
|
1375
1382
|
};
|
|
1383
|
+
tr.getTranslations = (language) => {
|
|
1384
|
+
return trans[language != null ? language : curLang];
|
|
1385
|
+
};
|
|
1376
1386
|
|
|
1377
1387
|
export { DataStore, DataStoreSerializer, Dialog, NanoEmitter, SelectorObserver, addGlobalStyle, addParent, autoPlural, clamp, compress, computeHash, currentDialogId, darkenColor, debounce, decompress, defaultDialogCss, defaultStrings, fetchAdvanced, getSiblingsFrame, getUnsafeWindow, hexToRgb, insertValues, interceptEvent, interceptWindowEvent, isScrollable, lightenColor, mapRange, observeElementProp, openDialogs, openInNewTab, pauseFor, preloadImages, randRange, randomId, randomItem, randomItemIndex, randomizeArray, rgbToHex, setInnerHtmlUnsafe, takeRandomItem, tr };
|
package/dist/lib/colors.d.ts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
/** Converts a hex color string to a tuple of RGB values */
|
|
2
|
-
export declare function hexToRgb(hex: string): [red: number, green: number, blue: number];
|
|
3
|
-
/** Converts RGB values to a hex color string */
|
|
4
|
-
export declare function rgbToHex(red: number, green: number, blue: number, withHash?: boolean, upperCase?: boolean): string;
|
|
5
1
|
/**
|
|
6
|
-
*
|
|
2
|
+
* Converts a hex color string in the format `#RRGGBB`, `#RRGGBBAA` (or even `RRGGBB` and `RGB`) to a tuple.
|
|
3
|
+
* @returns Returns a tuple array where R, G and B are an integer from 0-255 and alpha is a float from 0 to 1, or undefined if no alpha channel exists.
|
|
4
|
+
*/
|
|
5
|
+
export declare function hexToRgb(hex: string): [red: number, green: number, blue: number, alpha?: number];
|
|
6
|
+
/** Converts RGB or RGBA number values to a hex color string in the format `#RRGGBB` or `#RRGGBBAA` */
|
|
7
|
+
export declare function rgbToHex(red: number, green: number, blue: number, alpha?: number, withHash?: boolean, upperCase?: boolean): string;
|
|
8
|
+
/**
|
|
9
|
+
* Lightens a CSS color value (in #HEX, rgb() or rgba() format) by a given percentage.
|
|
7
10
|
* Will not exceed the maximum range (00-FF or 0-255).
|
|
8
11
|
* @returns Returns the new color value in the same format as the input
|
|
9
12
|
* @throws Throws if the color format is invalid or not supported
|
|
10
13
|
*/
|
|
11
|
-
export declare function lightenColor(color: string, percent: number): string;
|
|
14
|
+
export declare function lightenColor(color: string, percent: number, upperCase?: boolean): string;
|
|
12
15
|
/**
|
|
13
|
-
* Darkens a CSS color value (in
|
|
16
|
+
* Darkens a CSS color value (in #HEX, rgb() or rgba() format) by a given percentage.
|
|
14
17
|
* Will not exceed the maximum range (00-FF or 0-255).
|
|
15
18
|
* @returns Returns the new color value in the same format as the input
|
|
16
19
|
* @throws Throws if the color format is invalid or not supported
|
|
17
20
|
*/
|
|
18
|
-
export declare function darkenColor(color: string, percent: number): string;
|
|
21
|
+
export declare function darkenColor(color: string, percent: number, upperCase?: boolean): string;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Stringifiable } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Returns the translated text for the specified key in the current language set by {@linkcode tr.setLanguage()}
|
|
4
|
-
*
|
|
4
|
+
* Use {@linkcode tr.forLang()} to get the translation for a specific language instead of the currently set one.
|
|
5
|
+
* If the key is not found in the currently set language, the key itself is returned.
|
|
5
6
|
*
|
|
6
7
|
* ⚠️ Remember to register a language with {@linkcode tr.addLanguage()} and set it as active with {@linkcode tr.setLanguage()} before using this function, otherwise it will always return the key itself.
|
|
7
8
|
* @param key Key of the translation to return
|
|
@@ -9,8 +10,10 @@ import type { Stringifiable } from "./types.js";
|
|
|
9
10
|
*/
|
|
10
11
|
declare function tr(key: string, ...args: Stringifiable[]): string;
|
|
11
12
|
declare namespace tr {
|
|
13
|
+
var forLang: (language: string, key: string, ...args: Stringifiable[]) => string;
|
|
12
14
|
var addLanguage: (language: string, translations: Record<string, string>) => void;
|
|
13
15
|
var setLanguage: (language: string) => void;
|
|
14
16
|
var getLanguage: () => string;
|
|
17
|
+
var getTranslations: (language?: string | undefined) => Record<string, string> | undefined;
|
|
15
18
|
}
|
|
16
19
|
export { tr };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sv443-network/userutils",
|
|
3
3
|
"libName": "UserUtils",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "8.0.0",
|
|
5
5
|
"description": "Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and more",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.mjs",
|