blob: 071daf518181dc74d1c054b5a6aab942bb96d63f [file] [log] [blame]
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +02001import {correctArrayKeys} from '../common/protojs';
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +02002import * as utils from '../common/xhrInterceptorUtils.js';
3
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +02004const originalOpen = window.XMLHttpRequest.prototype.open;
5const originalSetRequestHeader =
6 window.XMLHttpRequest.prototype.setRequestHeader;
7const originalSend = window.XMLHttpRequest.prototype.send;
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +02008
avm999631f50f6f2021-08-12 23:04:41 +02009let messageID = 0;
10
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +020011window.XMLHttpRequest.prototype.open = function() {
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020012 this.$TWPTRequestURL = arguments[1] || location.href;
avm999631f50f6f2021-08-12 23:04:41 +020013 this.$TWPTID = messageID++;
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020014
15 let interceptors = utils.matchInterceptors('response', this.$TWPTRequestURL);
16 if (interceptors.length > 0) {
17 this.addEventListener('load', function() {
18 var body = utils.getResponseJSON(this);
19 if (body !== undefined)
20 interceptors.forEach(i => {
avm999631f50f6f2021-08-12 23:04:41 +020021 utils.triggerEvent(i.eventName, body, this.$TWPTID);
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020022 });
23 });
24 }
25
26 originalOpen.apply(this, arguments);
27};
28
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +020029window.XMLHttpRequest.prototype.setRequestHeader = function() {
30 originalSetRequestHeader.apply(this, arguments);
31
32 let header = arguments[0];
33 let value = arguments[1];
34 if ('Content-Type'.localeCompare(
35 header, undefined, {sensitivity: 'accent'}) == 0)
36 this.$isArrayProto = (value == 'application/json+protobuf');
37};
38
39window.XMLHttpRequest.prototype.send = function() {
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020040 originalSend.apply(this, arguments);
41
42 let interceptors =
43 utils.matchInterceptors('request', this.$TWPTRequestURL || location.href);
44 if (interceptors.length > 0) {
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +020045 let rawBody = arguments[0];
46 let body;
47 if (typeof (rawBody) === 'object' &&
48 (rawBody instanceof Object.getPrototypeOf(Uint8Array))) {
49 let dec = new TextDecoder('utf-8');
50 body = dec.decode(rawBody);
51 } else if (typeof (rawBody) === 'string') {
52 body = rawBody;
53 } else {
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020054 console.error(
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +020055 'Unexpected type of request body (' + typeof (rawBody) + ').',
56 this.$TWPTRequestURL);
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020057 return;
58 }
59
Adrià Vilanova Martínez4e0cb182022-06-26 00:21:50 +020060 let JSONBody = JSON.parse(body);
61 if (this.$isArrayProto) JSONBody = correctArrayKeys(JSONBody);
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020062
63 interceptors.forEach(i => {
avm999631f50f6f2021-08-12 23:04:41 +020064 utils.triggerEvent(i.eventName, JSONBody, this.$TWPTID);
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020065 });
66 }
67};