OsuWebRequests

simple wrapper for making osu related sequential web requests

目前为 2022-03-11 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/441005/1027174/OsuWebRequests.js

class Request {
    constructor(url, params, callback, options = {}) {
        this.url = new URL(url);
        this.url.search = new URLSearchParams(params).toString();
        this.callback = callback;
        this.options = options;
    }

    send(cb, ) {
        fetch(this.url, this.options).then(res => {
            cb();
            this.callback(res);
        }).catch(err => {
            cb();
            console.log(err);
        });
    }
}

class Web {
    base = "https://osu.ppy.sh";

    constructor(apiKey = null) {
        this.key = apiKey;
        this.requests = [];
    }

    get(path, params, cb, options = {}) {
        const req = new Request(`${this.base}${path}`, params, cb, options);
        this.queue(req);
    }

    api(path, params, cb) {
        params.k = this.key;
        const req = new Request(`${this.base}/api${path}`, params, cb);
        this.queue(req);
    }

    queue(req) {
        this.requests.push(req);
        if (this.requests.length === 1) {
            this.next();
        }
    }

    next() {
        if (this.requests.length === 0) {
            return;
        }
        const req = this.requests[0];
        req.send(() => {
            this.requests.shift();
            this.next();
        });
    }
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址