Skip to content

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

License

Notifications You must be signed in to change notification settings

Sv443-Network/UserUtils

Repository files navigation

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 require special setup.

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


Code coverage percentage badge Bundle size badge

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


View the documentation of previous major versions:

v9.4.4 β€’ v8.4.0 β€’ v7.3.0 β€’ v6.3.0 β€’ v5.0.1 β€’ v4.2.1 β€’ v3.0.0 β€’ v2.0.1 β€’ v1.2.0 β€’ v0.5.3


Note

In version 10.0.0, many of the platform-agnostic features were moved to the CoreUtils library.
Everything is re-exported by UserUtils for backwards compatibility, but you may want to consider using CoreUtils directly if you don't need any of the DOM- or GreaseMonkey-specific features or want control over the installed version of CoreUtils.


Table of Contents:

Note

🟣 = function
🟧 = class
πŸ”· = type
🟩 = const



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/UserUtils.user.js
    // @require https://unpkg.com/@sv443-network/userutils@INSERT_VERSION/dist/UserUtils.user.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/UserUtils.umd.js"></script>
    <script src="https://unpkg.com/@sv443-network/userutils@INSERT_VERSION/dist/UserUtils.umd.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. 10.0.4) 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

About

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

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

  •  

Contributors 2

  •  
  •