From 689f785d0ea03a299cd47932d0a7180a8b8fddd3 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 13 Jan 2018 14:58:52 +0200 Subject: [PATCH] Add observeMutations.md --- snippets/observeMutations.md | 23 +++++++++++++++++++++++ tag_database | 1 + 2 files changed, 24 insertions(+) create mode 100644 snippets/observeMutations.md diff --git a/snippets/observeMutations.md b/snippets/observeMutations.md new file mode 100644 index 000000000..d78e5cc39 --- /dev/null +++ b/snippets/observeMutations.md @@ -0,0 +1,23 @@ +### observeMutations + +Runs the provided callback for each mutation on the specified element. + +Use a [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to observe mutations on the given element. +Use `Array.forEach()` to run the callback for each mutation that is observed. +Omit the third argument, `options`, to use the default [options](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#MutationObserverInit) (all `true`). + +```js +const observeMutations = (element, callback, options) => new MutationObserver(mutations => mutations.forEach(m => callback(m))).observe(element, Object.assign( + { + childList: true, + attributes: true, + attributeOldValue: true, + characterData: true, + characterDataOldValue: true, + subtree: true + }, options)); +``` + +```js +observeMutations(document, console.log); // Logs all mutations that happen on the page +``` diff --git a/tag_database b/tag_database index e1d4aa6aa..040f5371a 100644 --- a/tag_database +++ b/tag_database @@ -119,6 +119,7 @@ negate:function nthElement:array objectFromPairs:object,array objectToPairs:object,array +observeMutations:browser,event,advanced off:browser,event on:browser,event once:function