y_method

dom取得等の機能追加

Verze ze dne 18. 10. 2021. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/419955/980158/y_method.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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

/**
 * スタイルを追加する
 * @param id {string} - スタイルのID
 * @param css {string} - css本体
 */
function addStyle(id, css) {
    let style = document.createElement('style')
    style.id = id
    style.type = 'text/css'
    style.textContent = css
    document.querySelector('head').appendChild(style)
}
/**
 * 対象までスクロールせずにクリックする
 * @param selector {string} - 取得対象のセレクタ
 */
function click_(element) {
    let x = window.scrollX, y = window.scrollY
    element.click()
    window.scrollTo(x, y)
}
/**
 * 対象までスクロールせずにフォーカスする
 * @param selector {string} - 取得対象のセレクタ
 */
function focus_(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} - 繰り返し回数
 */
function repeatGetElements(selector, interval = 500, repeat = 60) {
    return new Promise(function(resolve, reject) {
        let cnt = 0
        let it = setInterval(function() {
            if (++cnt > 60) {
                clearInterval(it)
                reject("Could'n get " + selector)
            }
            let ret = document.querySelectorAll(selector)
            if (ret.length > 0) {
                clearInterval(it)
                resolve(ret)
            }
        }, interval)
    })
}
/**
 * src込みのvideo要素の取得
 */
async function getVideo(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} - ファイル名
 */
function filenameEscape(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
}

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