Adrià Vilanova Martínez | 4e0cb18 | 2022-06-26 00:21:50 +0200 | [diff] [blame] | 1 | import {correctArrayKeys} from '../common/protojs'; |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 2 | import * as utils from '../common/xhrInterceptorUtils.js'; |
| 3 | |
Adrià Vilanova Martínez | 4e0cb18 | 2022-06-26 00:21:50 +0200 | [diff] [blame] | 4 | const originalOpen = window.XMLHttpRequest.prototype.open; |
| 5 | const originalSetRequestHeader = |
| 6 | window.XMLHttpRequest.prototype.setRequestHeader; |
| 7 | const originalSend = window.XMLHttpRequest.prototype.send; |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 8 | |
avm99963 | 1f50f6f | 2021-08-12 23:04:41 +0200 | [diff] [blame] | 9 | let messageID = 0; |
| 10 | |
Adrià Vilanova Martínez | 4e0cb18 | 2022-06-26 00:21:50 +0200 | [diff] [blame] | 11 | window.XMLHttpRequest.prototype.open = function() { |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 12 | this.$TWPTRequestURL = arguments[1] || location.href; |
avm99963 | 1f50f6f | 2021-08-12 23:04:41 +0200 | [diff] [blame] | 13 | this.$TWPTID = messageID++; |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 14 | |
| 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 => { |
avm99963 | 1f50f6f | 2021-08-12 23:04:41 +0200 | [diff] [blame] | 21 | utils.triggerEvent(i.eventName, body, this.$TWPTID); |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 22 | }); |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | originalOpen.apply(this, arguments); |
| 27 | }; |
| 28 | |
Adrià Vilanova Martínez | 4e0cb18 | 2022-06-26 00:21:50 +0200 | [diff] [blame] | 29 | window.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 | |
| 39 | window.XMLHttpRequest.prototype.send = function() { |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 40 | originalSend.apply(this, arguments); |
| 41 | |
| 42 | let interceptors = |
| 43 | utils.matchInterceptors('request', this.$TWPTRequestURL || location.href); |
| 44 | if (interceptors.length > 0) { |
Adrià Vilanova Martínez | 4e0cb18 | 2022-06-26 00:21:50 +0200 | [diff] [blame] | 45 | 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ínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 54 | console.error( |
Adrià Vilanova Martínez | 4e0cb18 | 2022-06-26 00:21:50 +0200 | [diff] [blame] | 55 | 'Unexpected type of request body (' + typeof (rawBody) + ').', |
| 56 | this.$TWPTRequestURL); |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
Adrià Vilanova Martínez | 4e0cb18 | 2022-06-26 00:21:50 +0200 | [diff] [blame] | 60 | let JSONBody = JSON.parse(body); |
| 61 | if (this.$isArrayProto) JSONBody = correctArrayKeys(JSONBody); |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 62 | |
| 63 | interceptors.forEach(i => { |
avm99963 | 1f50f6f | 2021-08-12 23:04:41 +0200 | [diff] [blame] | 64 | utils.triggerEvent(i.eventName, JSONBody, this.$TWPTID); |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 65 | }); |
| 66 | } |
| 67 | }; |