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

Sv443 0e4fbb67b0 ref: build type decls with tsc instead of tsup 1 년 전
.changeset 65e4d0b60f chore: create new release 1 년 전
.github 2ed32fac8c chore: update npmignore & contributing 1 년 전
lib 2b6e2dbb85 ref: remove types.ts 1 년 전
.eslintrc.cjs e54ccbcf24 feat: initial commit 1 년 전
.gitignore 72181703dd docs: readme stuff 1 년 전
CHANGELOG.md 65e4d0b60f chore: create new release 1 년 전
LICENSE.txt e54ccbcf24 feat: initial commit 1 년 전
README-greasyfork.md 9d3de8f4ce ref: replace npmignore with files in pkg.json 1 년 전
README.md 0e4fbb67b0 ref: build type decls with tsc instead of tsup 1 년 전
package-lock.json 9c9426a111 ref: improve build commands 1 년 전
package.json 0e4fbb67b0 ref: build type decls with tsc instead of tsup 1 년 전
tsconfig.json 0e4fbb67b0 ref: build type decls with tsc instead of tsup 1 년 전

README-greasyfork.md

UserUtils

Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, modify the DOM more easily and more.
Contains builtin TypeScript declarations. Webpack compatible and supports ESM and CJS.
Licensed under the MIT license.

If you like using this library, please consider supporting the development ❤️


Full documentation on GitHub


Features:

  • DOM:
    • onSelector() - call a listener once a selector is found in the DOM
    • initOnSelector() - needs to be called once to be able to use onSelector()
    • getSelectorMap() - returns all currently registered selectors, listeners and options
    • getUnsafeWindow() - get the unsafeWindow object or fall back to the regular window object
    • insertAfter() - insert an element as a sibling after another element
    • 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
    • amplifyMedia() - amplify an audio or video element's volume past the maximum of 100%
  • 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
  • Misc:
    • autoPlural() - automatically pluralize a string
    • pauseFor() - pause the execution of a function for a given amount of time
    • debounce() - call a function only once, after a given amount of time
    • fetchAdvanced() - wrapper around the fetch API with a timeout option
  • Arrays:



Installation:

  • If you are using a bundler like webpack, you can install this package using npm:

    npm i @sv443-network/userutils
    

    Then, import it in your script as usual:

    import { addGlobalStyle } from "@sv443-network/userutils";
    // or
    import * as userUtils from "@sv443-network/userutils";
    

    Shameless plug: I also have a webpack-based template for userscripts in TypeScript that you can use to get started quickly.


  • If you are not using a bundler, you can include the latest release from GreasyFork by adding this directive to the userscript header:

    // @require https://greasyfork.org/scripts/472956-userutils/code/UserUtils.js
    

    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