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

Sv443 bc88de54cd docs: add SelectorObserver to readme summary 1 år sedan
.changeset 38002d78c4 chore: create new release 1 år sedan
.github cf9084bc2b ci: disable minification 1 år sedan
lib 17e0b41862 feat: better SelectorObserver chaining 1 år sedan
test 7c4cfa8581 ref: move test page 1 år sedan
tools 431b2181c6 fix: post-build regex 1 år sedan
.eslintrc.cjs e54ccbcf24 feat: initial commit 1 år sedan
.gitignore 72181703dd docs: readme stuff 1 år sedan
.gitmodules 56dcdadcbe feat: add test script as submodule 1 år sedan
CHANGELOG.md 38002d78c4 chore: create new release 1 år sedan
LICENSE.txt e54ccbcf24 feat: initial commit 1 år sedan
README-summary.md bc88de54cd docs: add SelectorObserver to readme summary 1 år sedan
README.md 17e0b41862 feat: better SelectorObserver chaining 1 år sedan
package-lock.json 0db73b55ed fix!: much better audio amplification 1 år sedan
package.json 38002d78c4 chore: create new release 1 år sedan
tsconfig.json 969d157585 fix: delete selectors entry if array empty 1 år sedan

README-summary.md

UserUtils

Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, manage persistent user configurations, modify the DOM more easily and more.
Contains builtin TypeScript declarations. Webpack compatible and supports ESM and CJS.
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 ❤️


Full documentation on GitHub


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
    • 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%
    • isScrollable() - check if an element has a horizontal or vertical scroll bar
  • 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
    • randomId() - generate a random ID of a given length and radix
  • Misc:
    • ConfigManager() - class that manages persistent userscript configurations, including data migration
    • 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
    • insertValues() - insert values into a string at specified placeholders
  • Arrays:
  • Translation:
  • 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



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 just import everything (not recommended because this doesn't allow for treeshaking):
    
    import * as UserUtils from "@sv443-network/userutils";
    

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


  • If you are not using a bundler, 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, 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