y_method

ちょっとした機能

Stan na 10-01-2021. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/419955/889438/y_method.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         y_method
// @version      0.1
// @description  ちょっとした機能
// @author       y_kahou
// ==/UserScript==


/**
 * 対象までスクロールせずにクリックする
 * @param selector {string} - 取得対象のセレクタ
 */
function click_(element) {
    var x = window.scrollX, y = window.scrollY
    element.click()
    window.scrollTo(x, y)
}
/**
 * 対象までスクロールせずにフォーカスする
 * @param selector {string} - 取得対象のセレクタ
 */
function focus_(element) {
    var 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) {
        var cnt = 0
        var it = setInterval(function() {
            if (++cnt > 60) {
                clearInterval(it)
                reject("Could'n get " + selector)
            }
            var ret = document.querySelectorAll(selector)
            if (ret.length > 0) {
                clearInterval(it)
                resolve(ret)
            }
        }, interval)
    })
}
/**
 * async関数内で使えるwait
 */
function wait(ms = 100) {
    return new Promise(function(resolve, reject) {
        setTimeout(() => resolve(), ms)
    })
}


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