@sv443-network/userutils 8.3.3 → 9.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 CHANGED
@@ -1,5 +1,57 @@
1
1
  # @sv443-network/userutils
2
2
 
3
+ ## 9.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 9abfc6b: **BREAKING** - Reworked translation system:
8
+ - Removed `tr()`, `tr.setLanguage()` and `tr.getLanguage()`
9
+ - Renamed function `tr.addLanguage()` to `tr.addTranslations()`
10
+ - Removed `%n`-based argument insertion by default (re-enable explicitly with `tr.addTransform(tr.transforms.percent)`).
11
+ - Added ability for nested translation objects and object traversal via dot notation.
12
+ - Added functions:
13
+ - `tr.for()` - translates a key for the specified language.
14
+ - `tr.use()` - creates a translation function for the specified language for much easier usage.
15
+ - `tr.hasKey()` - checks if a key exists in the given language.
16
+ - `tr.setFallbackLanguage()` - sets the fallback language used when a key is not found in the given language.
17
+ - `tr.getFallbackLanguage()` - returns the fallback language.
18
+ - `tr.addTransform()` - adds a transform function to the translation system, allowing for custom argument insertion and much more.
19
+ - `tr.deleteTransform()` - removes a transform function.
20
+ - Added ability to specify transform patterns and functions for arbitrary modification of the translation string.
21
+ - Added transform for template literal syntax (e.g. `${keyName}`) with `tr.addTransform(tr.transforms.templateLiteral)`. This transform supports positional argument injection, as well as named arguments via an object with the same keys as in the template literal pattern. See the documentation for more information and a code example.
22
+ - Added TS type `TrKeys<T>` for extracting the keys of a translation object (both flat and nested).
23
+ - Fixed bug with resolving translations for flat objects.
24
+ - d0737dc: **BREAKING** - Reworked debounce system:
25
+ - Edge types `rising` and `falling` have been removed.
26
+ - Added new edge types `immediate` and `idle` with new behavior.
27
+ - `immediate` (default & recommended) will trigger immediately, then queue all subsequent calls until the timeout has passed.
28
+ - `idle` will trigger the last queued call only after there haven't been any subsequent calls for the specified timeout.
29
+ - Added `Debouncer` class for more advanced control over debouncing, and with that the following changes:
30
+ - Ability to attach and manage multiple listeners.
31
+ - Inherits from NanoEmitter, allowing event-based debouncing.
32
+ - Can be inherited by your own classes for built-in debouncing.
33
+ - `debounce()` function can still be called as usual (after replacing the edge type names with the new ones). Internally, it will instantiate a `Debouncer` instance, which is available via the `debouncer` property on the returned function.
34
+ - Reduced default timeout from 300ms to 200ms.
35
+
36
+ ### Minor Changes
37
+
38
+ - cd241b0: Added `additionalProps` parameter to `openInNewTab()` to add or overwrite anchor element props (only if `GM.openInTab()` is unavailable)
39
+ - d0737dc: Moved documentation to separate file `docs.md` to speed up `README.md` load time.
40
+
41
+ ### Patch Changes
42
+
43
+ - 1a754db: Fixed newlines being collapsed in TSDoc comments.
44
+ - d0737dc: Fixed `randRange()` with `enhancedEntropy = true` only returning the first digit.
45
+
46
+ ## 8.4.0
47
+
48
+ ### Minor Changes
49
+
50
+ - e10d629: Added function `digitCount()` to calculate the amount of digits in the passed number
51
+ - 949877a: Added support for nested objects in translations (e.g. `tr("foo.bar.baz")`)
52
+ - 52d392a: Added `ValueGen` and `StringGen` types with accompanying `consumeGen()` and `consumeStringGen()` functions to allow for super flexible typing and declaration of values
53
+ - bbce0e1: Added overload to `clamp()` without `min` parameter
54
+
3
55
  ## 8.3.3
4
56
 
5
57
  ### Patch Changes
@@ -236,11 +288,11 @@
236
288
  - separate instances can have different MutationObserver options set to further reduce performance impact
237
289
  - the separation into instances allows for a new "chaining" paradigm where selector listeners are only added and checked for once they are actually needed (see examples in the documentation)
238
290
  - when using chaining, separate instances can be created and have listeners added to them before their base element is available in the DOM tree
239
- - every listener can have a set debounce time, so that it doesn't get called too often (works the same as the [`debounce()` function](https://github.com/Sv443-Network/UserUtils/blob/main/README.md#debounce), but is disabled by default)
291
+ - every listener can have a set debounce time, so that it doesn't get called too often (works the same as the [`debounce()` function](https://github.com/Sv443-Network/UserUtils/blob/v3.0.0/README.md#debounce), but is disabled by default)
240
292
  - there are now multiple methods to get and delete specific listeners
241
293
 
242
294
  The `SelectorObserver.addListener()` method is backwards compatible with the old `onSelector()` function, so you can just add the class instance in front (for full backwards compat use `document.body` for the `baseElement` parameter of the constructor), then change the old function's name and it should work as before.
243
- For more info and examples, please view the [SelectorObserver documentation](https://github.com/Sv443-Network/UserUtils/blob/main/README.md#selectorobserver)
295
+ For more info and examples, please view the [SelectorObserver documentation](https://github.com/Sv443-Network/UserUtils/blob/v3.0.0/README.md#selectorobserver)
244
296
 
245
297
  ### Minor Changes
246
298
 
@@ -0,0 +1,191 @@
1
+ ## UserUtils
2
+ 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 much more.
3
+ Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via `@require`
4
+ The library also works in any DOM environment without the [GreaseMonkey API](https://wiki.greasespot.net/Greasemonkey_Manual:API), but some features will be unavailable or limited.
5
+
6
+ You may want to check out my [template for userscripts in TypeScript](https://github.com/Sv443/Userscript.ts) that you can use to get started quickly. It also includes this library by default.
7
+
8
+ If you like using this library, please consider [supporting the development ❤️](https://github.com/sponsors/Sv443)
9
+
10
+ <br>
11
+
12
+ [![minified bundle size badge](https://badgen.net/bundlephobia/min/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
13
+ [![minified and gzipped bundle size badge](https://badgen.net/bundlephobia/minzip/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
14
+ [![tree shaking support badge](https://badgen.net/bundlephobia/tree-shaking/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
15
+
16
+ [![github stargazers badge](https://badgen.net/github/stars/Sv443-Network/UserUtils?icon=github)](https://github.com/Sv443-Network/UserUtils/stargazers)
17
+ [![discord server badge](https://badgen.net/discord/online-members/aBH4uRG?icon=discord)](https://dc.sv443.net/)
18
+
19
+ <br>
20
+
21
+ ## &gt; [Full documentation on GitHub](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#readme) &lt;
22
+
23
+ <br>
24
+
25
+ <span style="font-size: 0.8em;">
26
+
27
+ View the documentation of previous major releases:
28
+ <a href="https://github.com/Sv443-Network/UserUtils/blob/v8.0.0/README.md" rel="noopener noreferrer">8.0.0</a>, <a href="https://github.com/Sv443-Network/UserUtils/blob/v7.0.0/README.md" rel="noopener noreferrer">7.0.0</a>, <a href="https://github.com/Sv443-Network/UserUtils/blob/v6.0.0/README.md" rel="noopener noreferrer">6.0.0</a>, <a href="https://github.com/Sv443-Network/UserUtils/blob/v5.0.0/README.md" rel="noopener noreferrer">5.0.0</a>, <a href="https://github.com/Sv443-Network/UserUtils/blob/v4.0.0/README.md" rel="noopener noreferrer">4.0.0</a>, <a href="https://github.com/Sv443-Network/UserUtils/blob/v3.0.0/README.md" rel="noopener noreferrer">3.0.0</a>, <a href="https://github.com/Sv443-Network/UserUtils/blob/v2.0.0/README.md" rel="noopener noreferrer">2.0.0</a>, <a href="https://github.com/Sv443-Network/UserUtils/blob/v1.0.0/README.md" rel="noopener noreferrer">1.0.0</a>, <a href="https://github.com/Sv443-Network/UserUtils/blob/v0.5.3/README.md" rel="noopener noreferrer">0.5.3</a>
29
+ <!-- <a href="https://github.com/Sv443-Network/UserUtils/blob/vX.0.0/docs.md" rel="noopener noreferrer">X.0.0</a>, -->
30
+ </span>
31
+
32
+ <br>
33
+
34
+ <!-- https://github.com/Sv443-Network/UserUtils < #foo -->
35
+ ## Feature Summary:
36
+ - **DOM:**
37
+ - [`SelectorObserver`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#selectorobserver) - class that manages listeners that are called when selectors are found in the DOM
38
+ - [`getUnsafeWindow()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#getunsafewindow) - get the unsafeWindow object or fall back to the regular window object
39
+ - [`addParent()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#addparent) - add a parent element around another element
40
+ - [`addGlobalStyle()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#addglobalstyle) - add a global style to the page
41
+ - [`preloadImages()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#preloadimages) - preload images into the browser cache for faster loading later on
42
+ - [`openInNewTab()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#openinnewtab) - open a link in a new tab
43
+ - [`interceptEvent()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#interceptevent) - conditionally intercepts events registered by `addEventListener()` on any given EventTarget object
44
+ - [`interceptWindowEvent()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#interceptwindowevent) - conditionally intercepts events registered by `addEventListener()` on the window object
45
+ - [`isScrollable()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#isscrollable) - check if an element has a horizontal or vertical scroll bar
46
+ - [`observeElementProp()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#observeelementprop) - observe changes to an element's property that can't be observed with MutationObserver
47
+ - [`getSiblingsFrame()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#getsiblingsframe) - returns a frame of an element's siblings, with a given alignment and size
48
+ - [`setInnerHtmlUnsafe()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#setinnerhtmlunsafe) - set the innerHTML of an element using a [Trusted Types policy](https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API) without sanitizing or escaping it
49
+ - **Math:**
50
+ - [`clamp()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#clamp) - constrain a number between a min and max value
51
+ - [`mapRange()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#maprange) - map a number from one range to the same spot in another range
52
+ - [`randRange()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#randrange) - generate a random number between a min and max boundary
53
+ - [`digitCount()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#digitcount) - calculate the amount of digits in a number
54
+ - **Misc:**
55
+ - [`DataStore`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#datastore) - class that manages a hybrid sync & async persistent JSON database, including data migration
56
+ - [`DataStoreSerializer`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#datastoreserializer) - class for importing & exporting data of multiple DataStore instances, including compression, checksumming and running migrations
57
+ - [`Dialog`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#dialog) - class for creating custom modal dialogs with a promise-based API and a generic, default style
58
+ - [`NanoEmitter`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#nanoemitter) - tiny event emitter class with a focus on performance and simplicity (based on [nanoevents](https://npmjs.com/package/nanoevents))
59
+ - [`Debouncer`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debouncer) - class for debouncing function calls with a given timeout
60
+ - [`debounce()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debounce) - function wrapper for the Debouncer class for easier usage
61
+ - [`autoPlural()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#autoplural) - automatically pluralize a string
62
+ - [`pauseFor()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#pausefor) - pause the execution of a function for a given amount of time
63
+ - [`fetchAdvanced()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#fetchadvanced) - wrapper around the fetch API with a timeout option
64
+ - [`insertValues()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#insertvalues) - insert values into a string at specified placeholders
65
+ - [`compress()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#compress) - compress a string with Gzip or Deflate
66
+ - [`decompress()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#decompress) - decompress a previously compressed string
67
+ - [`computeHash()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#computehash) - compute the hash / checksum of a string or ArrayBuffer
68
+ - [`randomId()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#randomid) - generate a random ID of a given length and radix
69
+ - **Arrays:**
70
+ - [`randomItem()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#randomitem) - returns a random item from an array
71
+ - [`randomItemIndex()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#randomitemindex) - returns a tuple of a random item and its index from an array
72
+ - [`takeRandomItem()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#takerandomitem) - returns a random item from an array and mutates it to remove the item
73
+ - [`randomizeArray()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#randomizearray) - returns a copy of the array with its items in a random order
74
+ - **Translation:**
75
+ - [`tr()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#tr) - simple JSON-based translation system with placeholder and nesting support
76
+ - [`tr.forLang()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#trforlang) - translate with the specified language instead of the currently active one
77
+ - [`tr.setLanguage()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#trsetlanguage) - set the currently active language for translations
78
+ - [`tr.getLanguage()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#trgetlanguage) - returns the currently active language
79
+ - [`tr.addTranslations()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#traddtranslations) - add a language and its translations
80
+ - [`tr.getTranslations()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#trgettranslations) - returns the translations for the given language or the currently active one
81
+ - [`tr.deleteTranslations()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#trdeletetranslations) - delete the translations for the given language or the active one
82
+ - [`tr.hasKey()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#trhaskey) - check if a translation key exists for the given or active language
83
+ - [`tr.addTransform()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#traddtransform) - add a transformation function to dynamically modify the translation value
84
+ - [`tr.deleteTransform()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#trdeletetransform) - delete a transformation function that was previously added
85
+ - **Colors:**
86
+ - [`hexToRgb()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#hextorgb) - convert a hex color string to an RGB or RGBA value tuple
87
+ - [`rgbToHex()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#rgbtohex) - convert RGB or RGBA values to a hex color string
88
+ - [`lightenColor()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#lightencolor) - lighten a CSS color string (hex, rgb or rgba) by a given percentage
89
+ - [`darkenColor()`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#darkencolor) - darken a CSS color string (hex, rgb or rgba) by a given percentage
90
+ - **Utility types for TypeScript:**
91
+ - [`Stringifiable`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#stringifiable) - any value that is a string or can be converted to one (implicitly or explicitly)
92
+ - [`NonEmptyArray`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#nonemptyarray) - any array that should have at least one item
93
+ - [`NonEmptyString`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#nonemptystring) - any string that should have at least one character
94
+ - [`LooseUnion`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#looseunion) - a union that gives autocomplete in the IDE but also allows any other value of the same type
95
+ - [`Prettify`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#prettify) - expands a complex type into a more readable format while keeping functionality the same
96
+ - [`ValueGen`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#valuegen) - a "generator" value that allows for super flexible value typing and declaration
97
+ - [`StringGen`](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#stringgen) - a "generator" string that allows for super flexible string typing and declaration, including enhanced support for unions
98
+
99
+ <br><br>
100
+
101
+ ## Installation:
102
+ Shameless plug: I made a [template for userscripts in TypeScript](https://github.com/Sv443/Userscript.ts) that you can use to get started quickly. It also includes this library by default.
103
+
104
+ - If you are using a bundler (like webpack, rollup, vite, etc.), you can install this package in one of the following ways:
105
+ ```
106
+ npm i @sv443-network/userutils
107
+ pnpm i @sv443-network/userutils
108
+ yarn add @sv443-network/userutils
109
+ npx jsr install @sv443-network/userutils
110
+ deno add jsr:@sv443-network/userutils
111
+ ```
112
+ Then import it in your script as usual:
113
+
114
+ ```ts
115
+ // on Node:
116
+ import { addGlobalStyle } from "@sv443-network/userutils";
117
+
118
+ // on Deno:
119
+ import { addGlobalStyle } from "jsr:@sv443-network/userutils";
120
+
121
+ // you can also import the entire library as an object (not recommended because of worse treeshaking support):
122
+ import * as UserUtils from "@sv443-network/userutils";
123
+ ```
124
+
125
+
126
+ <br>
127
+
128
+ - If you are not using a bundler, want to reduce the size of your userscript, or declared the package as external in your bundler, you can include the latest release by adding one of these directives to the userscript header, depending on your preferred CDN:
129
+ Versioned (recommended):
130
+
131
+ ```
132
+ // @require https://cdn.jsdelivr.net/npm/@sv443-network/userutils@INSERT_VERSION/dist/index.global.js
133
+ // @require https://unpkg.com/@sv443-network/userutils@INSERT_VERSION/dist/index.global.js
134
+ ```
135
+ Non-versioned (not recommended because auto-updating):
136
+
137
+ ```
138
+ // @require https://update.greasyfork.org/scripts/472956/UserUtils.js
139
+ // @require https://openuserjs.org/src/libs/Sv443/UserUtils.js
140
+ ```
141
+
142
+ > **Note:**
143
+ > In order for your userscript not to break on a major library update, use one the versioned URLs above after replacing `INSERT_VERSION` with the desired version (e.g. `8.3.2`) or the versioned URL that's shown at the top of the [GreasyFork page.](https://greasyfork.org/scripts/472956-userutils)
144
+
145
+ <br>
146
+
147
+ - Then, access the functions on the global variable `UserUtils`:
148
+ ```ts
149
+ UserUtils.addGlobalStyle("body { background-color: red; }");
150
+
151
+ // or using object destructuring:
152
+
153
+ const { clamp } = UserUtils;
154
+ console.log(clamp(1, 5, 10)); // 5
155
+ ```
156
+
157
+ <br>
158
+
159
+ - If you're using TypeScript and it complains about the missing global variable `UserUtils`, install the library using the package manager of your choice and add the following inside a `.d.ts` file somewhere in the directory (or a subdirectory) defined in your `tsconfig.json`'s `baseUrl` option or `include` array:
160
+ ```ts
161
+ declare const UserUtils: typeof import("@sv443-network/userutils");
162
+
163
+ declare global {
164
+ interface Window {
165
+ UserUtils: typeof UserUtils;
166
+ }
167
+ }
168
+ ```
169
+
170
+ <br>
171
+
172
+ - If you're using a linter like ESLint, it might complain about the global variable `UserUtils` not being defined. To fix this, add the following to your ESLint configuration file:
173
+ ```json
174
+ "globals": {
175
+ "UserUtils": "readonly"
176
+ }
177
+ ```
178
+
179
+ <br><br>
180
+
181
+ <!-- #region License -->
182
+ ## License:
183
+ This library is licensed under the MIT License.
184
+ See the [license file](./LICENSE.txt) for details.
185
+
186
+ <br><br>
187
+
188
+ ---
189
+
190
+ Made with ❤️ by [Sv443](https://github.com/Sv443)
191
+ If you like this library, please consider [supporting the development](https://github.com/sponsors/Sv443)