aboutsummaryrefslogtreecommitdiff
path: root/src/content
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2021-03-17 22:50:23 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2021-03-21 12:24:36 +0900
commit5382f2353e693bc8e2682eb045e8bb9930829adc (patch)
tree556171409a854a39b616230496773a98b5b58f63 /src/content
parentd8bbad8e1b774d6e46c7fa05324c90ce2b716a66 (diff)
Load content scripts on document.body is ready
Diffstat (limited to 'src/content')
-rw-r--r--src/content/Bootstrap.ts27
-rw-r--r--src/content/index.ts34
2 files changed, 49 insertions, 12 deletions
diff --git a/src/content/Bootstrap.ts b/src/content/Bootstrap.ts
new file mode 100644
index 0000000..62e1530
--- /dev/null
+++ b/src/content/Bootstrap.ts
@@ -0,0 +1,27 @@
+type Callback = () => void;
+
+export default class Bootstrap {
+ constructor() {}
+
+ isReady(): boolean {
+ return document.body !== null;
+ }
+
+ waitForReady(callback: Callback): void {
+ const observer = new MutationObserver(() => {
+ if (document.body != null) {
+ observer.disconnect();
+ callback();
+ }
+ });
+
+ observer.observe(document, {
+ attributes: false,
+ attributeOldValue: false,
+ characterData: false,
+ characterDataOldValue: false,
+ childList: true,
+ subtree: true,
+ });
+ }
+}
diff --git a/src/content/index.ts b/src/content/index.ts
index b575e0d..5252338 100644
--- a/src/content/index.ts
+++ b/src/content/index.ts
@@ -1,22 +1,32 @@
import "reflect-metadata";
import Application from "./Application";
+import Bootstrap from "./Bootstrap";
import consoleFrameStyle from "./site-style";
import { ConsoleFramePresenterImpl } from "./presenters/ConsoleFramePresenter";
import { container } from "tsyringe";
import "./di";
-if (window.self === window.top) {
- new ConsoleFramePresenterImpl().initialize();
-}
+const initDom = () => {
+ if (window.self === window.top) {
+ new ConsoleFramePresenterImpl().initialize();
+ }
-try {
- const app = container.resolve(Application);
- app.run();
-} catch (e) {
- console.error(e);
-}
+ try {
+ const app = container.resolve(Application);
+ app.run();
+ } catch (e) {
+ console.error(e);
+ }
-const style = window.document.createElement("style");
-style.textContent = consoleFrameStyle;
-window.document.head.appendChild(style);
+ const style = window.document.createElement("style");
+ style.textContent = consoleFrameStyle;
+ window.document.head.appendChild(style);
+};
+
+const bootstrap = new Bootstrap();
+if (bootstrap.isReady()) {
+ initDom();
+} else {
+ bootstrap.waitForReady(() => initDom());
+}