所需工具
chrome、chrome暴力猴插件/chromeTampermonkey油猴插件
脚本①——Ajax hook
!function (ob) {
ob.hookAjax = function (funs) {
window._ahrealxhr = window._ahrealxhr || XMLHttpRequest
XMLHttpRequest = function () {
this.xhr = new window._ahrealxhr;
for (var attr in this.xhr) {
var type = "";
try {
type = typeof this.xhr[attr]
} catch (e) {}
if (type === "function") {
this[attr] = hookfun(attr);
} else {
Object.defineProperty(this, attr, {
get: getFactory(attr),
set: setFactory(attr)
})
}
}
}
function getFactory(attr) {
return function () {
return this.hasOwnProperty(attr + "_")?this[attr + "_"]:this.xhr[attr];
}
}
function setFactory(attr) {
return function (f) {
var xhr = this.xhr;
var that = this;
if (attr.indexOf("on") != 0) {
this[attr + "_"] = f;
return;
}
if (funs[attr]) {
xhr[attr] = function () {
funs[attr](that) || f.apply(xhr, arguments);
}
} else {
xhr[attr] = f;
}
}
}
function hookfun(fun) {
return function () {
var args = [].slice.call(arguments)
if (funs[fun] && funs[fun].call(this, args, this.xhr)) {
return;
}
return this.xhr[fun].apply(this.xhr, args);
}
}
return window._ahrealxhr;
}
ob.unHookAjax = function () {
if (window._ahrealxhr) XMLHttpRequest = window._ahrealxhr;
window._ahrealxhr = undefined;
}
}(window);
脚本②——使用ajax hook
hookAjax({
onload:function(xhr) {
//console.log(xhr)
},
open:function(arg){
console.log(arg)
if(arg[0]=="PUT"||arg[0]=="DELETE"){
arg[0]="GET"
}
}
});
arg 是请求方法及url。初步修改直接改这个