QOS-Handler

QOS-Handler(window param ver)

Stan na 03-08-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/430297/956917/QOS-Handler.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         QOS-Handler
// @namespace    http://greasyfork.org/
// @version      0.1
// @description  QOS-Handler(window param ver)
// @author       Cosil.C
// @license      GPL
// ==/UserScript==

/* jshint esversion: 6 */
/**
 * @description 流控相关
 */
document.defaultView.qos = {
    /**
     * @description 记录这次的访问时间戳,并返回是否超出限制
     * @param sec 时间限制
     * @param timesLimit 次数限制
     * @returns sec秒内访问了超过timesLimit次 ? true : false
     */
    record: (sec, timesLimit) => {
        sec = sec || 5;
        timesLimit = timesLimit || 10;
        console.log(`start qos recording...\nsec:${sec},timesLimit:${timesLimit}`);
        let timestamp = new Date().getTime(), historyArr = document.defaultView.qos.getRecord();
        historyArr.push(timestamp);
        if (historyArr.length > timesLimit) {
            let shift;
            do {
                shift = parseInt(historyArr.shift());
            } while (historyArr.length > timesLimit);
            //
            if (timestamp - shift <= sec * 1000) {
                let format = function (target) {
                    return new Date(target).toTimeString().substr(0, 8);
                };
                console.log(`current:${format(timestamp)}, shift:${format(shift)}, interval(sec):${(timestamp - shift) / 1000}`);
                return true;
            }
        }
        localStorage.setItem('historyArr', JSON.stringify(historyArr));
        console.log('qos recorded');
        return false;
    },
    /**
     * @description 清除缓存中的记录
     */
    clearRecord: () => {
        localStorage.removeItem('historyArr');
    },
    /**
     * @description 获取记录的历史
     * @returns 历史记录数组
     */
    getRecord: () => {
        let historyArr;
        try {
            historyArr = JSON.parse(localStorage.getItem('historyArr') || '[]');
        } catch (e) {
            console.error(e);
            console.error(`cause:${localStorage.getItem('historyArr')}`);
            document.defaultView.qos.clearRecord();
            historyArr = [];
        }
        return historyArr;
    }
};