y_method

dom取得等の機能追加

当前为 2022-04-24 提交的版本,查看 最新版本

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         y_method
// @version      0.2
// @description  dom取得等の機能追加
// @author       y_kahou
// ==/UserScript==


const y_method = {
    /**
     * スタイルを追加する
     * @param id {string} - スタイルのID
     * @param css {string} - css本体
     */
    addStyle: function(id, css) {
        let style = document.createElement('style')
        style.id = id
        style.setAttribute('type', 'text/css')
        style.textContent = css
        document.querySelector('head').appendChild(style)
    },
    /**
     * 対象までスクロールせずにクリックする
     * @param selector {string} - 取得対象のセレクタ
     */
    click_: function(element) {
        let x = window.scrollX, y = window.scrollY
        element.click()
        window.scrollTo(x, y)
    },
    /**
     * 対象までスクロールせずにフォーカスする
     * @param selector {string} - 取得対象のセレクタ
     */
    focus_: function(element) {
        let x = window.scrollX, y = window.scrollY
        element.focus()
        window.scrollTo(x, y)
    },
    /**
     * 対象のdomを取得できるまで取得を挑戦する
     * @param selector {string} - 取得対象のセレクタ
     * @param interval {number} - 次の挑戦までの時間ms
     * @param repeat   {number} - 繰り返し回数
     */
    repeatGetElements: function(selector, interval = 500, repeat = 60) {
        return new Promise(function(resolve, reject) {
            let cnt = 0
            let it = setInterval(function() {
                if (++cnt > repeat) {
                    clearInterval(it)
                    reject("Could'n get " + selector)
                }
                let ret = document.querySelectorAll(selector)
                if (ret.length > 0) {
                    clearInterval(it)
                    resolve(ret)
                }
            }, interval)
        })
    },
    /**
     * src込みのvideo要素の取得
     */
    getVideo: async function(selector = 'video') {
        let video
        for (var i = 0; i < 60; i++) {
            video = await repeatGetElements(selector)
            if (video[0].getAttribute('src'))
                break
            await wait(500)
        }
        return video
    },

    /**
     * ファイル名に使えない文字を半角から全角へ変換する
     * @param name {string} - ファイル名
     */
    filenameEscape: function(name) {
        const target = ['\\', '/', ':', '*', '?', '"', '<', '>', '|', ]
        const rep = ['\', '/', ':', '*', '?', '”', '<', '>', '|', ]
        let ename = name
        for (let i = 0; i < target.length; i++) {
            ename = ename.replaceAll(target[i], rep[i])
        }
        return ename
    },

    /**
     * SPA等のページ遷移をイベントで検知できるようにする
     * Event: pagetransition
     */
    DetectPagetransition: function() {
        let agoHref = location.href;
        new MutationObserver(() => {
            if (agoHref != location.href) {
                const event = new CustomEvent('pagetransition', { detail: location.href });
                document.dispatchEvent(event);
                agoHref = location.href;
            }
        })
        .observe(document.body, { childList: true, subtree: true ,attributes: true })
    },
}

if (window.jQuery) (function($) {
    /**
     * 対象までスクロールせずにクリックする
     */
    $.fn.click_ = function() {
        y_method.click_(this[0])
        return this
    }
    /**
     * 対象までスクロールせずにフォーカスする
     */
    $.fn.focus_ = function() {
        y_method.focus_(this[0])
        return this
    }
})(window.jQuery);