Greasy Fork

OsuPageObserver

Detects dynamic osu page changes

当前为 2022-03-06 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/441010/1024850/OsuPageObserver.js

class OsuWebObserver {
    constructor(callback) {
        this.callback = callback;
        const firstLoad = new MutationObserver(() => {
            this.callback();
            firstLoad.disconnect();
        });
        const layout = document.querySelector(".osu-layout__section")?.firstElementChild;
        if (layout != null) {
            firstLoad.observe(layout, {childList: true});
        }
        this.pageObserver = new MutationObserver(this.detectPageChange);
        this.pageObserver.observe(document.documentElement, {childList: true});
    }
    
    detectPageChange = (mutations) => {
        for (const mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeName == "BODY") {
                        this.callback();
                        return;
                    }
                });
            }
        }
    }

    destroy = () => {
        this.pageObserver.disconnect();
    }
}