y_method

ちょっとした機能

2021-01-10 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greasyfork.org/scripts/419955/889438/y_method.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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         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);