Node Mutation Hook Library

A wrapper around MutationObserver to hook into creation of elements (filtered on CSS selector) or removal of nodes.

Este script no debería instalarse directamente. Es una biblioteca que utilizan otros scripts mediante la meta-directiva de inclusión // @require https://update.greatest.deepsurf.us/scripts/565361/1853994/Node%20Mutation%20Hook%20Library.js

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Advertisement:

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

Advertisement:

// ==UserScript==
// @name        Node Mutation Hook Library
// @namespace   https://greatest.deepsurf.us/users/1545341
// @version     3.0.1
// @license     MIT
// @author      abcenjoyer
// @description A wrapper around MutationObserver to hook into creation of elements (filtered on CSS selector) or removal of nodes.
// ==/UserScript==

/*
 * Copyright (c) 2026 abcenjoyer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

// callbacks should return a truthy value when they want no more elements to be processed, and falsy value when they want to continue processing
function hookCreation(selector, callback, callbackOnExisting) {
  const observerTarget = document.documentElement;

  if (callbackOnExisting) {
    for (const element of observerTarget.querySelectorAll(selector)) {
      if (callback(element)) {
        return;
      }
    }
  }

  const observer = new MutationObserver((mutations) => {
    for (const record of mutations) {
      for (const node of record.addedNodes) {
        if (node.nodeType === Node.ELEMENT_NODE) {
          if (node.matches(selector)) {
            if (callback(node)) {
              observer.disconnect();
              return;
            }
          }

          for (const element of node.querySelectorAll(selector)) {
            if (callback(element)) {
              observer.disconnect();
              return;
            }
          }
        }
      }
    }
  });

  observer.observe(observerTarget, {
    childList: true,
    subtree: true
  });
}

function hookRemoval(targetNode, callback) {
  const observer = new MutationObserver((mutations) => {
    for (const record of mutations) {
      for (const node of record.removedNodes) {
          if (node.contains(targetNode)) {
            observer.disconnect();
            callback();
          }
      }
    }
  });

  // observing the entire document is required because if targetNode is deleted because an ancestor node was, observing the parent won't work
  observer.observe(document, {
    childList: true,
    subtree: true
  });
}