您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
MutationObserver wrapper to wait for the specified CSS selector
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/12228/174471/setMutationHandler.js
- /* EXAMPLE:
- setMutationHandler(document, '.container p.some-child', function(nodes) {
- // single node:
- nodes[0].remove();
- // or multiple nodes:
- nodes.forEach(node => node.style.display = 'none');
- // disconnect the observer, this is useful for one-time jobs
- return false;
- });
- // the observation target parameter may be omitted so "document" will be used by default:
- // setMutationHandler('#some-id', function(nodes) { ....... });
- // process existing elements and set a mutation handler for the future ones
- processExistingAndSetMutationHandler('a', processLinks);
- */
- // ==UserScript==
- // @name setMutationHandler
- // @description MutationObserver wrapper to wait for the specified CSS selector
- // @namespace wOxxOm.scripts
- // @author wOxxOm
- // @grant none
- // @version 3.0.0
- // ==/UserScript==
- function setMutationHandler(baseNode, selector, cb, options) {
- if (typeof baseNode == 'string') {
- options = cb;
- cb = selector;
- selector = baseNode;
- baseNode = document;
- }
- var ob;
- if (/^#[\w\d-]+$/.test(selector)) {
- selector = selector.substr(1);
- ob = new MutationObserver(MOhandlerForId);
- } else {
- ob = new MutationObserver(MOhandler);
- }
- ob.observe(baseNode || document, options || {subtree:true, childList:true});
- return ob;
- function MOhandler(mutations, observer) {
- if (mutations.length > 100 && !document.querySelector(selector))
- return;
- var found = [];
- for (var i=0, m; (m = mutations[i++]); ) {
- switch (m.type) {
- case 'childList':
- var nodes = m.addedNodes, nl = nodes.length;
- var textNodesOnly = true;
- for (var j=0; j < nl; j++) {
- var n = nodes[j];
- textNodesOnly &= n.nodeType == 3; // TEXT_NODE
- if (n.nodeType != 1) // ELEMENT_NODE
- continue;
- if (n.matches(selector))
- found.push(n);
- else if (n.querySelector(selector)) {
- n = n.querySelectorAll(selector);
- if (n.length < 1000)
- found.push.apply(found, n);
- else
- found = found.concat(found.slice.call(n));
- }
- }
- if (textNodesOnly && m.target.matches(selector))
- found.push(m.target);
- break;
- case 'attributes':
- if (m.target.matches(selector))
- found.push(m.target);
- break;
- case 'characterData':
- if (m.target.parentNode && m.target.parentNode.matches(selector))
- found.push(m.target.parentNode);
- break;
- }
- }
- if (!found.length)
- return;
- if (cb.call(ob, found) === false)
- ob.disconnect();
- }
- function MOhandlerForId(mutations, observer) {
- var el = document.getElementById(selector);
- if (!el || !baseNode.contains(el))
- return;
- if (cb.call(ob, [el]) === false)
- ob.disconnect();
- }
- }
- function processExistingAndSetMutationHandler(baseNode, selector, cb, options) {
- if (typeof baseNode == 'string') {
- options = cb;
- cb = selector;
- selector = baseNode;
- baseNode = document;
- }
- baseNode = baseNode || document;
- if (baseNode.querySelector(selector))
- cb.call(null, Array.prototype.slice.call(baseNode.querySelectorAll(selector)));
- setMutationHandler(baseNode, selector, cb, options);
- }