XHR_INTERCEPTOR

XHR请求拦截工具类

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greatest.deepsurf.us/scripts/494576/1377969/XHR_INTERCEPTOR.js

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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!)

// ==UserScript==
// @name         XHR_INTERCEPTOR
// @version      1.0.0
// @description  XHR请求拦截工具类
// @author       Zosah
// ==/UserScript==

class XMRInterceptor {
        constructor() {
            if (XMRInterceptor.instance) {
                return XMRInterceptor.instance;
            }
            this.interceptors = [];
            XMRInterceptor.instance = this;
        }

        addUrlInterceptor(item) {
            this.interceptors.push(item);
        }

        execute() {
            function xhrInterceptor(callback) {
                // send拦截
                let oldSend = null;
                // 在xhr函数下创建一个回调
                XMLHttpRequest.callbacks = function (xhr) {
                    //调用劫持函数,填入一个function的回调函数
                    //回调函数监听了对xhr调用了监听load状态,并且在触发的时候再次调用一个function,进行一些数据的劫持以及修改
                    xhr.addEventListener("load", function () {
                        if (xhr.readyState == 4 && xhr.status == 200) {
                            callback(xhr);
                        }
                    });
                };
                //获取xhr的send函数,并对其进行劫持
                oldSend = XMLHttpRequest.prototype.send;
                XMLHttpRequest.prototype.send = function () {
                    XMLHttpRequest.callbacks(this);
                    //由于我们获取了send函数的引用,并且复写了send函数,这样我们在调用原send的函数的时候,需要对其传入引用,而arguments是传入的参数
                    oldSend.apply(this, arguments);
                };
            }
            xhrInterceptor((xhr) => {
                for (const item of this.interceptors) {
                    const url = xhr.responseURL.split(item.domain)[1];
                    if (url.indexOf(item.url) !== -1) {
                        item.cb(JSON.parse(xhr.response));
                    }
                }
            });
        }
    }