Greasy Fork is available in English.

setMutationHandler

MutationObserver wrapper to wait for the specified CSS selector

Verze ze dne 05. 09. 2015. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greatest.deepsurf.us/scripts/12228/72406/setMutationHandler.js

  1. /* EXAMPLE:
  2. setMutationHandler(document, '.container p.some-child', function(observer, nodes) {
  3. // single node:
  4. nodes[0].remove();
  5. // or multiple nodes:
  6. [].forEach.call(nodes, function(node) {
  7. node.style.display = 'none';
  8. });
  9.  
  10. //observer.disconnect(); // disconnect the observer, this is useful for one-time jobs
  11. return true; // continue enumerating current batch of mutations
  12. });
  13. */
  14.  
  15. // ==UserScript==
  16. // @name setMutationHandler
  17. // @description MutationObserver wrapper to wait for the specified CSS selector
  18. // @namespace wOxxOm.scripts
  19. // @author wOxxOm
  20. // @grant none
  21. // @version 1
  22. // ==/UserScript==
  23.  
  24. function setMutationHandler(baseNode, selector, cb) {
  25. var ob = new MutationObserver(function(mutations) {
  26. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
  27. for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
  28. if (n.nodeType == 1)
  29. if ((n = n.matches(selector) ? [n] : n.querySelectorAll(selector)) && n.length)
  30. if (!cb(ob, n))
  31. return;
  32. });
  33. ob.observe(baseNode, {subtree:true, childList:true});
  34. }