setMutationHandler

MutationObserver wrapper to wait for the specified CSS selector

目前為 2016-07-11 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/12228/135916/setMutationHandler.js

  1. /* EXAMPLE:
  2. setMutationHandler(document, '.container p.some-child', function(nodes) {
  3. // single node:
  4. nodes[0].remove();
  5. // or multiple nodes:
  6. nodes.forEach(function(node) {
  7. node.style.display = 'none';
  8. });
  9.  
  10. //this.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 2.0.5
  22. // ==/UserScript==
  23.  
  24. function setMutationHandler(baseNode, selector, cb, options) {
  25. var ob = new MutationObserver(function handler(mutations, fromTimer) {
  26. if (mutations.length > 100 && !fromTimer)
  27. return setTimeout(function() { handler(mutations, true) }, 0);
  28.  
  29. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
  30. switch (m.type) {
  31. case 'childList':
  32. if (m.addedNodes[0] && m.addedNodes[0].nodeType == 3) { // TEXT_NODE
  33. if (m.target.matches(selector) && !cb.call(ob, [m.target], m))
  34. return;
  35. continue;
  36. }
  37. for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
  38. if (n.nodeType == 1) { // ELEMENT_NODE
  39. n = n.matches(selector) ? [n] : Array.prototype.slice.call(n.querySelectorAll(selector));
  40. if (n.length && !cb.call(ob, n, m))
  41. return;
  42. }
  43. break;
  44. case 'attributes':
  45. if (m.target.matches(selector) && !cb.call(ob, [m.target], m))
  46. return;
  47. break;
  48. case 'characterData':
  49. if (m.target.parentNode && m.target.parentNode.matches(selector) && !cb.call(ob, [m.target.parentNode], m))
  50. return;
  51. break;
  52. }
  53. });
  54. ob.observe(baseNode, options || {subtree:true, childList:true});
  55. return ob;
  56. }