Greasy Fork

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-04 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.cloud/scripts/405144/1057705/HttpRequest%20Library.js

// ==UserScript==
// @name         HttpRequest Library
// @namespace    hoehleg.userscripts.private
// @version      0.2
// @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 */
const HttpRequest = (() => {

    const urlWithParams = (url, paramsObject) => {
        const params = Object.entries(paramsObject).map(([key, value]) => key + '=' + value).join('&');
        return params.length ? url + '?' + params : url;
    };

    const responsesCache = new Map();

    const requestKey = ({ method, url, params, data }) => `${method}:${urlWithParams(url, params)}:DATA:${data}`;

    return class HttpRequest {
        constructor({
            method,
            url,
            headers = {},
            data = '',
            keepInCacheTimoutMs = 0,
            params = {}
        } = {}) {
            Object.assign(this, { method, url, headers, data, params, keepInCacheTimoutMs });
        }
        async send() {
            if (!this.method || !this.url) {
                return await Promise.reject(Error("invalid request"));
            }
            return await new Promise((resolve, reject) => {
                let method, url, onload, onerror, headers, data;
                method = this.method.toUpperCase();
                url = this.url;
                headers = this.headers;
                data = this.data;

                onload = (response) => {
                    switch (response.status) {
                        case 200:
                            if (this.keepInCacheTimoutMs) {
                                const key = requestKey(this);
                                responsesCache.set(key, response);

                                if (this.keepInCacheTimoutMs > 0) {
                                    setTimeout(() => responsesCache.delete(key), this.keepInCacheTimoutMs);
                                }
                            }
                            break;
                        case 304:
                            if (this.isCached()) {
                                response = this.readFromCache();
                                response.status = 304;
                            }
                            break;
                        default:
                            reject(Error(`Status: ${response.status}, Error: ${response.statusText}`));
                            return;
                    }
                    resolve(response);
                };

                onerror = () => {
                    reject(Error('network error'));
                };

                switch (method) {
                    case 'GET':
                        if (this.params) {
                            url = urlWithParams(url, this.params);
                        }
                        break;
                    case 'POST':
                    case 'PUT':
                        headers = Object.assign({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, headers || {});
                        if (this.params) {
                            data = urlWithParams(data, this.params);
                        }
                        break;
                }

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

        isCached() {
            return responsesCache.has(requestKey(this));
        }

        readFromCache() {
            return responsesCache.get(requestKey(this));
        }

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

class HttpRequestHtml extends HttpRequest {
    constructor({
        url,
        params = {},
        keepInCacheTimoutMs = 0,
        pageNr = 0,
        pagesMaxCount = 1,
        resultTransformer = (resp, _httpRequestHtml) => resp,
        hasNextPage = (_resp, _httpRequestHtml) => false,
        urlConfiguratorForPageNr = (url, _pageNr) => url,
        paramsConfiguratorForPageNr = (params, _pageNr) => params,
    } = {}) {
        super({ method: 'GET', url, params, keepInCacheTimoutMs });
        Object.assign(this, {
            pageNr,
            pagesMaxCount: Math.max(0, pagesMaxCount),
            resultTransformer,
            hasNextPage,
            urlConfiguratorForPageNr,
            paramsConfiguratorForPageNr
        });
    }

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

    async send() {
        const results = [];

        let response = null, requestForPage = null;

        for (let pageNr = this.pageNr; pageNr < this.pageNr + this.pagesMaxCount; pageNr++) {
            if (requestForPage && !this.hasNextPage(response, requestForPage)) {
                break;
            }

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

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

    /**
     * @param {*} args { url, params, keepInCacheTimoutMs }
     * @returns  Promise of response with { status, html }
     */
    static async send(...args) {
        return await new HttpRequestHtml(...args).send();
    }
}