OsuPageObserver

Detects dynamic osu page changes

このスクリプトは単体で利用できません。右のようなメタデータを含むスクリプトから、ライブラリとして読み込まれます: // @require https://update.greasyfork.org/scripts/441010/1025088/OsuPageObserver.js

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
class OsuWebObserver {
    constructor(staticFn, dynamicFn) {
        this.static = staticFn;
        this.dynamic = dynamicFn;
        this.static();
        const pageType = this.getType();
        if (pageType === "dynamic") {
            const firstLoad = new MutationObserver(() => {
                this.dynamic();
                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.static();
                        this.dynamic();
                        return;
                    }
                });
            }
        }
    }

    getType = () => {
        const type = location.pathname.split("/")[1];
        const dynamic = ["users", "beatmapsets", "scores"];
        if (dynamic.includes(type)) {
            return "dynamic";
        }
        return "static";
    }

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