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

Sven Fehler 6630aeb556 Merge pull request #45 from Sv443-Network/changeset-release/main 8 meses atrás
.changeset 54c03b2600 chore: create new release 8 meses atrás
.github ec95b7069a feat: unfinished debounce v2 sketch 8 meses atrás
.vscode 0100221bc4 feat: add vsc settings 1 ano atrás
lib 4cb3ce724e fix: fetchAdvanced timeout abort on error 8 meses atrás
test 602f619457 docs: test readme 1 ano atrás
tools 5e4cd95e0f ref: post-build 1 ano atrás
.eslintrc.cjs 49bc85e3c0 feat: utility types NonEmptyString and LooseUnion 1 ano atrás
.gitignore 72181703dd docs: readme stuff 1 ano atrás
.gitmodules 56dcdadcbe feat: add test script as submodule 1 ano atrás
CHANGELOG.md 54c03b2600 chore: create new release 8 meses atrás
LICENSE.txt e54ccbcf24 feat: initial commit 1 ano atrás
README-summary.md d7cdac004f fix(#48): Math.random() impl for randomId() 9 meses atrás
README.md 287b006a79 feat: DataStore storageMethod prop (including localStorage & sessionStorage) 9 meses atrás
jsr.jsonc cd648aeaa5 feat: add jsr config 10 meses atrás
package-lock.json 6ad28b6fcb feat: add package.json exports prop 10 meses atrás
package.json 54c03b2600 chore: create new release 8 meses atrás
tsconfig.json 6c765fef9b ref: switch to esnext import syntax 9 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. Fully web compatible and supports ESM and CJS imports and global declaration.
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 <


or view the documentation of previous major releases:
5.0.1, 4.2.1, 3.0.0, 2.0.1, 1.2.0, 0.5.3


Feature Summary:

  • 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
    • 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
  • 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:
    • 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
    • autoPlural() - automatically pluralize a string
    • pauseFor() - pause the execution of a function for a given amount of time
    • debounce() - call a function only once in a series of calls, after or before a given timeout
    • 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:
  • 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



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 of worse treeshaking support):
    
    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