onDomChange

Dom变化监听

Bu script direkt olarak kurulamaz. Başka scriptler için bir kütüphanedir ve meta yönergeleri içerir // @require https://update.greasyfork.org/scripts/464529/1183888/onDomChange.js

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         onDomChange
// @namespace    http://bbs.91wc.net/?onDomChange
// @version      0.1.2
// @description  Dom变化监听
// @author       Wilson
// ==/UserScript==
 
function onDomChange(el, callback, config) {
    // 观察器的配置(需要观察什么变动)
    config = config || { attributes: true, childList: true, subtree: true };
    // 当观察到变动时执行的回调函数
    const handler = function(mutationsList, observer) {
        // Use traditional 'for loops' for IE 11
        for(let mutation of mutationsList) {
            callback(observer, mutation);
        }
    };
    // 创建一个观察器实例并传入回调函数
    const observer = new MutationObserver(handler);

    // 以上述配置开始观察目标节点
    el = typeof el === 'string' ? document.querySelector(el) : el;
    observer.observe(el, config);

    //再次开始观察,通常用于先停止监听处理后再监听的场景,防止死循环
    observer.observeAgain = function(ele, cfg){
        observer.observe(ele || el, cfg || config);
    }

    // 之后,可停止观察
    //observer.disconnect();

    return observer;
}

//使用示例:
//说明:childList是子元素,subtree是后代元素
//onDomChange(el, (observer, mutation) => {
    //your code

    //if (mutation.type === 'childList'){
        //do something
    //}
    //if(observer) observer.disconnect();
    //observer.observeAgain();
//}, {childList: true});