-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookRequest.js
More file actions
134 lines (130 loc) · 5.15 KB
/
HookRequest.js
File metadata and controls
134 lines (130 loc) · 5.15 KB
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
(() => {
var contextWindow = window.unsafeWindow || document.defaultView || window;
if (contextWindow['__hookRequest__'] != null) {
return;
}
var globalVariable = new Map();
var FetchMapList = new Map();
function deliveryTask(callbackList, _object, period) {
let newObject = _object;
for (let i = 0; i < callbackList.length; i++) {
let tempObject = null;
try {
tempObject = callbackList[i](newObject, period);
} catch (e) {
new Error(e);
}
if (tempObject == null) {
continue;
}
newObject = tempObject;
}
return newObject;
}
function hookFetch() {
const originalFetch = contextWindow.fetch;
globalVariable.set('Fetch', originalFetch);
contextWindow.fetch = (...args) => {
let U = args[0];
if (U.indexOf('http') == -1) {
if (U[0] !== '/') {
let pathname = new URL(location.href).pathname;
U = pathname + U;
}
U = location.origin + U;
}
let apply = null;
(() => {
let url = new URL(U),
pathname = url.pathname,
callback = FetchMapList.get(pathname);
if (callback == null) return;
if (callback.length === 0) return;
let newObject = deliveryTask(callback, { args }, 'preRequest');
if (newObject && newObject.args) {
args = newObject.args;
}
apply = originalFetch.apply(this, args);
apply.then((response) => {
let originalGetReader = response.body.getReader;
response.body.getReader = function () {
let originalReader = originalGetReader.apply(this, arguments);
let originalRead = originalReader.read;
originalReader.read = function () {
return originalRead.apply(this, arguments).then(function (result) {
let tempObject = deliveryTask(callback, { result, args }, 'doing');
if (tempObject && tempObject.result) {
result = tempObject.result;
}
return result;
});
};
return originalReader;
};
let text = response.text,
json = response.json;
response.text = () => {
return text.apply(response).then((text) => {
let _object = deliveryTask(callback, { text, args }, 'done');
if (_object && _object.text) {
text = _object.text;
}
return text;
});
};
response.json = () => {
return json.apply(response).then((json) => {
let text = JSON.stringify(json);
let _object = deliveryTask(callback, { text, args }, 'done');
if (_object && _object.text) {
text = _object.text;
return JSON.parse(text);
}
return json;
});
};
});
})();
if (apply == null) {
apply = originalFetch.apply(this, args);
}
return apply;
};
}
hookFetch();
contextWindow['__hookRequest__'] = {
FetchCallback: {
add: (pathname, callback) => {
let list = FetchMapList.get(pathname) || (FetchMapList.set(pathname, []), FetchMapList.get(pathname));
list.push(callback);
let index = list.length;
return index;
},
del: (pathname, index) => {
try {
let list = FetchMapList.get(pathname);
if (list == null) return false;
list.splice(index - 1, 1);
} catch (e) {
new Error(e);
return false;
}
return true;
}
},
globalVariable: {
get: (key) => {
return globalVariable.get(key);
},
getAll: () => {
return globalVariable.entries();
},
set: (key, value) => {
globalVariable.set(key, value);
},
getOrDrfault: (key, defaultValue) => {
return globalVariable.get(key) || defaultValue;
}
}
};
})();