-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (55 loc) · 1.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(function(window){
window.oldXMLHttpRequest = window.oldXMLHttpRequest || window.XMLHttpRequest;
let isHook = false;
function MyXMLHttpRequest () {
const allowableMethods = ['GET', 'POST'];
this.xhr = new window.oldXMLHttpRequest();
for (let attr in this.xhr) {
let type = '';
try {
type = typeof this.xhr[attr]
} catch (e) {}
if (type === 'function') {
this[attr] = function() {
let args = [].slice.call(arguments)
return this.xhr[attr].apply(this.xhr, args);
}
} else if(attr !== 'open') {
Object.defineProperty(this, attr, {
get () {
return this.xhr[attr];
},
set (newVal) {
this.xhr[attr] = newVal;
}
})
}
}
this.open = (method, url, async, username, password) => {
if(!allowableMethods.includes(method) && isHook){
if (url.includes('?')){
url += '&_method=' + method;
} else {
if (url[url.length - 1] === '/'){
url = url.substr(0, url.length - 1);
}
url += '?_method=' + method;
}
return this.xhr.open('POST', url, async, username, password);
}
return this.xhr.open(method, url, async, username, password);
}
}
const onlyGetPost = {
hook () {
isHook = true;
window.XMLHttpRequest = MyXMLHttpRequest;
},
unHook () {
isHook = false;
window.XMLHttpRequest = window.oldXMLHttpRequest;
}
}
window.onlyGetPost = onlyGetPost;
})(window);
export default window.onlyGetPost;