A wrapper around MutationObserver to hook into creation of elements (filtered on CSS selector) or removal of nodes.
Hindi dapat direktang i-install ang script na ito. Ito ay isang library para sa iba pang mga script na isasama sa meta directive. // @require https://update.greasyfork.org/scripts/565361/1853994/Node%20Mutation%20Hook%20Library.js
// ==UserScript==
// @name Node Mutation Hook Library
// @namespace https://greasyfork.org/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
});
}