XMLHttpRequest拦截请求和响应

环境: angular

实现: 拦截请求 向请求信息增加字段

拦截响应 过滤返回值
响应拦截:
根据angular使用的XMLHttpRequest 将对原本的请求转移到另一个将监听返回事件挂载到另一个世纪发送请求的xml上
使用get set 将客户端获取的responseText和response按照自己的意愿返回实现响应拦截
请求拦截

比较简单了 网上也比较常见

修改send函数的参数即可

typescript 复制代码
const CommentReg = new RegExp(
    /\\/api\\/.+\\/.+\\/[0-9a-f]{24}\\/comment(\\/[0-9a-f]{24})*/
);
const GetCommentsReg = new RegExp(
    /\\/api\\/.+\\/.+\\/[0-9a-f]{24}\\/comments*/
);

const MyXMLHttpRequest = window.XMLHttpRequest;

class InterceptXML extends window.XMLHttpRequest {
    constructor(...p) {
        super(...p);
    }

    addEventListener(t, fn) {
        super.addEventListener(t, fn)
    }

    get hasInjectDom() {
        return document.getElementById("insertCheckBox")
    }

    _statusText = "";
    
    get statusText() {
        return this._statusText || super.statusText;
    }

    set statusText(val) {
        this._statusText = val;
    }

    _status = "";

    get status() {
        return this._status || super.status;
    }

    set status(val) {
        this._status = val;
    }

    
    _response = "";

    get response() {
        return this._response || super.response;
    }

    set response(val) {
        this._response = val;
    }

    
    _responseText = "";

    get responseText() {
        return this._responseText || super.responseText;
    }

    set responseText(val) {
        this._responseText = val;
    }

    
    cover(method, url) {
        const xml = new MyXMLHttpRequest();
        xml.open(method, url, true);

        this.addEventListener = (type,callback) => {
            if (type == 'load') {
                this.getAllResponseHeaders = () => {
                    return xml.getAllResponseHeaders()
                }
                xml.addEventListener(type, () => {
                    this.statusText = xml.statusText;
                    this.status = xml.status;
                    this.response = xml.response;
                    this.responseText = xml.responseText;
                    callback()
                })
                // 处理dom
                xml.addEventListener("loadend", () => {
                    requestAnimationFrame(() => {
                        requestAnimationFrame(() => {
                            
                        })
                    })
                })
            }
            else xml.addEventListener(type,callback)
        }

        this.setRequestHeader = (...r) => {
            xml.setRequestHeader(...r)
        }

        this.send = () => {
            xml.send();
        }
    }

    open(method, url) {
        if (method === 'GET' && GetCommentsReg.test(url)) { 
           return this.cover(method, url);
        } else {
            if (["POST", "PUT","DELETE"].includes(method) && CommentReg.test(url) && this.hasInjectDom) {
                const originalSend = super.send;
                super.send = function (data) {
                    const modifiedData = Object.assign(
                        { is_private: window._is_private_comment || false },
                        JSON.parse(data)
                    );
                    originalSend.call(this,JSON.stringify(modifiedData));
                };
            }
            super.open(method, url);
        }
    }
}

window.XMLHttpRequest = InterceptXML;
相关推荐
共享家95271 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Halo_tjn3 小时前
基于封装的专项 知识点
java·前端·python·算法
摘星编程3 小时前
OpenHarmony环境下React Native:自定义useTruncate文本截断
javascript·react native·react.js
Duang007_4 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
2601_949868365 小时前
Flutter for OpenHarmony 电子合同签署App实战 - 主入口实现
开发语言·javascript·flutter
m0_748229995 小时前
Vue2 vs Vue3:核心差异全解析
前端·javascript·vue.js
C澒5 小时前
前端监控系统的最佳实践
前端·安全·运维开发
xiaoxue..6 小时前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试
摘星编程6 小时前
在OpenHarmony上用React Native:自定义useHighlight关键词高亮
javascript·react native·react.js
hhy_smile6 小时前
Class in Python
java·前端·python