npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@sv443-network/userutils

v9.4.4

Published

General purpose DOM/GreaseMonkey library that allows you to register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and much more

Readme

UserUtils

General purpose DOM/GreaseMonkey library that allows you to register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and much more.
Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via @require or <script>
The library works in any DOM environment with or without the GreaseMonkey API, but some features will be unavailable or limited.

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 ❤️

Tree shaking support badge Code coverage percentage badge Minified bundle size badge Minified and gzipped bundle size badge

Discord server badge Github stargazers badge Support development on Github Sponsors badge

View the documentation of previous releases:

v8 (8.4.0)v7 (7.3.0)v6 (6.3.0)v5 (5.0.1)v4 (4.2.1)v3v2 (2.0.1)v1 (1.2.0)v0.5.3

Table of Contents:

  • Installation
  • License
  • Preamble (info about the documentation)
  • Features
    • DOM:
      • SelectorObserver - class that manages listeners that are called when selectors are found in the DOM
      • getUnsafeWindow() - get the unsafeWindow object or fall back to the regular window object
      • isDomLoaded() - check if the DOM has finished loading and can be queried and modified
      • onDomLoad() - run a function or pause async execution until the DOM has finished loading (or immediately if DOM is already loaded)
      • addParent() - add a parent element around another element
      • addGlobalStyle() - add a global style to the page
      • preloadImages() - preload images into the browser cache for faster loading later on
      • openInNewTab() - open a link in a new tab
      • interceptEvent() - conditionally intercepts events registered by addEventListener() on any given EventTarget object
      • interceptWindowEvent() - conditionally intercepts events registered by addEventListener() on the window object
      • isScrollable() - check if an element has a horizontal or vertical scroll bar
      • observeElementProp() - observe changes to an element's property that can't be observed with MutationObserver
      • getSiblingsFrame() - returns a frame of an element's siblings, with a given alignment and size
      • setInnerHtmlUnsafe() - set the innerHTML of an element using a Trusted Types policy without sanitizing or escaping it
      • probeElementStyle() - probe the computed style of a temporary element (get default font size, resolve CSS variables, etc.)
    • Math:
      • clamp() - constrain a number between a min and max value
      • mapRange() - map a number from one range to the same spot in another range
      • randRange() - generate a random number between a min and max boundary
      • digitCount() - calculate the amount of digits in a number
      • roundFixed() - round a floating-point number at the given amount of decimals, or to the given power of 10
      • bitSetHas() - check if a bit is set in a bitset
    • Misc:
      • DataStore - class that manages a hybrid sync & async persistent JSON database, including data migration
      • DataStoreSerializer - class for importing & exporting data of multiple DataStore instances, including compression, checksumming and running migrations
      • Dialog - class for creating custom modal dialogs with a promise-based API and a generic, default style
      • Mixins - class for creating mixin functions that allow multiple sources to modify a target value in a highly flexible way
      • NanoEmitter - tiny event emitter class with a focus on performance and simplicity (based on nanoevents)
      • Debouncer - class for debouncing function calls with a given timeout
      • debounce() - function wrapper for the Debouncer class for easier usage
      • autoPlural() - automatically pluralize a string
      • pauseFor() - pause the execution of a function for a given amount of time
      • fetchAdvanced() - wrapper around the fetch API with a timeout option
      • insertValues() - insert values into a string at specified placeholders
      • compress() - compress a string with Gzip or Deflate
      • decompress() - decompress a previously compressed string
      • computeHash() - compute the hash / checksum of a string or ArrayBuffer
      • randomId() - generate a random ID of a given length and radix
      • consumeGen() - consumes a ValueGen and returns the value
      • consumeStringGen() - consumes a StringGen and returns the string
      • getListLength() - get the length of any object with a numeric length, count or size property
      • purifyObj() - removes the prototype chain (all default properties like toString, __proto__, etc.) from an object
    • Arrays:
    • Translation:
    • Colors:
      • hexToRgb() - convert a hex color string to an RGB or RGBA value tuple
      • rgbToHex() - convert RGB or RGBA values to a hex color string
      • lightenColor() - lighten a CSS color string (hex, rgb or rgba) by a given percentage
      • darkenColor() - darken a CSS color string (hex, rgb or rgba) by a given percentage
    • Utility types for TypeScript:
      • Stringifiable - any value that is a string or can be converted to one (implicitly or explicitly)
      • NonEmptyArray - any array that should have at least one item
      • NonEmptyString - any string that should have at least one character
      • LooseUnion - a union that gives autocomplete in the IDE but also allows any other value of the same type
      • Prettify - expands a complex type into a more readable format while keeping functionality the same
      • ValueGen - a "generator" value that allows for super flexible value typing and declaration
      • StringGen - a "generator" string that allows for super flexible string typing and declaration, including enhanced support for unions
      • ListWithLength - represents an array or object with a numeric length, count or size property
    • Custom Error classes
      • UUError - base class for all custom UserUtils errors - has a custom date prop set to the time of creation
      • ChecksumMismatchError - thrown when a string of data doesn't match its checksum
      • MigrationError - thrown when a data migration fails
      • PlatformError - thrown when a function is called in an unsupported environment

Installation:

Shameless plug: I made a template for userscripts in TypeScript that you can use to get started quickly. It also includes this library by default.

  • If you are using a bundler (like webpack, rollup, vite, etc.), you can install this package in one of the following ways:

    npm i @sv443-network/userutils
    pnpm i @sv443-network/userutils
    yarn add @sv443-network/userutils
    npx jsr install @sv443-network/userutils
    deno add jsr:@sv443-network/userutils

    Then import it in your script as usual:

    // on Node:
    import { addGlobalStyle } from "@sv443-network/userutils";
    
    // on Deno:
    import { addGlobalStyle } from "jsr:@sv443-network/userutils";
    
    // you can also import the entire library as an object (not recommended because of worse treeshaking support):
    import * as UserUtils from "@sv443-network/userutils";
  • If you are not using a bundler, want to reduce the size of your script, 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:

    Versioned (recommended):

    // @require https://cdn.jsdelivr.net/npm/@sv443-network/userutils@INSERT_VERSION/dist/index.global.js
    // @require https://unpkg.com/@sv443-network/userutils@INSERT_VERSION/dist/index.global.js

    Non-versioned (not recommended because it auto-updates):

    // @require https://update.greasyfork.org/scripts/472956/UserUtils.js
    // @require https://openuserjs.org/src/libs/Sv443/UserUtils.js
  • If you are using this library in a generic DOM environment without access to the GreaseMonkey API, either download the latest release from the releases page to include in your project or add one of the following tags to the <head>:

    <script src="https://cdn.jsdelivr.net/npm/@sv443-network/userutils@INSERT_VERSION/dist/index.global.js"></script>
    <script src="https://unpkg.com/@sv443-network/userutils@INSERT_VERSION/dist/index.global.js"></script>

[!NOTE]
In order for your script 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.

  • 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 any .ts file that is included in the final build:

    declare const UserUtils: typeof import("@sv443-network/userutils");
    
    declare global {
      interface Window {
        UserUtils: typeof UserUtils;
      }
    }
  • 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:
    "globals": {
        "UserUtils": "readonly"
    }

License:

This library is licensed under the MIT License.
See the license file for details.

Made with ❤️ by Sv443
If you like this library, please consider supporting the development