@sv443-network/userutils 2.0.1 → 4.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 +49 -0
- package/README.md +511 -218
- package/dist/index.global.js +198 -114
- package/dist/index.js +197 -113
- package/dist/index.mjs +194 -109
- package/dist/lib/{config.d.ts → ConfigManager.d.ts} +8 -5
- package/dist/lib/SelectorObserver.d.ts +84 -0
- package/dist/lib/array.d.ts +6 -4
- package/dist/lib/dom.d.ts +8 -53
- package/dist/lib/index.d.ts +2 -2
- package/dist/lib/math.d.ts +12 -5
- package/dist/lib/misc.d.ts +26 -5
- package/dist/lib/translation.d.ts +2 -2
- package/package.json +10 -4
- package/dist/lib/onSelector.d.ts +0 -40
package/dist/index.global.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// ==UserLibrary==
|
|
10
10
|
// @name UserUtils
|
|
11
11
|
// @description Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, manage persistent user configurations, modify the DOM more easily and more
|
|
12
|
-
// @version
|
|
12
|
+
// @version 4.0.0
|
|
13
13
|
// @license MIT
|
|
14
14
|
// @copyright Sv443 (https://github.com/Sv443)
|
|
15
15
|
|
|
@@ -69,28 +69,36 @@ var UserUtils = (function (exports) {
|
|
|
69
69
|
function clamp(value, min, max) {
|
|
70
70
|
return Math.max(Math.min(value, max), min);
|
|
71
71
|
}
|
|
72
|
-
function mapRange(value,
|
|
73
|
-
if (Number(
|
|
74
|
-
return value * (
|
|
75
|
-
return (value -
|
|
72
|
+
function mapRange(value, range1min, range1max, range2min, range2max) {
|
|
73
|
+
if (Number(range1min) === 0 && Number(range2min) === 0)
|
|
74
|
+
return value * (range2max / range1max);
|
|
75
|
+
return (value - range1min) * ((range2max - range2min) / (range1max - range1min)) + range2min;
|
|
76
76
|
}
|
|
77
77
|
function randRange(...args) {
|
|
78
78
|
let min, max;
|
|
79
|
-
if (typeof args[0] === "number" && typeof args[1] === "number")
|
|
79
|
+
if (typeof args[0] === "number" && typeof args[1] === "number")
|
|
80
80
|
[min, max] = args;
|
|
81
|
-
|
|
81
|
+
else if (typeof args[0] === "number" && typeof args[1] !== "number") {
|
|
82
82
|
min = 0;
|
|
83
|
-
max = args
|
|
83
|
+
[max] = args;
|
|
84
84
|
} else
|
|
85
85
|
throw new TypeError(`Wrong parameter(s) provided - expected: "number" and "number|undefined", got: "${typeof args[0]}" and "${typeof args[1]}"`);
|
|
86
86
|
min = Number(min);
|
|
87
87
|
max = Number(max);
|
|
88
88
|
if (isNaN(min) || isNaN(max))
|
|
89
|
-
|
|
89
|
+
return NaN;
|
|
90
90
|
if (min > max)
|
|
91
91
|
throw new TypeError(`Parameter "min" can't be bigger than "max"`);
|
|
92
92
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
93
93
|
}
|
|
94
|
+
function randomId(length = 16, radix = 16) {
|
|
95
|
+
const arr = new Uint8Array(length);
|
|
96
|
+
crypto.getRandomValues(arr);
|
|
97
|
+
return Array.from(
|
|
98
|
+
arr,
|
|
99
|
+
(v) => mapRange(v, 0, 255, 0, radix).toString(radix).substring(0, 1)
|
|
100
|
+
).join("");
|
|
101
|
+
}
|
|
94
102
|
|
|
95
103
|
// lib/array.ts
|
|
96
104
|
function randomItem(array) {
|
|
@@ -120,14 +128,14 @@ var UserUtils = (function (exports) {
|
|
|
120
128
|
return retArray;
|
|
121
129
|
}
|
|
122
130
|
|
|
123
|
-
// lib/
|
|
131
|
+
// lib/ConfigManager.ts
|
|
124
132
|
var ConfigManager = class {
|
|
125
133
|
/**
|
|
126
134
|
* Creates an instance of ConfigManager to manage a user configuration that is cached in memory and persistently saved across sessions.
|
|
127
135
|
* Supports migrating data from older versions of the configuration to newer ones and populating the cache with default data if no persistent data is found.
|
|
128
136
|
*
|
|
129
137
|
* ⚠️ Requires the directives `@grant GM.getValue` and `@grant GM.setValue`
|
|
130
|
-
* ⚠️ Make sure to call
|
|
138
|
+
* ⚠️ Make sure to call {@linkcode loadData()} at least once after creating an instance, or the returned data will be the same as `options.defaultConfig`
|
|
131
139
|
*
|
|
132
140
|
* @template TData The type of the data that is saved in persistent storage (will be automatically inferred from `config.defaultConfig`) - this should also be the type of the data format associated with the current `options.formatVersion`
|
|
133
141
|
* @param options The options for this ConfigManager instance
|
|
@@ -170,7 +178,10 @@ var UserUtils = (function (exports) {
|
|
|
170
178
|
}
|
|
171
179
|
});
|
|
172
180
|
}
|
|
173
|
-
/**
|
|
181
|
+
/**
|
|
182
|
+
* Returns a copy of the data from the in-memory cache.
|
|
183
|
+
* Use {@linkcode loadData()} to get fresh data from persistent storage (usually not necessary since the cache should always exactly reflect persistent storage).
|
|
184
|
+
*/
|
|
174
185
|
getData() {
|
|
175
186
|
return this.deepCopy(this.cachedConfig);
|
|
176
187
|
}
|
|
@@ -200,8 +211,8 @@ var UserUtils = (function (exports) {
|
|
|
200
211
|
}
|
|
201
212
|
/**
|
|
202
213
|
* Call this method to clear all persistently stored data associated with this ConfigManager instance.
|
|
203
|
-
* The in-memory cache will be left untouched, so you may still access the data with
|
|
204
|
-
* Calling
|
|
214
|
+
* The in-memory cache will be left untouched, so you may still access the data with {@linkcode getData()}
|
|
215
|
+
* Calling {@linkcode loadData()} or {@linkcode setData()} after this method was called will recreate persistent storage with the cached or default data.
|
|
205
216
|
*
|
|
206
217
|
* ⚠️ This requires the additional directive `@grant GM.deleteValue`
|
|
207
218
|
*/
|
|
@@ -294,7 +305,7 @@ var UserUtils = (function (exports) {
|
|
|
294
305
|
openElem.click();
|
|
295
306
|
setTimeout(openElem.remove, 50);
|
|
296
307
|
}
|
|
297
|
-
function interceptEvent(eventObject, eventName, predicate) {
|
|
308
|
+
function interceptEvent(eventObject, eventName, predicate = () => true) {
|
|
298
309
|
if (typeof Error.stackTraceLimit === "number" && Error.stackTraceLimit < 1e3) {
|
|
299
310
|
Error.stackTraceLimit = 1e3;
|
|
300
311
|
}
|
|
@@ -312,56 +323,9 @@ var UserUtils = (function (exports) {
|
|
|
312
323
|
};
|
|
313
324
|
})(eventObject.__proto__.addEventListener);
|
|
314
325
|
}
|
|
315
|
-
function interceptWindowEvent(eventName, predicate) {
|
|
326
|
+
function interceptWindowEvent(eventName, predicate = () => true) {
|
|
316
327
|
return interceptEvent(getUnsafeWindow(), eventName, predicate);
|
|
317
328
|
}
|
|
318
|
-
function amplifyMedia(mediaElement, initialMultiplier = 1) {
|
|
319
|
-
const context = new (window.AudioContext || window.webkitAudioContext)();
|
|
320
|
-
const props = {
|
|
321
|
-
/** Sets the gain multiplier */
|
|
322
|
-
setGain(multiplier) {
|
|
323
|
-
props.gainNode.gain.setValueAtTime(multiplier, props.context.currentTime);
|
|
324
|
-
},
|
|
325
|
-
/** Returns the current gain multiplier */
|
|
326
|
-
getGain() {
|
|
327
|
-
return props.gainNode.gain.value;
|
|
328
|
-
},
|
|
329
|
-
/** Enable the amplification for the first time or if it was disabled before */
|
|
330
|
-
enable() {
|
|
331
|
-
props.source.connect(props.limiterNode);
|
|
332
|
-
props.limiterNode.connect(props.gainNode);
|
|
333
|
-
props.gainNode.connect(props.context.destination);
|
|
334
|
-
},
|
|
335
|
-
/** Disable the amplification */
|
|
336
|
-
disable() {
|
|
337
|
-
props.source.disconnect(props.limiterNode);
|
|
338
|
-
props.limiterNode.disconnect(props.gainNode);
|
|
339
|
-
props.gainNode.disconnect(props.context.destination);
|
|
340
|
-
props.source.connect(props.context.destination);
|
|
341
|
-
},
|
|
342
|
-
/**
|
|
343
|
-
* Set the options of the [limiter / DynamicsCompressorNode](https://developer.mozilla.org/en-US/docs/Web/API/DynamicsCompressorNode/DynamicsCompressorNode#options)
|
|
344
|
-
* The default is `{ threshold: -12, knee: 30, ratio: 12, attack: 0.003, release: 0.25 }`
|
|
345
|
-
*/
|
|
346
|
-
setLimiterOptions(options) {
|
|
347
|
-
for (const [key, val] of Object.entries(options))
|
|
348
|
-
props.limiterNode[key].setValueAtTime(val, props.context.currentTime);
|
|
349
|
-
},
|
|
350
|
-
context,
|
|
351
|
-
source: context.createMediaElementSource(mediaElement),
|
|
352
|
-
gainNode: context.createGain(),
|
|
353
|
-
limiterNode: context.createDynamicsCompressor()
|
|
354
|
-
};
|
|
355
|
-
props.setLimiterOptions({
|
|
356
|
-
threshold: -12,
|
|
357
|
-
knee: 30,
|
|
358
|
-
ratio: 12,
|
|
359
|
-
attack: 3e-3,
|
|
360
|
-
release: 0.25
|
|
361
|
-
});
|
|
362
|
-
props.setGain(initialMultiplier);
|
|
363
|
-
return props;
|
|
364
|
-
}
|
|
365
329
|
function isScrollable(element) {
|
|
366
330
|
const { overflowX, overflowY } = getComputedStyle(element);
|
|
367
331
|
return {
|
|
@@ -400,63 +364,184 @@ var UserUtils = (function (exports) {
|
|
|
400
364
|
return res;
|
|
401
365
|
});
|
|
402
366
|
}
|
|
403
|
-
function insertValues(
|
|
404
|
-
return
|
|
367
|
+
function insertValues(input, ...values) {
|
|
368
|
+
return input.replace(/%\d/gm, (match) => {
|
|
405
369
|
var _a, _b;
|
|
406
370
|
const argIndex = Number(match.substring(1)) - 1;
|
|
407
371
|
return (_b = (_a = values[argIndex]) != null ? _a : match) == null ? void 0 : _b.toString();
|
|
408
372
|
});
|
|
409
373
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
374
|
+
function compress(input, compressionFormat, outputType = "base64") {
|
|
375
|
+
return __async(this, null, function* () {
|
|
376
|
+
const byteArray = typeof input === "string" ? new TextEncoder().encode(input) : input;
|
|
377
|
+
const comp = new CompressionStream(compressionFormat);
|
|
378
|
+
const writer = comp.writable.getWriter();
|
|
379
|
+
writer.write(byteArray);
|
|
380
|
+
writer.close();
|
|
381
|
+
const buf = yield new Response(comp.readable).arrayBuffer();
|
|
382
|
+
return outputType === "arrayBuffer" ? buf : ab2str(buf);
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
function decompress(input, compressionFormat, outputType = "string") {
|
|
386
|
+
return __async(this, null, function* () {
|
|
387
|
+
const byteArray = typeof input === "string" ? str2ab(input) : input;
|
|
388
|
+
const decomp = new DecompressionStream(compressionFormat);
|
|
389
|
+
const writer = decomp.writable.getWriter();
|
|
390
|
+
writer.write(byteArray);
|
|
391
|
+
writer.close();
|
|
392
|
+
const buf = yield new Response(decomp.readable).arrayBuffer();
|
|
393
|
+
return outputType === "arrayBuffer" ? buf : new TextDecoder().decode(buf);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
function ab2str(buf) {
|
|
397
|
+
return getUnsafeWindow().btoa(
|
|
398
|
+
new Uint8Array(buf).reduce((data, byte) => data + String.fromCharCode(byte), "")
|
|
399
|
+
);
|
|
420
400
|
}
|
|
421
|
-
function
|
|
422
|
-
return
|
|
401
|
+
function str2ab(str) {
|
|
402
|
+
return Uint8Array.from(getUnsafeWindow().atob(str), (c) => c.charCodeAt(0));
|
|
423
403
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
404
|
+
|
|
405
|
+
// lib/SelectorObserver.ts
|
|
406
|
+
var SelectorObserver = class {
|
|
407
|
+
constructor(baseElement, options = {}) {
|
|
408
|
+
__publicField(this, "enabled", false);
|
|
409
|
+
__publicField(this, "baseElement");
|
|
410
|
+
__publicField(this, "observer");
|
|
411
|
+
__publicField(this, "observerOptions");
|
|
412
|
+
__publicField(this, "listenerMap");
|
|
413
|
+
this.baseElement = baseElement;
|
|
414
|
+
this.listenerMap = /* @__PURE__ */ new Map();
|
|
415
|
+
this.observer = new MutationObserver(() => this.checkAllSelectors());
|
|
416
|
+
this.observerOptions = __spreadValues({
|
|
417
|
+
childList: true,
|
|
418
|
+
subtree: true
|
|
419
|
+
}, options);
|
|
420
|
+
}
|
|
421
|
+
checkAllSelectors() {
|
|
422
|
+
for (const [selector, listeners] of this.listenerMap.entries())
|
|
423
|
+
this.checkSelector(selector, listeners);
|
|
424
|
+
}
|
|
425
|
+
checkSelector(selector, listeners) {
|
|
426
|
+
var _a;
|
|
427
|
+
if (!this.enabled)
|
|
428
|
+
return;
|
|
429
|
+
const baseElement = typeof this.baseElement === "string" ? document.querySelector(this.baseElement) : this.baseElement;
|
|
430
|
+
if (!baseElement)
|
|
431
|
+
return;
|
|
432
|
+
const all = listeners.some((listener) => listener.all);
|
|
433
|
+
const one = listeners.some((listener) => !listener.all);
|
|
434
|
+
const allElements = all ? baseElement.querySelectorAll(selector) : null;
|
|
435
|
+
const oneElement = one ? baseElement.querySelector(selector) : null;
|
|
436
|
+
for (const options of listeners) {
|
|
437
|
+
if (options.all) {
|
|
438
|
+
if (allElements && allElements.length > 0) {
|
|
439
|
+
options.listener(allElements);
|
|
440
|
+
if (!options.continuous)
|
|
441
|
+
this.removeListener(selector, options);
|
|
442
|
+
}
|
|
443
|
+
} else {
|
|
444
|
+
if (oneElement) {
|
|
445
|
+
options.listener(oneElement);
|
|
446
|
+
if (!options.continuous)
|
|
447
|
+
this.removeListener(selector, options);
|
|
448
|
+
}
|
|
433
449
|
}
|
|
434
|
-
|
|
435
|
-
|
|
450
|
+
if (((_a = this.listenerMap.get(selector)) == null ? void 0 : _a.length) === 0)
|
|
451
|
+
this.listenerMap.delete(selector);
|
|
436
452
|
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
453
|
+
}
|
|
454
|
+
debounce(func, time) {
|
|
455
|
+
let timeout;
|
|
456
|
+
return function(...args) {
|
|
457
|
+
clearTimeout(timeout);
|
|
458
|
+
timeout = setTimeout(() => func.apply(this, args), time);
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Starts observing the children of the base element for changes to the given {@linkcode selector} according to the set {@linkcode options}
|
|
463
|
+
* @param selector The selector to observe
|
|
464
|
+
* @param options Options for the selector observation
|
|
465
|
+
* @param options.listener Gets called whenever the selector was found in the DOM
|
|
466
|
+
* @param [options.all] Whether to use `querySelectorAll()` instead - default is false
|
|
467
|
+
* @param [options.continuous] Whether to call the listener continuously instead of just once - default is false
|
|
468
|
+
* @param [options.debounce] Whether to debounce the listener to reduce calls to `querySelector` or `querySelectorAll` - set undefined or <=0 to disable (default)
|
|
469
|
+
*/
|
|
470
|
+
addListener(selector, options) {
|
|
471
|
+
options = __spreadValues({ all: false, continuous: false, debounce: 0 }, options);
|
|
472
|
+
if (options.debounce && options.debounce > 0 || this.observerOptions.defaultDebounce && this.observerOptions.defaultDebounce > 0) {
|
|
473
|
+
options.listener = this.debounce(
|
|
474
|
+
options.listener,
|
|
475
|
+
options.debounce || this.observerOptions.defaultDebounce
|
|
476
|
+
);
|
|
444
477
|
}
|
|
478
|
+
if (this.listenerMap.has(selector))
|
|
479
|
+
this.listenerMap.get(selector).push(options);
|
|
480
|
+
else
|
|
481
|
+
this.listenerMap.set(selector, [options]);
|
|
482
|
+
this.checkSelector(selector, [options]);
|
|
445
483
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
484
|
+
/** Disables the observation of the child elements */
|
|
485
|
+
disable() {
|
|
486
|
+
if (!this.enabled)
|
|
487
|
+
return;
|
|
488
|
+
this.enabled = false;
|
|
489
|
+
this.observer.disconnect();
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Enables or reenables the observation of the child elements.
|
|
493
|
+
* @param immediatelyCheckSelectors Whether to immediately check if all previously registered selectors exist (default is true)
|
|
494
|
+
* @returns Returns true when the observation was enabled, false otherwise (e.g. when the base element wasn't found)
|
|
495
|
+
*/
|
|
496
|
+
enable(immediatelyCheckSelectors = true) {
|
|
497
|
+
const baseElement = typeof this.baseElement === "string" ? document.querySelector(this.baseElement) : this.baseElement;
|
|
498
|
+
if (this.enabled || !baseElement)
|
|
499
|
+
return false;
|
|
500
|
+
this.enabled = true;
|
|
501
|
+
this.observer.observe(baseElement, this.observerOptions);
|
|
502
|
+
if (immediatelyCheckSelectors)
|
|
503
|
+
this.checkAllSelectors();
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
/** Returns whether the observation of the child elements is currently enabled */
|
|
507
|
+
isEnabled() {
|
|
508
|
+
return this.enabled;
|
|
509
|
+
}
|
|
510
|
+
/** Removes all listeners that have been registered with {@linkcode addListener()} */
|
|
511
|
+
clearListeners() {
|
|
512
|
+
this.listenerMap.clear();
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Removes all listeners for the given {@linkcode selector} that have been registered with {@linkcode addListener()}
|
|
516
|
+
* @returns Returns true when all listeners for the associated selector were found and removed, false otherwise
|
|
517
|
+
*/
|
|
518
|
+
removeAllListeners(selector) {
|
|
519
|
+
return this.listenerMap.delete(selector);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Removes a single listener for the given {@linkcode selector} and {@linkcode options} that has been registered with {@linkcode addListener()}
|
|
523
|
+
* @returns Returns true when the listener was found and removed, false otherwise
|
|
524
|
+
*/
|
|
525
|
+
removeListener(selector, options) {
|
|
526
|
+
const listeners = this.listenerMap.get(selector);
|
|
527
|
+
if (!listeners)
|
|
528
|
+
return false;
|
|
529
|
+
const index = listeners.indexOf(options);
|
|
530
|
+
if (index > -1) {
|
|
531
|
+
listeners.splice(index, 1);
|
|
532
|
+
return true;
|
|
533
|
+
}
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
/** Returns all listeners that have been registered with {@linkcode addListener()} */
|
|
537
|
+
getAllListeners() {
|
|
538
|
+
return this.listenerMap;
|
|
539
|
+
}
|
|
540
|
+
/** Returns all listeners for the given {@linkcode selector} that have been registered with {@linkcode addListener()} */
|
|
541
|
+
getListeners(selector) {
|
|
542
|
+
return this.listenerMap.get(selector);
|
|
543
|
+
}
|
|
544
|
+
};
|
|
460
545
|
|
|
461
546
|
// lib/translation.ts
|
|
462
547
|
var trans = {};
|
|
@@ -484,31 +569,30 @@ var UserUtils = (function (exports) {
|
|
|
484
569
|
};
|
|
485
570
|
|
|
486
571
|
exports.ConfigManager = ConfigManager;
|
|
572
|
+
exports.SelectorObserver = SelectorObserver;
|
|
487
573
|
exports.addGlobalStyle = addGlobalStyle;
|
|
488
574
|
exports.addParent = addParent;
|
|
489
|
-
exports.amplifyMedia = amplifyMedia;
|
|
490
575
|
exports.autoPlural = autoPlural;
|
|
491
576
|
exports.clamp = clamp;
|
|
577
|
+
exports.compress = compress;
|
|
492
578
|
exports.debounce = debounce;
|
|
579
|
+
exports.decompress = decompress;
|
|
493
580
|
exports.fetchAdvanced = fetchAdvanced;
|
|
494
|
-
exports.getSelectorMap = getSelectorMap;
|
|
495
581
|
exports.getUnsafeWindow = getUnsafeWindow;
|
|
496
|
-
exports.initOnSelector = initOnSelector;
|
|
497
582
|
exports.insertAfter = insertAfter;
|
|
498
583
|
exports.insertValues = insertValues;
|
|
499
584
|
exports.interceptEvent = interceptEvent;
|
|
500
585
|
exports.interceptWindowEvent = interceptWindowEvent;
|
|
501
586
|
exports.isScrollable = isScrollable;
|
|
502
587
|
exports.mapRange = mapRange;
|
|
503
|
-
exports.onSelector = onSelector;
|
|
504
588
|
exports.openInNewTab = openInNewTab;
|
|
505
589
|
exports.pauseFor = pauseFor;
|
|
506
590
|
exports.preloadImages = preloadImages;
|
|
507
591
|
exports.randRange = randRange;
|
|
592
|
+
exports.randomId = randomId;
|
|
508
593
|
exports.randomItem = randomItem;
|
|
509
594
|
exports.randomItemIndex = randomItemIndex;
|
|
510
595
|
exports.randomizeArray = randomizeArray;
|
|
511
|
-
exports.removeOnSelector = removeOnSelector;
|
|
512
596
|
exports.takeRandomItem = takeRandomItem;
|
|
513
597
|
exports.tr = tr;
|
|
514
598
|
|