make-mutation-observer

Simple wrapper around `MutationObserver` API to watch DOM changes.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/488160/1335044/make-mutation-observer.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         make-mutation-observer
// @description  Simple wrapper around `MutationObserver` API to watch DOM changes.
// @version      0.1.0
// @namespace    owowed.moe
// @author       owowed <[email protected]>
// @license      LGPL-3.0
// ==/UserScript==

/**
 * @typedef {(info: { records: MutationRecord[], observer: MutationObserver }) => void} MakeMutationObserverCallback
 */

/**
 * @typedef MakeMutationObserverOptionsBasic
 * @prop {HTMLElement} target
 * @prop {MakeMutationObserverCallback} callback
 * @prop {AbortSignal} [abortSignal]
 * @prop {boolean} [once]
 */

/** @typedef {MakeMutationObserverOptionsBasic & MutationObserverInit} MakeMutationObserverOptions */

/**
 * Create a new `MutationObserver` from target and callback.
 * @param {Node} target target node
 * @param {MakeMutationObserverCallback} callback observer callback
 * @param {MakeMutationObserverOptions} options additional observer options
 * @returns {MutationObserver}
 */
function makeMutationObserver(target, callback, options) {
    return makeMutationObserverOptions({ target, ...options }, callback);
}

/**
 * Create a new `MutationObserver` with options and callback.
 * @param {MakeMutationObserverOptions} options 
 * @param {MakeMutationObserverCallback} callback 
 * @returns {MutationObserver}
 */
function makeMutationObserverOptions({ target, abortSignal, once, ...options }, callback) {
   const observer = new MutationObserver(records => {
       abortSignal?.throwIfAborted();
       if (once) observer.disconnect();
       callback({ records, observer });
   });

   observer.observe(target, options);

   abortSignal?.addEventListener("abort", () => {
       observer.disconnect();
   });

   return observer;
}