setMutationHandler

MutationObserver wrapper to wait for the specified CSS selector

目前为 2015-09-05 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/12228/72410/setMutationHandler.js

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

		//observer.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       1
// ==/UserScript==

function setMutationHandler(baseNode, selector, cb) {
	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(ob, n))
							return;
	});
	ob.observe(baseNode, {subtree:true, childList:true}); 
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址