setMutationHandler

MutationObserver wrapper to wait for the specified CSS selector

Tính đến 12-10-2015. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greatest.deepsurf.us/scripts/12228/79812/setMutationHandler.js

/* EXAMPLE:
	
	setMutationHandler(document, '.container p.some-child', function(nodes) {
		// single node:
		nodes[0].remove();
		
		// or multiple nodes:
		nodes.forEach(function(node) {
			node.style.display = 'none';
		});

		//this.disconnect(); // disconnect the observer, this is useful for one-time jobs
		return true; // continue enumerating current batch of mutations
	});
*/

// ==UserScript==
// @name          setMutationHandler
// @description   MutationObserver wrapper to wait for the specified CSS selector
// @namespace     wOxxOm.scripts
// @author        wOxxOm
// @grant         none
// @version       2
// ==/UserScript==

function setMutationHandler(baseNode, selector, cb, options) {
	var ob = new MutationObserver(function(mutations) {
		for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
			for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
				if (n.nodeType == 1) 
					if ((n = n.matches(selector) ? [n] : n.querySelectorAll(selector)) && n.length)
						if (!cb.call(ob, Array.prototype.slice.call(n)))
							return;
	});
	ob.observe(baseNode, options || {subtree:true, childList:true}); 
	return ob;
}