HttpRequest Library

HttpRequest for any type of request and HttpRequestHTML to request webpage. Supports caching of responses for a given period and paging.

2022-06-21 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/405144/1063037/HttpRequest%20Library.js

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         HttpRequest Library
// @namespace    hoehleg.userscripts.private
// @version      0.6
// @description  HttpRequest for any type of request and HttpRequestHTML to request webpage. Supports caching of responses for a given period and paging.
// @author       Gerrit Höhle
//
// @grant        GM_xmlhttpRequest
//
// ==/UserScript==

/* jslint esversion: 9 */
class HttpRequest {
    constructor({
        method,
        url,
        headers = {},
        data = '',
        params = {},
        contentType = 'application/x-www-form-urlencoded; charset=UTF-8',
        responseType = null,
    } = {}) {
        const urlWithParams = (url, paramsObject) => {
            const params = Object.entries(paramsObject).map(([key, value]) => key + '=' + value).join('&');
            return params.length ? url + '?' + params : url;
        };

        switch (method.toUpperCase()) {
            case 'GET':
            case 'HEAD':
                if (params) {
                    url = urlWithParams(url, params);
                }
                break;
            default:
                headers = Object.assign({ 'Content-Type': contentType }, headers || {});

                if (params) {
                    if (typeof data === 'string') {
                        try {
                            data = JSON.parse(data);
                        } catch (e) {
                            console.log(e);
                        }
                    }

                    data = { ...(data || {}), ...this.params };
                }
                break;
        }

        /** @type {string} */
        this.method = method.toUpperCase();

        /** @type {string} */
        this.url = encodeURI(url);

        /** @type {object} */
        this.headers = headers;

        /** @type {string} */
        this.data = typeof data === 'string' ? data : JSON.stringify(data);

        /** @type {string} */
        this.responseType = responseType;
    }

    async send() {
        if (!this.method || !this.url) {
            return await Promise.reject("invalid request");
        }

        return await new Promise((resolve, reject) => {
            const { method, url, headers, data, responseType } = this;

            const onload = (response) => {
                resolve(response);
            };

            const onerror = (errorEvent) => {
                console.log(errorEvent);
                reject("network error");
            };

            GM_xmlhttpRequest({ method, url, onload, onerror, headers, data, responseType });
        });
    }

    static async send(...args) {
        return await new HttpRequest(...args).send();
    }
}

class HttpRequestHtml extends HttpRequest {

    /**
     * @param {HttpRequestHtmlParams} param0 
     */
    constructor({
        url,
        params = {},
        pageNr = 0,
        pagesMaxCount = 1,
        resultTransformer = (resp, _httpRequestHtml) => resp,
        hasNextPage = (_resp, _httpRequestHtml, _lastResult) => false,
        urlConfiguratorForPageNr = (url, _pageNr) => url,
        paramsConfiguratorForPageNr = (params, _pageNr) => params,
    } = {}) {
        super({ method: 'GET', url, params });

        Object.assign(this, {
            pageNr,
            pagesMaxCount: Math.max(0, pagesMaxCount),
            resultTransformer,
            hasNextPage,
            urlConfiguratorForPageNr,
            paramsConfiguratorForPageNr,
        });
    }

    clone() {
        return new HttpRequestHtml({ ...this });
    }

    /**
     * @returns {Promise<HttpRequestHtmlResponse|object|Array<object>}
     */
    async send() {
        const results = [];

        let response = null, requestForPage = null;

        for (let pageNr = this.pageNr; pageNr < this.pageNr + this.pagesMaxCount; pageNr++) {
            requestForPage = Object.assign(this.clone(), {
                url: this.urlConfiguratorForPageNr(this.url, pageNr),
                params: this.paramsConfiguratorForPageNr({ ...this.params }, pageNr)
            });

            response = await HttpRequest.prototype.send.call(requestForPage);
            if (response.status == 200 || response.status == 304) {
                response.html = new DOMParser().parseFromString(response.responseText, 'text/html');
            }

            const resultForPage = this.resultTransformer(response, requestForPage);
            results.push(resultForPage);

            if (!this.hasNextPage(response, requestForPage, resultForPage)) {
                break;
            }
        }

        return this.pagesMaxCount > 1 ? results : results[0];
    }

    /**
     * @param {HttpRequestHtmlParams} param0 
     * @returns {Promise<HttpRequestHtmlResponse|object|Array<object>}
     */
    static async send(...args) {
        return await new HttpRequestHtml(...args).send();
    }
}

class HttpRequestJSON extends HttpRequest {
    /** @param {HttpRequestJSONParams} param0 */
    constructor({
        method = 'GET',
        protocol = 'http:',
        hostname = 'localhost',
        port = null,
        pathname = '',
        params = {},
        fallbackResult = null,
        contentType = 'application/json',
    }) {
        const url = Object.assign(new URL("http://example.com"), { protocol, hostname, port, pathname });
        super({ method, url: url.href, params, responseType: "json", contentType });
        this.fallbackResult = fallbackResult;
    }

    /** @returns {Promise<any>} */
    async send() {
        const response = await super.send();
        if (!response || !response.responseText) {
            return this.fallbackResult;
        }

        try {
            return JSON.parse(response.responseText);
        } catch (error) {
            console.log(error);
            return this.fallbackResult;
        }
    }

    /**
     * 
     * @param {HttpRequestJSONParams} param0
     * @returns {Promise<any>}
     */
    static async send(param0) {
        return await new HttpRequestJSON_test(param0).send();
    }
}

class HttpRequestBlob extends HttpRequest {
    constructor({
        method = 'GET',
        url,
        headers,
        data,
        params = {},
        contentType,
    }) {
        super({ method, url, headers, data, params, responseType: 'blob', contentType });
    }

    async send() {
        const response = await super.send();

        /** @type {Blob} */
        const blob = response.response;
        const data = await new Promise((resolve, reject) => {
            const reader = Object.assign(new FileReader(), {

                onload: (load) => {
                    resolve(load.target.result);
                },

                onerror: (error) => {
                    console.log(error);
                    reject(reader.result);
                }

            });
            reader.readAsDataURL(blob);
        });
        return data;
    }

    static async send(param0) {
        return await new HttpRequestBlob(param0).send();
    }
}