node-creation-observer

Callback binding for HTML Node creation events

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://updategreasyfork.deno.dev/scripts/19857/174436/node-creation-observer.js

Autor
soufianesakhi
Wersja
0.0.1.20170211003743
Utworzono
21-05-2016
Zaktualizowano
11-02-2017
Rozmiar
2,98 KB
Licencja
Brak licencji

NodeCreationObserverJS

Use cases

1: Apply a callback each time a type of node is created

Fire a callback each time an element that matches the selector is created. Don't apply the callback for the same element multiple times. In this case, the removeOnFirstMatch property should be set to false.

2: Wait for the creation of one node and apply a callback

Fire a callback when the first element that matches the selector is created. In this case, the removeOnFirstMatch property should be set to true.

Definition

// Add a new callback for a selector
NodeCreationObserver.onCreation(
  String selector,
  function callback,
  boolean removeOnFirstMatch (optionnal, default value: false)
);

// Stop observing a selector 
NodeCreationObserver.remove(
  String selector
);

// Stop observing all the selectors
NodeCreationObserver.stop();

Usage

// Use case 1
NodeCreationObserver.onCreation("MY_SELECTOR", function (element) {
    // callback body
});

// Use case 2
NodeCreationObserver.onCreation("#my_element_id", function (element) {
    // callback body
}, true);

// When observing "MY_SELECTOR" is no longer needed
NodeCreationObserver.remove("MY_SELECTOR");

// When node observing is no longer needed
NodeCreationObserver.stop();

Implementation

Based on the MutationObserver API