Greasy Fork is available in English.

make-mutation-observer

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

Este script no debería instalarse directamente. Es una biblioteca que utilizan otros scripts mediante la meta-directiva de inclusión // @require https://update.greasyfork.org/scripts/488160/1335044/make-mutation-observer.js

  1. // ==UserScript==
  2. // @name make-mutation-observer
  3. // @description Simple wrapper around `MutationObserver` API to watch DOM changes.
  4. // @version 0.1.0
  5. // @namespace owowed.moe
  6. // @author owowed <island@owowed.moe>
  7. // @license LGPL-3.0
  8. // ==/UserScript==
  9.  
  10. /**
  11. * @typedef {(info: { records: MutationRecord[], observer: MutationObserver }) => void} MakeMutationObserverCallback
  12. */
  13.  
  14. /**
  15. * @typedef MakeMutationObserverOptionsBasic
  16. * @prop {HTMLElement} target
  17. * @prop {MakeMutationObserverCallback} callback
  18. * @prop {AbortSignal} [abortSignal]
  19. * @prop {boolean} [once]
  20. */
  21.  
  22. /** @typedef {MakeMutationObserverOptionsBasic & MutationObserverInit} MakeMutationObserverOptions */
  23.  
  24. /**
  25. * Create a new `MutationObserver` from target and callback.
  26. * @param {Node} target target node
  27. * @param {MakeMutationObserverCallback} callback observer callback
  28. * @param {MakeMutationObserverOptions} options additional observer options
  29. * @returns {MutationObserver}
  30. */
  31. function makeMutationObserver(target, callback, options) {
  32. return makeMutationObserverOptions({ target, ...options }, callback);
  33. }
  34.  
  35. /**
  36. * Create a new `MutationObserver` with options and callback.
  37. * @param {MakeMutationObserverOptions} options
  38. * @param {MakeMutationObserverCallback} callback
  39. * @returns {MutationObserver}
  40. */
  41. function makeMutationObserverOptions({ target, abortSignal, once, ...options }, callback) {
  42. const observer = new MutationObserver(records => {
  43. abortSignal?.throwIfAborted();
  44. if (once) observer.disconnect();
  45. callback({ records, observer });
  46. });
  47.  
  48. observer.observe(target, options);
  49.  
  50. abortSignal?.addEventListener("abort", () => {
  51. observer.disconnect();
  52. });
  53.  
  54. return observer;
  55. }