Add XHR proxy kill switch
Since the XHRProxy loads very early to be able to catch all requests,
we cache the kill switch value in the localStorage and this cached
value is consulted early in the page load to determine whether the
XHR proxy should be put in place.
Thus, this kill switch only takes effect after a page reload.
Fixed: twpowertools:162
Change-Id: I43a163c506657d5ba9fb4ec268b2d4409d6401e3
diff --git a/src/common/optionsPrototype.json5 b/src/common/optionsPrototype.json5
index 1b114a4..eb7d892 100644
--- a/src/common/optionsPrototype.json5
+++ b/src/common/optionsPrototype.json5
@@ -174,6 +174,12 @@
killSwitchType: 'ignore',
},
+ // Internal kill switches:
+ 'killswitch_xhrproxy': {
+ context: 'internal',
+ killSwitchType: 'internalKillSwitch',
+ },
+
// Deprecated options:
'escalatethreads': {
defaultValue: false,
diff --git a/src/contentScripts/communityConsole/main.js b/src/contentScripts/communityConsole/main.js
index 0e53917..b16b42f 100644
--- a/src/contentScripts/communityConsole/main.js
+++ b/src/contentScripts/communityConsole/main.js
@@ -1,5 +1,6 @@
import {injectScript, injectStyles, injectStylesheet} from '../../common/contentScriptsUtils.js';
import {getOptions} from '../../common/optionsUtils.js';
+import XHRProxyKillSwitchHandler from '../../xhrInterceptor/killSwitchHandler.js';
import {injectPreviousPostsLinksUnifiedProfileIfEnabled} from '../utilsCommon/unifiedProfiles.js';
import AvatarsHandler from './avatars.js';
@@ -346,3 +347,5 @@
// Flatten threads
injectStylesheet(chrome.runtime.getURL('css/flatten_threads.css'));
});
+
+new XHRProxyKillSwitchHandler();
diff --git a/src/injections/xhrProxy.js b/src/injections/xhrProxy.js
index d33521c..2799bd2 100644
--- a/src/injections/xhrProxy.js
+++ b/src/injections/xhrProxy.js
@@ -1,3 +1,7 @@
+import {KILL_SWITCH_LOCALSTORAGE_KEY, KILL_SWITCH_LOCALSTORAGE_VALUE} from '../xhrInterceptor/killSwitchHandler.js';
import XHRProxy from '../xhrInterceptor/XHRProxy.js';
-new XHRProxy();
+if (window.localStorage.getItem(KILL_SWITCH_LOCALSTORAGE_KEY) !==
+ KILL_SWITCH_LOCALSTORAGE_VALUE) {
+ new XHRProxy();
+}
diff --git a/src/xhrInterceptor/killSwitchHandler.js b/src/xhrInterceptor/killSwitchHandler.js
new file mode 100644
index 0000000..02df59b
--- /dev/null
+++ b/src/xhrInterceptor/killSwitchHandler.js
@@ -0,0 +1,21 @@
+import InternalKillSwitchWatcher from '../killSwitch/internalKillSwitchWatcher.js';
+
+export const KILL_SWITCH = 'killswitch_xhrproxy';
+export const KILL_SWITCH_LOCALSTORAGE_KEY = 'TWPTKillSwitchXHRProxyEnabled';
+export const KILL_SWITCH_LOCALSTORAGE_VALUE = 'true';
+
+export default class XHRProxyKillSwitchHandler {
+ constructor() {
+ this.watcher =
+ new InternalKillSwitchWatcher(KILL_SWITCH, this.onChange, true);
+ }
+
+ onChange(isActive) {
+ if (isActive) {
+ window.localStorage.setItem(
+ KILL_SWITCH_LOCALSTORAGE_KEY, KILL_SWITCH_LOCALSTORAGE_VALUE);
+ } else {
+ window.localStorage.removeItem(KILL_SWITCH_LOCALSTORAGE_KEY);
+ }
+ }
+}