Greasy Fork

Greasy Fork is available in English.

HttpRequest

HttpRequest for any type of request and HttpRequestHTML to request webpage. Supports caching of responses to handle status 304.

当前为 2020-06-14 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/405144/816123/HttpRequest.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         HttpRequest
// @namespace    hoehleg.userscripts.private
// @version      0.1
// @description  HttpRequest for any type of request and HttpRequestHTML to request webpage. Supports caching of responses to handle status 304.
// @author       Gerrit Höhle
//
// @grant        GM_xmlhttpRequest
//
// @require      http://greasyfork.icu/scripts/405143-simplecache/code/SimpleCache.js
// @require      http://greasyfork.icu/scripts/405144-httprequest/code/HttpRequest.js
//
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
//
// ==/UserScript==

/* jshint esnext: true */
/* globals globals $, jQuery, SimpleCache */
const HttpRequest = (() => {
    const responsesCache = new SimpleCache({ keyExtractor: (httpRequest) => [ httpRequest.method, httpRequest.url, httpRequest.headers, httpRequest.data, httpRequest.params ].join() });

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

    return class HttpRequest {
        constructor({ method, url = window.location.href, onLoad = () => {}, onError = () => {}, headers = '', data = '', keepInCacheTimoutMs = 10 * 60000, params = {} } = {}) {
            Object.assign(this, { method, url, onLoad, onError, headers, data, keepInCacheTimoutMs, params });
        }

        send() {
            const method = this.method.toUpperCase();
            let url = this.url;
            let headers = this.headers;
            let data = this.data;
            const onload = (response) => {
                switch (response.status) {
                    case 200:
                        if (this.keepInCacheTimoutMs > 0) {
                            responsesCache.set(this, response, this.keepInCacheTimoutMs);
                        }
                        break;
                    case 304:
                        if (this.isCached()) {
                            response = this.getFromCache();
                            response.status = 304;
                        }
                        break;
                    default:
                        this.onError(response);
                        return;
                }
                this.onLoad(response);
            };
            const onerror = this.onError;

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

            return GM_xmlhttpRequest({ method, url, onload, onerror: this.onError, headers: this.headers, data });
        }

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

        getFromCache() {
            return responsesCache.get(this);
        }
    };
})();

class HttpRequestHtml extends HttpRequest {
    constructor({ url, method = "GET", onSuccess = () => {}, onError, headers, data, keepInCacheTimoutMs, params } = {}) {
        super({ method, url, onError, headers, data, keepInCacheTimoutMs, params });

        this.onSuccess = onSuccess;

        Object.defineProperty(this, 'onLoad', {
            value: (response) => {
                if (response.status == 200) {
                    response.responseHtml = new DOMParser().parseFromString(response.responseText, 'text/html');
                    this.onSuccess(response);
                }
            },
            enumerable: true
        });
    }
}