Yinr-libs

Useful functions for yinr

ของเมื่อวันที่ 24-01-2023 ดู เวอร์ชันล่าสุด

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greatest.deepsurf.us/scripts/458769/1141569/Yinr-libs.js

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name               Yinr-libs
// @namespace          https://yinr.cc/
// @version            0.0.2
// @description        Useful functions for yinr
// @author             Yinr
// @license            GPL-v3
// ==/UserScript==

const YinrLibs = (function() {
    'use strict';

    /** YinrLibs */
    return {
        /** 创建 DOM 监听
         * @param {Object} option
         * @param {(Element | string)} option.parentNode MutationObserver 绑定的 DOM 对象
         * @param {string} option.selector 需要监听变化的 selector
         * @param {voidCallback} [option.failCallback=callback] selector 不存在时执行的回调函数
         * @param {MutationCallback} [option.successCallback=null] selector 对象发生 Mutation 事件时执行的回调函数
         * @param {boolean} [option.stopWhenSuccess=true] 执行一次 `successCallback` 后立即解除监听
         * @param {MutationObserverInit} [option.config={childList: true, subtree: true}] MutationObserver 配置参数
         */
        launchObserver ({
            parentNode,
            selector,
            failCallback = null,
            successCallback = null,
            stopWhenSuccess = true,
            config = { childList: true, subtree: true }
        }) {
            if (!parentNode) return
            /** @type {MutationCallback} */
            const observeFunc = mutationList => {
                if (!document.querySelector(selector)) {
                    if (failCallback) failCallback()
                    return
                }
                if (stopWhenSuccess) observer.disconnect()

                mutationList.itemFilter = (fn, type = 'addedNodes') => mutationList.map(i => Array.from(i[type]).filter(fn)).reduce((arr, val) => arr.concat(val), [])

                if (successCallback) successCallback(mutationList)
            }
            const observer = new MutationObserver(observeFunc)
            observer.observe(parentNode, config)
        }
    };
})();