refactor: migrate standalone scripts to the new DI architecture

Bug: twpowertools:226
Change-Id: Ie32b0200a31626bde06f3bdb0655790bb5a76802
diff --git a/src/presentation/standaloneScripts/Scripts.ts b/src/presentation/standaloneScripts/Scripts.ts
new file mode 100644
index 0000000..7f3b602
--- /dev/null
+++ b/src/presentation/standaloneScripts/Scripts.ts
@@ -0,0 +1,16 @@
+import Script, {
+  ConcreteScript,
+} from '../../common/architecture/scripts/Script';
+import ScriptFilterListProvider from '../../common/architecture/scripts/ScriptFilterListProvider';
+
+export default class StandaloneScripts extends ScriptFilterListProvider {
+  private scripts: ConcreteScript[] = [];
+  private initializedScripts: Script[];
+
+  protected getUnfilteredScriptsList() {
+    if (this.initializedScripts === undefined) {
+      this.initializedScripts = this.scripts.map((script) => new script());
+    }
+    return this.initializedScripts;
+  }
+}
diff --git a/src/presentation/standaloneScripts/litComponents/injectLitComponents.script.ts b/src/presentation/standaloneScripts/litComponents/injectLitComponents.script.ts
new file mode 100644
index 0000000..a34c8b9
--- /dev/null
+++ b/src/presentation/standaloneScripts/litComponents/injectLitComponents.script.ts
@@ -0,0 +1,12 @@
+import Script, { ScriptEnvironment, ScriptPage, ScriptRunPhase } from "../../../common/architecture/scripts/Script"
+import { injectScript } from "../../../common/contentScriptsUtils";
+
+export default class InjectLitComponentsScript extends Script {
+  page = ScriptPage.CommunityConsole;
+  environment = ScriptEnvironment.ContentScript;
+  runPhase = ScriptRunPhase.Main;
+
+  execute() {
+    injectScript(chrome.runtime.getURL('litComponentsInject.bundle.js'));
+  }
+}
diff --git a/src/presentation/standaloneScripts/mainWorldServers/MWI18nServerScript.script.ts b/src/presentation/standaloneScripts/mainWorldServers/MWI18nServerScript.script.ts
new file mode 100644
index 0000000..86d28fa
--- /dev/null
+++ b/src/presentation/standaloneScripts/mainWorldServers/MWI18nServerScript.script.ts
@@ -0,0 +1,17 @@
+import Script, { ScriptEnvironment, ScriptPage, ScriptRunPhase } from "../../../common/architecture/scripts/Script"
+import MWI18nServer from "../../../common/mainWorldI18n/Server";
+
+export default class MWI18nServerScript extends Script {
+  // The server should be available as soon as possible, since e.g. the
+  // XHRProxy already sends a request to the optionsWatcher server as soon as it
+  // is constructed.
+  priority = 1;
+
+  page = ScriptPage.CommunityConsole;
+  environment = ScriptEnvironment.ContentScript;
+  runPhase = ScriptRunPhase.Start;
+
+  execute() {
+    new MWI18nServer();
+  }
+}
diff --git a/src/presentation/standaloneScripts/mainWorldServers/MWOptionsWatcherServerScript.script.ts b/src/presentation/standaloneScripts/mainWorldServers/MWOptionsWatcherServerScript.script.ts
new file mode 100644
index 0000000..464a7fa
--- /dev/null
+++ b/src/presentation/standaloneScripts/mainWorldServers/MWOptionsWatcherServerScript.script.ts
@@ -0,0 +1,18 @@
+import Script, { ScriptEnvironment, ScriptPage, ScriptRunPhase } from "../../../common/architecture/scripts/Script"
+import MWOptionsWatcherServer from "../../../common/mainWorldOptionsWatcher/Server"
+import { kCSTarget, kMWTarget } from "../../../xhrInterceptor/responseModifiers"
+
+export default class MWOptionsWatcherServerScript extends Script {
+  // The server should be available as soon as possible, since e.g. the
+  // XHRProxy already sends a request to the optionsWatcher server as soon as it
+  // is constructed.
+  priority = 0;
+
+  page = ScriptPage.CommunityConsole;
+  environment = ScriptEnvironment.ContentScript;
+  runPhase = ScriptRunPhase.Start;
+
+  execute() {
+    new MWOptionsWatcherServer(kCSTarget, kMWTarget);
+  }
+}
diff --git a/src/presentation/standaloneScripts/startupDataStorage/applyStartupDataModificationsOnMain.script.ts b/src/presentation/standaloneScripts/startupDataStorage/applyStartupDataModificationsOnMain.script.ts
new file mode 100644
index 0000000..de58a11
--- /dev/null
+++ b/src/presentation/standaloneScripts/startupDataStorage/applyStartupDataModificationsOnMain.script.ts
@@ -0,0 +1,15 @@
+import { ScriptRunPhase } from '../../../common/architecture/scripts/Script';
+import { StartupDataStoragePort } from '../../../services/communityConsole/StartupDataStorage.port';
+import BaseApplyStartupDataModificationsScript from './baseApplyStartupDataModifications.script';
+
+/**
+ * Applies pending startup data modifications which have been added by other
+ * scripts at the main run phase.
+ */
+export default class ApplyStartupDataModificationsOnMainScript extends BaseApplyStartupDataModificationsScript {
+  runPhase = ScriptRunPhase.Main;
+
+  constructor(startupDataStorage: StartupDataStoragePort) {
+    super(startupDataStorage);
+  }
+}
diff --git a/src/presentation/standaloneScripts/startupDataStorage/applyStartupDataModificationsOnStart.script.ts b/src/presentation/standaloneScripts/startupDataStorage/applyStartupDataModificationsOnStart.script.ts
new file mode 100644
index 0000000..473f29c
--- /dev/null
+++ b/src/presentation/standaloneScripts/startupDataStorage/applyStartupDataModificationsOnStart.script.ts
@@ -0,0 +1,15 @@
+import { ScriptRunPhase } from '../../../common/architecture/scripts/Script';
+import { StartupDataStoragePort } from '../../../services/communityConsole/StartupDataStorage.port';
+import BaseApplyStartupDataModificationsScript from './baseApplyStartupDataModifications.script';
+
+/**
+ * Applies pending startup data modifications which have been added by other
+ * scripts at the start run phase.
+ */
+export default class ApplyStartupDataModificationsOnStartScript extends BaseApplyStartupDataModificationsScript {
+  runPhase = ScriptRunPhase.Start;
+
+  constructor(startupDataStorage: StartupDataStoragePort) {
+    super(startupDataStorage);
+  }
+}
diff --git a/src/presentation/standaloneScripts/startupDataStorage/baseApplyStartupDataModifications.script.ts b/src/presentation/standaloneScripts/startupDataStorage/baseApplyStartupDataModifications.script.ts
new file mode 100644
index 0000000..8244382
--- /dev/null
+++ b/src/presentation/standaloneScripts/startupDataStorage/baseApplyStartupDataModifications.script.ts
@@ -0,0 +1,24 @@
+import Script, {
+  ScriptEnvironment,
+  ScriptPage,
+} from '../../../common/architecture/scripts/Script';
+import { StartupDataStoragePort } from '../../../services/communityConsole/StartupDataStorage.port';
+
+/**
+ * Base class which applies pending startup data modifications which have been
+ * added by other scripts.
+ */
+export default abstract class BaseApplyStartupDataModificationsScript extends Script {
+  priority = 2 ** 32;
+
+  page = ScriptPage.CommunityConsole;
+  environment = ScriptEnvironment.ContentScript;
+
+  constructor(private startupDataStorage: StartupDataStoragePort) {
+    super();
+  }
+
+  execute() {
+    this.startupDataStorage.applyModifications();
+  }
+}
diff --git a/src/presentation/standaloneScripts/xhrInterceptor/xhrInterceptor.script.ts b/src/presentation/standaloneScripts/xhrInterceptor/xhrInterceptor.script.ts
new file mode 100644
index 0000000..d2b146f
--- /dev/null
+++ b/src/presentation/standaloneScripts/xhrInterceptor/xhrInterceptor.script.ts
@@ -0,0 +1,21 @@
+import Script, {
+  ScriptEnvironment,
+  ScriptPage,
+  ScriptRunPhase,
+} from '../../../common/architecture/scripts/Script';
+import { injectScript } from '../../../common/contentScriptsUtils';
+
+export default class XHRInterceptorScript extends Script {
+  priority = 10;
+
+  page = ScriptPage.CommunityConsole;
+  environment = ScriptEnvironment.ContentScript;
+  runPhase = ScriptRunPhase.Start;
+
+  execute() {
+    injectScript(
+      chrome.runtime.getURL('xhrInterceptorInject.bundle.js'),
+      /* prepend = */ true,
+    );
+  }
+}