HttpRequest

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

اعتبارا من 16-06-2020. شاهد أحدث إصدار.

لا ينبغي أن لا يتم تثبيت هذا السكريت مباشرة. هو مكتبة لسكبتات لتشمل مع التوجيه الفوقية // @require https://update.greasyfork.org/scripts/405144/817102/HttpRequest.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 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.

ستحتاج إلى تثبيت إضافة مثل 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      https://greasyfork.org/scripts/405143-simplecache/code/SimpleCache.js
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js
//
// ==/UserScript==

/* jshint esnext: true */
/* globals SimpleCache, $, jQuery */
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, headers = '', data = '', keepInCacheTimoutMs = 10 * 60000, params = {} } = {}) {
            Object.assign(this, { method, url, headers, data, keepInCacheTimoutMs, params });
        }

        send() {
            return new Promise((resolve, reject) => {
                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:
                            reject(Error(`Status: ${response.status}, Error: ${response.statusText}`));
                            return;
                    }
                    resolve(response);
                };

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

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

                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", headers, data, keepInCacheTimoutMs, params } = {}) {
        super({ method, url, headers, data, keepInCacheTimoutMs, params });
    }
    send() {
        return super.send().then(response => new Promise(resolve => {
            if (response.status == 200) {
                const html = new DOMParser().parseFromString(response.responseText, 'text/html');
                resolve(response, html);
            }
        }));
    }
}