js 监听ajax请求

function hookSend(hook) {
  if (!XMLHttpRequest.prototype._oldSend)
    XMLHttpRequest.prototype._oldSend = XMLHttpRequest.prototype.send;
  XMLHttpRequest.prototype.send = function () {
    if (hook && typeof hook === "function") hook([...arguments], this);
    return this._oldSend(...arguments);
  };
}
hookSend((a,c) => {
  console.log("args: %o, this: %o", a, c)
})

监听你的send是否被挂钩

const _oldSend = XMLHttpRequest.prototype.send;
Object.defineProperty(XMLHttpRequest.prototype, "send", {
  set: function (v) {
    console.log("[XHR] set send!!!");
  },
  get: function () {
    return _oldSend;
  },
});

你可能还需要保护所有检测函数,避免被挂钩。

原文地址:https://www.cnblogs.com/ajanuw/p/13775888.html