OsuWebRequests

simple wrapper for making osu related sequential web requests

Fra og med 06.03.2022. Se den nyeste version.

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greatest.deepsurf.us/scripts/441005/1024922/OsuWebRequests.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!)

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

    send(cb) {
        fetch(this.url).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) {
        const req = new Request(`${this.base}${path}`, params, cb);
        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();
        });
    }
}