-
Notifications
You must be signed in to change notification settings - Fork 253
/
main.js
166 lines (152 loc) · 5.25 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 命名空间
let ajax_interceptor_qoweifjqon = {
settings: {
ajaxInterceptor_switchOn: false,
ajaxInterceptor_rules: [],
},
originalXHR: window.XMLHttpRequest,
myXHR: function() {
let pageScriptEventDispatched = false;
const modifyResponse = () => {
ajax_interceptor_qoweifjqon.settings.ajaxInterceptor_rules.forEach(({filterType = 'normal', switchOn = true, match, overrideTxt = ''}) => {
let matched = false;
if (switchOn && match) {
if (filterType === 'normal' && this.responseURL.indexOf(match) > -1) {
matched = true;
} else if (filterType === 'regex' && this.responseURL.match(new RegExp(match, 'i'))) {
matched = true;
}
}
if (matched) {
this.responseText = overrideTxt;
this.response = overrideTxt;
if (!pageScriptEventDispatched) {
window.dispatchEvent(new CustomEvent("pageScript", {
detail: {url: this.responseURL, match}
}));
pageScriptEventDispatched = true;
}
}
})
}
const xhr = new ajax_interceptor_qoweifjqon.originalXHR;
for (let attr in xhr) {
if (attr === 'onreadystatechange') {
xhr.onreadystatechange = (...args) => {
if (this.readyState == 4) {
// 请求成功
if (ajax_interceptor_qoweifjqon.settings.ajaxInterceptor_switchOn) {
// 开启拦截
modifyResponse();
}
}
this.onreadystatechange && this.onreadystatechange.apply(this, args);
}
continue;
} else if (attr === 'onload') {
xhr.onload = (...args) => {
// 请求成功
if (ajax_interceptor_qoweifjqon.settings.ajaxInterceptor_switchOn) {
// 开启拦截
modifyResponse();
}
this.onload && this.onload.apply(this, args);
}
continue;
}
if (typeof xhr[attr] === 'function') {
this[attr] = xhr[attr].bind(xhr);
} else {
// responseText和response不是writeable的,但拦截时需要修改它,所以修改就存储在this[`_${attr}`]上
if (attr === 'responseText' || attr === 'response') {
Object.defineProperty(this, attr, {
get: () => this[`_${attr}`] == undefined ? xhr[attr] : this[`_${attr}`],
set: (val) => this[`_${attr}`] = val,
enumerable: true
});
} else {
Object.defineProperty(this, attr, {
get: () => xhr[attr],
set: (val) => xhr[attr] = val,
enumerable: true
});
}
}
}
},
originalFetch: window.fetch.bind(window),
myFetch: function(...args) {
return ajax_interceptor_qoweifjqon.originalFetch(...args).then((response) => {
let txt = undefined;
ajax_interceptor_qoweifjqon.settings.ajaxInterceptor_rules.forEach(({filterType = 'normal', switchOn = true, match, overrideTxt = ''}) => {
let matched = false;
if (switchOn && match) {
if (filterType === 'normal' && response.url.indexOf(match) > -1) {
matched = true;
} else if (filterType === 'regex' && response.url.match(new RegExp(match, 'i'))) {
matched = true;
}
}
if (matched) {
window.dispatchEvent(new CustomEvent("pageScript", {
detail: {url: response.url, match}
}));
txt = overrideTxt;
}
});
if (txt !== undefined) {
const stream = new ReadableStream({
start(controller) {
const bufView = new Uint8Array(new ArrayBuffer(txt.length));
for (var i = 0; i < txt.length; i++) {
bufView[i] = txt.charCodeAt(i);
}
controller.enqueue(bufView);
controller.close();
}
});
const newResponse = new Response(stream, {
headers: response.headers,
status: response.status,
statusText: response.statusText,
});
const proxy = new Proxy(newResponse, {
get: function(target, name){
switch(name) {
case 'ok':
case 'redirected':
case 'type':
case 'url':
case 'useFinalURL':
case 'body':
case 'bodyUsed':
return response[name];
}
return target[name];
}
});
for (let key in proxy) {
if (typeof proxy[key] === 'function') {
proxy[key] = proxy[key].bind(newResponse);
}
}
return proxy;
} else {
return response;
}
});
},
}
window.addEventListener("message", function(event) {
const data = event.data;
if (data.type === 'ajaxInterceptor' && data.to === 'pageScript') {
ajax_interceptor_qoweifjqon.settings[data.key] = data.value;
}
if (ajax_interceptor_qoweifjqon.settings.ajaxInterceptor_switchOn) {
window.XMLHttpRequest = ajax_interceptor_qoweifjqon.myXHR;
window.fetch = ajax_interceptor_qoweifjqon.myFetch;
} else {
window.XMLHttpRequest = ajax_interceptor_qoweifjqon.originalXHR;
window.fetch = ajax_interceptor_qoweifjqon.originalFetch;
}
}, false);