Flatten thread: fix behavior after replying

After replying to a thread, the new reply wasn't visible. This was due
to the fact that when the Community Console loaded the thread again, it
used the XHR property `responseText` to read the response instead of
`response`, but until now only `response` returned the modified response
instead of the original one.

Thus, this CL adds logic to also return the modified response (in text
form) when calling `responseText`.

Bug: twpowertools:156
Change-Id: If30112ca2827749a5d6677bfb9dfc790dabd9281
diff --git a/src/xhrInterceptor/XHRProxy.js b/src/xhrInterceptor/XHRProxy.js
index 288142a..2d6d03f 100644
--- a/src/xhrInterceptor/XHRProxy.js
+++ b/src/xhrInterceptor/XHRProxy.js
@@ -166,7 +166,6 @@
       'onprogress',
       'onreadystatechange',
       'readyState',
-      'responseText',
       'responseType',
       'responseXML',
       'status',
@@ -202,6 +201,13 @@
         return this.xhr.response;
       },
     });
+    Object.defineProperty(window.XMLHttpRequest.prototype, 'responseText', {
+      get: function() {
+        if (!this.$responseIntercepted) return undefined;
+        if (this.$responseModified) return this.$newResponseText;
+        return this.xhr.responseText;
+      },
+    });
 
     return this;
   }
diff --git a/src/xhrInterceptor/responseModifiers/index.js b/src/xhrInterceptor/responseModifiers/index.js
index 40494eb..0b349bd 100644
--- a/src/xhrInterceptor/responseModifiers/index.js
+++ b/src/xhrInterceptor/responseModifiers/index.js
@@ -1,9 +1,9 @@
 import MWOptionsWatcherClient from '../../common/mainWorldOptionsWatcher/Client.js';
-import {convertJSONToResponse, getResponseJSON} from '../utils.js';
+import {convertJSONToResponse, convertJSONToResponseText, getResponseJSON} from '../utils.js';
 
-import loadMoreThread from './loadMoreThread.js';
-import flattenThread from './flattenThread.js';
 import createMessageRemoveParentRef from './createMessageRemoveParentRef.js';
+import flattenThread from './flattenThread.js';
+import loadMoreThread from './loadMoreThread.js';
 
 export const responseModifiers = [
   loadMoreThread,
@@ -72,8 +72,8 @@
     for (const modifier of matchingModifiers) {
       json = await modifier.interceptor(request, json);
     }
-    const response = convertJSONToResponse(request, json);
-    request.$newResponse = response;
+    request.$newResponse = convertJSONToResponse(request, json);
+    request.$newResponseText = convertJSONToResponseText(request, json);
     request.$responseModified = true;
   }
 }
diff --git a/src/xhrInterceptor/utils.js b/src/xhrInterceptor/utils.js
index 849c543..d48dfd5 100644
--- a/src/xhrInterceptor/utils.js
+++ b/src/xhrInterceptor/utils.js
@@ -78,7 +78,7 @@
       return JSON.stringify(json);
 
     case 'arraybuffer':
-      const encoder = new TextEncoder();
+      var encoder = new TextEncoder();
       return encoder.encode(JSON.stringify(json)).buffer;
 
     default:
@@ -89,6 +89,13 @@
   }
 }
 
+export function convertJSONToResponseText(xhr, json) {
+  return convertJSONToResponse({
+    $isArrayProto: xhr.$isArrayProto,
+    responseType: 'text',
+  }, json);
+}
+
 export function triggerEvent(eventName, body, id) {
   var evt = new CustomEvent('TWPT_' + eventName, {
     detail: {