NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
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.
Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via @require
Licensed under the MIT license.
You may want to check out my template for userscripts in TypeScript that you can use to get started quickly. It also includes this library by default.
If you like using this library, please consider supporting the development ❤️
or view the documentation of previous major releases:
7.3.0, 6.3.0, 5.0.1, 4.2.1, 3.0.0, 2.0.1, 1.2.0, 0.5.3
SelectorObserver
- class that manages listeners that are called when selectors are found in the DOMgetUnsafeWindow()
- get the unsafeWindow object or fall back to the regular window objectaddParent()
- add a parent element around another elementaddGlobalStyle()
- add a global style to the pagepreloadImages()
- preload images into the browser cache for faster loading later onopenInNewTab()
- open a link in a new tabinterceptEvent()
- conditionally intercepts events registered by addEventListener()
on any given EventTarget objectinterceptWindowEvent()
- conditionally intercepts events registered by addEventListener()
on the window objectisScrollable()
- check if an element has a horizontal or vertical scroll barobserveElementProp()
- observe changes to an element's property that can't be observed with MutationObservergetSiblingsFrame()
- returns a frame of an element's siblings, with a given alignment and sizesetInnerHtmlUnsafe()
- set the innerHTML of an element using a Trusted Types policy without sanitizing or escaping itclamp()
- constrain a number between a min and max valuemapRange()
- map a number from one range to the same spot in another rangerandRange()
- generate a random number between a min and max boundaryDataStore
- class that manages a hybrid sync & async persistent JSON database, including data migrationDataStoreSerializer
- class for importing & exporting data of multiple DataStore instances, including compression, checksumming and running migrationsDialog
- class for creating custom modal dialogs with a promise-based API and a generic, default styleNanoEmitter
- tiny event emitter class with a focus on performance and simplicity (based on nanoevents)autoPlural()
- automatically pluralize a stringpauseFor()
- pause the execution of a function for a given amount of timedebounce()
- call a function only once in a series of calls, after or before a given timeoutfetchAdvanced()
- wrapper around the fetch API with a timeout optioninsertValues()
- insert values into a string at specified placeholderscompress()
- compress a string with Gzip or Deflatedecompress()
- decompress a previously compressed stringcomputeHash()
- compute the hash / checksum of a string or ArrayBufferrandomId()
- generate a random ID of a given length and radixrandomItem()
- returns a random item from an arrayrandomItemIndex()
- returns a tuple of a random item and its index from an arraytakeRandomItem()
- returns a random item from an array and mutates it to remove the itemrandomizeArray()
- returns a copy of the array with its items in a random ordertr()
- simple translation of a string to another languagetr.forLang()
- specify a language besides the currently set one for the translationtr.addLanguage()
- add a language and its translationstr.setLanguage()
- set the currently active language for translationstr.getLanguage()
- returns the currently active languagetr.getTranslations()
- returns the translations for the given language or the currently active onehexToRgb()
- convert a hex color string to an RGB or RGBA value tuplergbToHex()
- convert RGB or RGBA values to a hex color stringlightenColor()
- lighten a CSS color string (hex, rgb or rgba) by a given percentagedarkenColor()
- darken a CSS color string (hex, rgb or rgba) by a given percentageStringifiable
- any value that is a string or can be converted to one (implicitly or explicitly)NonEmptyArray
- any array that should have at least one itemNonEmptyString
- any string that should have at least one characterLooseUnion
- a union that gives autocomplete in the IDE but also allows any other value of the same typePrettify
- expands a complex type into a more readable format while keeping functionality the sameShameless plug: I made a template for userscripts in TypeScript that you can use to get started quickly. It also includes this library by default.
npm i @sv443-network/userutils
For other package managers, check out the respective install command on the JavaScript Registryimport { addGlobalStyle } from "@sv443-network/userutils";
// or just import everything (not recommended because of worse treeshaking support):
import * as UserUtils from "@sv443-network/userutils";
If you are not using a bundler or want to reduce the size of your userscript, you can include the latest release by adding one of these directives to the userscript header, depending on your preferred CDN:
// @require https://greasyfork.org/scripts/472956-userutils/code/UserUtils.js
// @require https://openuserjs.org/src/libs/Sv443/UserUtils.js
(in order for your userscript not to break on a major library update, instead use the versioned URL at the top of the GreasyFork page)
Then, access the functions on the global variable UserUtils
:
UserUtils.addGlobalStyle("body { background-color: red; }");
// or using object destructuring:
const { clamp } = UserUtils;
console.log(clamp(1, 5, 10)); // 5
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 your project:
declare global {
const UserUtils: typeof import("@sv443-network/userutils");
}
Rating: 0