y_method

dom取得等の機能追加

目前為 2022-05-17 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/419955/1051148/y_method.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         y_method
// @version      0.3
// @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
    },
    
    /**
     * pagetransitionイベントを発火
     * target: document
     * event: pagetransition
     */
    TriggerPagetransition: function() {
        const event = new CustomEvent('pagetransition', { detail: location.href });
        document.dispatchEvent(event);
    },
    /**
     * SPA等のページ遷移をイベントで検知できるようにする
     * target: document
     * event: pagetransition
     */
    DetectPagetransition: function() {
        let agoHref = location.href;
        new MutationObserver(() => {
            if (agoHref != location.href) {
                y_method.TriggerPagetransition();
                agoHref = location.href;
            }
        })
        .observe(document.body, { childList: true, subtree: true ,attributes: true })
    },
    /**
     * 指定のページごとのcssと初期動作関数List
     * @param pageList [{match, css, run},]
     * @param match url RegExp
     * @param css css text
     * @param run function
     */
    PageLauncher: function(launcherList) {
        document.addEventListener('pagetransition', e => {
            for (const s of document.querySelectorAll('.spa-style')) {
                s.outerHTML = '';
            }
            for (const d of pageList) {
                if (new RegExp(d.match).test(location.href)) {
                    let style = document.createElement('style')
                    style.className = 'spa-style'
                    style.setAttribute('type', 'text/css')
                    style.textContent = d.css
                    document.head.appendChild(style)
                    if (d.run && typeof d.run === 'function') d.run();
                }
            }
        })
    },
}
 
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);