Refactor XHR interceptor
In preparation for future work on the XHR interceptor.
Bug: twpowertools:153
Change-Id: Id8df1486c033ba02429a17d161e2bcc87a0f1de5
diff --git a/src/common/contentScriptsUtils.js b/src/common/contentScriptsUtils.js
index cb7a67d..51257c0 100644
--- a/src/common/contentScriptsUtils.js
+++ b/src/common/contentScriptsUtils.js
@@ -10,8 +10,12 @@
injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css));
}
-export function injectScript(scriptName) {
+export function injectScript(scriptName, prepend = false) {
var script = document.createElement('script');
script.src = scriptName;
- (document.head || document.documentElement).append(script);
+ const root = (document.head || document.documentElement);
+ if (prepend)
+ root.prepend(script);
+ else
+ root.append(script);
}
diff --git a/src/common/xhrInterceptorUtils.js b/src/common/xhrInterceptorUtils.js
deleted file mode 100644
index 526296c..0000000
--- a/src/common/xhrInterceptorUtils.js
+++ /dev/null
@@ -1,49 +0,0 @@
-import {correctArrayKeys} from '../common/protojs';
-
-import xhrInterceptors from './xhrInterceptors.json5';
-
-export {xhrInterceptors};
-
-export function matchInterceptors(interceptFilter, url) {
- return xhrInterceptors.interceptors.filter(interceptor => {
- var regex = new RegExp(interceptor.urlRegex);
- return interceptor.intercepts == interceptFilter && regex.test(url);
- });
-}
-
-export function getResponseJSON(xhr) {
- let response;
- if (xhr.responseType === 'arraybuffer') {
- var arrBuffer = xhr.response;
- if (!arrBuffer) {
- console.error('No array buffer.');
- return undefined;
- }
- let byteArray = new Uint8Array(arrBuffer);
- let dec = new TextDecoder('utf-8');
- let rawResponse = dec.decode(byteArray);
- response = JSON.parse(rawResponse);
- } else if (xhr.responseType === 'text' || xhr.responseType === '') {
- response = JSON.parse(xhr.responseText);
- } else if (xhr.responseType === 'json') {
- response = xhr.response;
- } else {
- console.error(
- 'Unexpected responseType ' + xhr.responseType + '. Request url: ',
- xhr.$TWPTRequestURL);
- return undefined;
- }
-
- if (xhr.$isArrayProto) response = correctArrayKeys(response);
- return response;
-}
-
-export function triggerEvent(eventName, body, id) {
- var evt = new CustomEvent('TWPT_' + eventName, {
- detail: {
- body,
- id,
- }
- });
- window.dispatchEvent(evt);
-}
diff --git a/src/common/xhrInterceptors.json5 b/src/common/xhrInterceptors.json5
deleted file mode 100644
index 2a0ea6b..0000000
--- a/src/common/xhrInterceptors.json5
+++ /dev/null
@@ -1,34 +0,0 @@
-{
- interceptors: [
- {
- eventName: "ViewForumRequest",
- urlRegex: "api/ViewForum",
- intercepts: "request",
- },
- {
- eventName: "ViewForumResponse",
- urlRegex: "api/ViewForum",
- intercepts: "response",
- },
- {
- eventName: "CreateMessageRequest",
- urlRegex: "api/CreateMessage",
- intercepts: "request",
- },
- {
- eventName: "ViewUnifiedUserResponse",
- urlRegex: "api/ViewUnifiedUser",
- intercepts: "response",
- },
- {
- eventName: "ListCannedResponsesResponse",
- urlRegex: "api/ListCannedResponses",
- intercepts: "response",
- },
- {
- eventName: "ViewThreadResponse",
- urlRegex: "api/ViewThread",
- intercepts: "response",
- },
- ],
-}