Skip to content

Commit cb73fcb

Browse files
committed
hook xhr - almost done
1 parent 3e9d5a8 commit cb73fcb

File tree

2 files changed

+29
-48
lines changed

2 files changed

+29
-48
lines changed

scripts/_test.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,15 @@ export default {
268268
// console.log("onBefore", url, options);
269269
// },
270270
// });
271-
// const unregister = hookXHR({
272-
// onBefore: (...args) => {
273-
// console.log("before", args);
274-
// },
275-
// onAfter: (...args) => {
276-
// console.log("-> after", args);
277-
// },
278-
// });
271+
const unregister = hookXHR({
272+
onBefore: (params) => {
273+
if (params.url.includes("graphql")) console.log("before", params);
274+
},
275+
onAfter: (params, response) => {
276+
if (params.url.includes("graphql"))
277+
console.log("-> after", params, JSON.parse(response));
278+
},
279+
});
279280
},
280281

281282
// download album

scripts/libs/ajax-hook/index.js

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -82,41 +82,24 @@ export const CANCEL_XHR = {
8282
};
8383

8484
function initXhr() {
85-
const originalXMLHttpRequest = window.XMLHttpRequest;
86-
window.XMLHttpRequest = new Proxy(originalXMLHttpRequest, {
87-
construct(target, args) {
88-
const instance = new target(...args);
89-
90-
const open = instance.open;
91-
instance.open = function (method, url, m, y, f) {
92-
this._method = method;
93-
this._url = url;
94-
return open.apply(this, arguments);
95-
};
96-
97-
const send = instance.send;
98-
instance.send = function (dataSend) {
99-
this.dataSend = dataSend;
100-
101-
for (let { fn } of onBeforeXHRFn)
102-
dataSend = fn(this._url, this._method, dataSend) || dataSend;
103-
104-
if (dataSend?.[cancelKey]) return null;
105-
106-
return send.apply(this, arguments);
107-
};
108-
109-
// TODO verify
110-
instance.onreadystatechange = function () {
111-
if (this.readyState === 4 && this.status === 200) {
112-
for (let { fn } of onAfterXHRFn)
113-
fn(this._url, this._method, this.dataSend, this.responseText);
114-
}
115-
};
116-
117-
return instance;
118-
},
119-
});
85+
let oldXHROpen = window.XMLHttpRequest.prototype.open;
86+
window.XMLHttpRequest.prototype.open = function (
87+
method,
88+
url,
89+
async,
90+
user,
91+
password
92+
) {
93+
let params = { method, url, async, user, password };
94+
for (let { fn } of onBeforeXHRFn) params = fn(params) || params;
95+
if (params?.[cancelKey]) return;
96+
97+
this.addEventListener("load", function () {
98+
for (let { fn } of onAfterXHRFn) fn(params, this.responseText);
99+
});
100+
101+
return oldXHROpen.apply(this, Object.values(params));
102+
};
120103
}
121104

122105
export function hookXHR({ onBefore, onAfter } = {}) {
@@ -134,14 +117,11 @@ export function hookXHR({ onBefore, onAfter } = {}) {
134117

135118
/* hookXHR example
136119
hookXHR({
137-
onBefore: (url, method, dataSend) => {
138-
console.log({ url, method, dataSend });
139-
return dataSend;
120+
onBefore: ({ method, url, async, user, password }) => {
140121
// return CANCEL_XHR;
141122
},
142-
onAfter: (url, method, dataSend, response) => {
123+
onAfter: ({ method, url, async, user, password }, responseText) => {
143124
console.log({ url, method, dataSend, response });
144-
return res;
145125
},
146126
});
147127
*/

0 commit comments

Comments
 (0)