Mirror of UserUtils' source code https://github.com/Sv443-Network/UserUtils

Sv443 560ec660d9 docs: fix StringGen and ValueGen documentation há 3 meses atrás
.changeset d0737dcf0d feat: changesets há 3 meses atrás
.github 8bcf2ffc22 feat: new debounce type sketch há 3 meses atrás
.vscode 4a8771734a feat: launch cfg stuff há 3 meses atrás
lib bc0d016be9 fix: randRange with enhancedEntropy only returning first digit há 3 meses atrás
test d37d9dbca6 fix: replace assert import attribute with `with` keyword há 6 meses atrás
tools d6059d4eaf ref: logging in cli tools há 3 meses atrás
.eslintrc.cjs 3b8aa884c1 feat: `hexToRgb()`, `rgbToHex()`, `lightenColor()`, `darkenColor()` há 8 meses atrás
.gitignore 72181703dd docs: readme stuff há 1 ano atrás
.gitmodules 56dcdadcbe feat: add test script as submodule há 1 ano atrás
CHANGELOG.md 6cad00f8a0 ref: separate docs file (#71) & edit links há 3 meses atrás
LICENSE.txt e54ccbcf24 feat: initial commit há 1 ano atrás
README-summary.md 6cad00f8a0 ref: separate docs file (#71) & edit links há 3 meses atrás
README.md 6cad00f8a0 ref: separate docs file (#71) & edit links há 3 meses atrás
docs.md 560ec660d9 docs: fix StringGen and ValueGen documentation há 3 meses atrás
jsr.json 6cad00f8a0 ref: separate docs file (#71) & edit links há 3 meses atrás
package.json 6cad00f8a0 ref: separate docs file (#71) & edit links há 3 meses atrás
pnpm-lock.yaml b7083bdb68 ref: replace ts-node with node --import tsx há 3 meses atrás
tsconfig.json a37057d41b fix: JSR errors há 8 meses atrás

README-summary.md

UserUtils

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


minified bundle size badge minified and gzipped bundle size badge tree shaking support badge

github stargazers badge discord server badge


> Full documentation on GitHub <


View the documentation of previous major releases:
8.0.0, 7.0.0, 6.0.0, 5.0.0, 4.0.0, 3.0.0, 2.0.0, 1.0.0, 0.5.3


Feature Summary:

  • DOM:
  • 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
  • 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
    • 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
  • Arrays:
  • Translation:
    • tr() - simple JSON-based translation system with placeholder and nesting support
    • tr.forLang() - translate with the specified language instead of the currently active one
    • tr.setLanguage() - set the currently active language for translations
    • tr.getLanguage() - returns the currently active language
    • tr.addTranslations() - add a language and its translations
    • tr.getTranslations() - returns the translations for the given language or the currently active one
    • tr.deleteTranslations() - delete the translations for the given language or the active one
    • tr.hasKey() - check if a translation key exists for the given or active language
    • tr.addTransform() - add a transformation function to dynamically modify the translation value
    • tr.deleteTransform() - delete a transformation function that was previously added
  • 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



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 userscript, 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 auto-updating):

    // @require https://update.greasyfork.org/scripts/472956/UserUtils.js
    // @require https://openuserjs.org/src/libs/Sv443/UserUtils.js
    

    Note:
    In order for your userscript 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 a .d.ts file somewhere in the directory (or a subdirectory) defined in your tsconfig.json's baseUrl option or include array:

    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