@sv443-network/userutils 8.2.0 → 8.3.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 +6 -0
- package/README.md +10 -2
- package/dist/index.global.js +11 -9
- package/dist/index.js +10 -8
- package/dist/lib/DataStore.d.ts +3 -2
- package/dist/lib/SelectorObserver.d.ts +3 -2
- package/dist/lib/misc.d.ts +3 -3
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1775,24 +1775,32 @@ fetchAdvanced(input: string | Request | URL, options?: {
|
|
|
1775
1775
|
|
|
1776
1776
|
A drop-in replacement for the native `fetch()` function that adds options like a timeout property.
|
|
1777
1777
|
The timeout will default to 10 seconds if left undefined. Set it to a negative number to disable the timeout.
|
|
1778
|
-
Note that the `signal` option will be overwritten if passed.
|
|
1779
1778
|
|
|
1780
1779
|
<details><summary><b>Example - click to view</b></summary>
|
|
1781
1780
|
|
|
1782
1781
|
```ts
|
|
1783
1782
|
import { fetchAdvanced } from "@sv443-network/userutils";
|
|
1784
1783
|
|
|
1784
|
+
const { signal, abort } = new AbortController();
|
|
1785
|
+
|
|
1785
1786
|
fetchAdvanced("https://jokeapi.dev/joke/Any?safe-mode", {
|
|
1787
|
+
// times out after 5 seconds:
|
|
1786
1788
|
timeout: 5000,
|
|
1787
|
-
// also accepts any other fetch options like headers:
|
|
1789
|
+
// also accepts any other fetch options like headers and signal:
|
|
1788
1790
|
headers: {
|
|
1789
1791
|
"Accept": "text/plain",
|
|
1790
1792
|
},
|
|
1793
|
+
// makes the request abortable:
|
|
1794
|
+
signal,
|
|
1791
1795
|
}).then(async (response) => {
|
|
1792
1796
|
console.log("Fetch data:", await response.text());
|
|
1793
1797
|
}).catch((err) => {
|
|
1794
1798
|
console.error("Fetch error:", err);
|
|
1795
1799
|
});
|
|
1800
|
+
|
|
1801
|
+
document.querySelector("button#cancel")?.addEventListener("click", () => {
|
|
1802
|
+
abort();
|
|
1803
|
+
});
|
|
1796
1804
|
```
|
|
1797
1805
|
</details>
|
|
1798
1806
|
|
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 8.
|
|
11
|
+
// @version 8.3.0
|
|
12
12
|
// @license MIT
|
|
13
13
|
// @copyright Sv443 (https://github.com/Sv443)
|
|
14
14
|
|
|
@@ -1271,27 +1271,29 @@ Has: ${checksum}`);
|
|
|
1271
1271
|
});
|
|
1272
1272
|
}
|
|
1273
1273
|
function debounce(func, timeout = 300, edge = "falling") {
|
|
1274
|
-
let
|
|
1274
|
+
let id;
|
|
1275
1275
|
return function(...args) {
|
|
1276
1276
|
if (edge === "rising") {
|
|
1277
|
-
if (!
|
|
1277
|
+
if (!id) {
|
|
1278
1278
|
func.apply(this, args);
|
|
1279
|
-
|
|
1279
|
+
id = setTimeout(() => id = void 0, timeout);
|
|
1280
1280
|
}
|
|
1281
1281
|
} else {
|
|
1282
|
-
clearTimeout(
|
|
1283
|
-
|
|
1282
|
+
clearTimeout(id);
|
|
1283
|
+
id = setTimeout(() => func.apply(this, args), timeout);
|
|
1284
1284
|
}
|
|
1285
1285
|
};
|
|
1286
1286
|
}
|
|
1287
1287
|
function fetchAdvanced(_0) {
|
|
1288
1288
|
return __async(this, arguments, function* (input, options = {}) {
|
|
1289
|
+
var _a;
|
|
1289
1290
|
const { timeout = 1e4 } = options;
|
|
1291
|
+
const { signal, abort } = new AbortController();
|
|
1292
|
+
(_a = options.signal) == null ? void 0 : _a.addEventListener("abort", abort);
|
|
1290
1293
|
let signalOpts = {}, id = void 0;
|
|
1291
1294
|
if (timeout >= 0) {
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
signalOpts = { signal: controller.signal };
|
|
1295
|
+
id = setTimeout(() => abort(), timeout);
|
|
1296
|
+
signalOpts = { signal };
|
|
1295
1297
|
}
|
|
1296
1298
|
try {
|
|
1297
1299
|
const res = yield fetch(input, __spreadValues(__spreadValues({}, options), signalOpts));
|
package/dist/index.js
CHANGED
|
@@ -1231,27 +1231,29 @@ function pauseFor(time) {
|
|
|
1231
1231
|
});
|
|
1232
1232
|
}
|
|
1233
1233
|
function debounce(func, timeout = 300, edge = "falling") {
|
|
1234
|
-
let
|
|
1234
|
+
let id;
|
|
1235
1235
|
return function(...args) {
|
|
1236
1236
|
if (edge === "rising") {
|
|
1237
|
-
if (!
|
|
1237
|
+
if (!id) {
|
|
1238
1238
|
func.apply(this, args);
|
|
1239
|
-
|
|
1239
|
+
id = setTimeout(() => id = void 0, timeout);
|
|
1240
1240
|
}
|
|
1241
1241
|
} else {
|
|
1242
|
-
clearTimeout(
|
|
1243
|
-
|
|
1242
|
+
clearTimeout(id);
|
|
1243
|
+
id = setTimeout(() => func.apply(this, args), timeout);
|
|
1244
1244
|
}
|
|
1245
1245
|
};
|
|
1246
1246
|
}
|
|
1247
1247
|
function fetchAdvanced(_0) {
|
|
1248
1248
|
return __async(this, arguments, function* (input, options = {}) {
|
|
1249
|
+
var _a;
|
|
1249
1250
|
const { timeout = 1e4 } = options;
|
|
1251
|
+
const { signal, abort } = new AbortController();
|
|
1252
|
+
(_a = options.signal) == null ? void 0 : _a.addEventListener("abort", abort);
|
|
1250
1253
|
let signalOpts = {}, id = void 0;
|
|
1251
1254
|
if (timeout >= 0) {
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
signalOpts = { signal: controller.signal };
|
|
1255
|
+
id = setTimeout(() => abort(), timeout);
|
|
1256
|
+
signalOpts = { signal };
|
|
1255
1257
|
}
|
|
1256
1258
|
try {
|
|
1257
1259
|
const res = yield fetch(input, __spreadValues(__spreadValues({}, options), signalOpts));
|
package/dist/lib/DataStore.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/// <reference types="greasemonkey" />
|
|
2
|
+
import type { Prettify } from "./types.js";
|
|
2
3
|
/** Function that takes the data in the old format and returns the data in the new format. Also supports an asynchronous migration. */
|
|
3
4
|
type MigrationFunc = (oldData: any) => any | Promise<any>;
|
|
4
5
|
/** Dictionary of format version numbers and the function that migrates to them from the previous whole integer */
|
|
5
6
|
export type DataMigrationsDict = Record<number, MigrationFunc>;
|
|
6
7
|
/** Options for the DataStore instance */
|
|
7
|
-
export type DataStoreOptions<TData> = {
|
|
8
|
+
export type DataStoreOptions<TData> = Prettify<{
|
|
8
9
|
/**
|
|
9
10
|
* A unique internal ID for this data store.
|
|
10
11
|
* To avoid conflicts with other scripts, it is recommended to use a prefix that is unique to your script.
|
|
@@ -66,7 +67,7 @@ export type DataStoreOptions<TData> = {
|
|
|
66
67
|
} | {
|
|
67
68
|
encodeData?: never;
|
|
68
69
|
decodeData?: never;
|
|
69
|
-
})
|
|
70
|
+
})>;
|
|
70
71
|
/**
|
|
71
72
|
* Manages a hybrid synchronous & asynchronous persistent JSON database that is cached in memory and persistently saved across sessions using [GM storage](https://wiki.greasespot.net/GM.setValue) (default), [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) or [sessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage).
|
|
72
73
|
* Supports migrating data from older format versions to newer ones and populating the cache with default data if no persistent data is found.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { Prettify } from "./types.js";
|
|
1
2
|
/** Options for the `onSelector()` method of {@linkcode SelectorObserver} */
|
|
2
|
-
export type SelectorListenerOptions<TElem extends Element = HTMLElement> = SelectorOptionsOne<TElem> | SelectorOptionsAll<TElem
|
|
3
|
+
export type SelectorListenerOptions<TElem extends Element = HTMLElement> = Prettify<SelectorOptionsOne<TElem> | SelectorOptionsAll<TElem>>;
|
|
3
4
|
type SelectorOptionsOne<TElem extends Element> = SelectorOptionsCommon & {
|
|
4
5
|
/** Whether to use `querySelectorAll()` instead - default is false */
|
|
5
6
|
all?: false;
|
|
@@ -36,7 +37,7 @@ export type SelectorObserverOptions = {
|
|
|
36
37
|
/** If set to a number, the checks will be run on interval instead of on mutation events - in that case all MutationObserverInit props will be ignored */
|
|
37
38
|
checkInterval?: number;
|
|
38
39
|
};
|
|
39
|
-
export type SelectorObserverConstructorOptions =
|
|
40
|
+
export type SelectorObserverConstructorOptions = Prettify<SelectorObserverOptions & MutationObserverInit>;
|
|
40
41
|
/** Observes the children of the given element for changes */
|
|
41
42
|
export declare class SelectorObserver {
|
|
42
43
|
private enabled;
|
package/dist/lib/misc.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Stringifiable } from "./types.js";
|
|
1
|
+
import type { Prettify, Stringifiable } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Automatically appends an `s` to the passed {@linkcode word}, if {@linkcode num} is not equal to 1
|
|
4
4
|
* @param word A word in singular form, to auto-convert to plural
|
|
@@ -24,9 +24,9 @@ export declare function pauseFor(time: number): Promise<void>;
|
|
|
24
24
|
export declare function debounce<TFunc extends (...args: TArgs[]) => void, // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
25
|
TArgs = any>(func: TFunc, timeout?: number, edge?: "rising" | "falling"): (...args: TArgs[]) => void;
|
|
26
26
|
/** Options for the `fetchAdvanced()` function */
|
|
27
|
-
export type FetchAdvancedOpts =
|
|
27
|
+
export type FetchAdvancedOpts = Prettify<Partial<{
|
|
28
28
|
/** Timeout in milliseconds after which the fetch call will be canceled with an AbortController signal */
|
|
29
29
|
timeout: number;
|
|
30
|
-
}
|
|
30
|
+
}> & RequestInit>;
|
|
31
31
|
/** Calls the fetch API with special options like a timeout */
|
|
32
32
|
export declare function fetchAdvanced(input: RequestInfo | URL, options?: FetchAdvancedOpts): Promise<Response>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sv443-network/userutils",
|
|
3
3
|
"libName": "UserUtils",
|
|
4
|
-
"version": "8.
|
|
4
|
+
"version": "8.3.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.js",
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"dev-all": "npm run build-all -- --watch",
|
|
27
27
|
"update-jsr-version": "npm run node-ts -- ./tools/update-jsr-version.mts",
|
|
28
28
|
"publish-package": "changeset publish",
|
|
29
|
-
"publish-package-jsr": "npm run update-jsr-version && npx jsr publish",
|
|
29
|
+
"publish-package-jsr": "npm run update-jsr-version && npx jsr publish --allow-dirty",
|
|
30
|
+
"change": "changeset",
|
|
30
31
|
"node-ts": "node --no-warnings=ExperimentalWarning --enable-source-maps --loader ts-node/esm",
|
|
31
32
|
"test-serve": "npm run node-ts -- ./test/TestPage/server.mts",
|
|
32
33
|
"test-dev": "cd test/TestScript && npm run dev",
|