-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinjected.js
38 lines (31 loc) · 1.24 KB
/
injected.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
(function (xhr) {
var XHR = XMLHttpRequest.prototype;
var open = XHR.open;
var send = XHR.send;
XHR.open = function (method, url) {
this._method = method;
this._url = url;
return open.apply(this, arguments);
};
XHR.send = function (postData) {
console.log('injected script xhr request:', this._method, this._url, this.getAllResponseHeaders(), postData);
this.addEventListener('load', function () {
window.postMessage({ type: 'xhr', data: this.response }, '*'); // send to content script
});
return send.apply(this, arguments);
};
})(XMLHttpRequest);
const { fetch: origFetch } = window;
window.fetch = async (...args) => {
const response = await origFetch(...args);
console.log('injected script fetch request:', args);
response
.clone()
.blob() // maybe json(), text(), blob()
.then(data => {
window.postMessage({ type: 'fetch', data: data }, '*'); // send to content script
//window.postMessage({ type: 'fetch', data: URL.createObjectURL(data) }, '*'); // if a big media file, can createObjectURL before send to content script
})
.catch(err => console.error(err));
return response;
};