aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/modify-dom.ts41
-rw-r--r--src/shared/observe-dom.ts12
-rw-r--r--src/shared/styling.ts8
3 files changed, 61 insertions, 0 deletions
diff --git a/src/shared/modify-dom.ts b/src/shared/modify-dom.ts
new file mode 100644
index 0000000..0832d5b
--- /dev/null
+++ b/src/shared/modify-dom.ts
@@ -0,0 +1,41 @@
+import { observeDOM } from "/shared/observe-dom";
+
+type DOMReplacements = {
+ innerHTML?: string;
+ [key: string]: string | undefined | null;
+};
+
+type DOMModificaitonRule = readonly [string, DOMReplacements];
+
+export const modifyDOM = (modificationRules: (DOMModificaitonRule | null)[]) => {
+ let count = 0;
+
+ observeDOM(() => {
+ for (let i = 0; i < modificationRules.length; i++) {
+ if (modificationRules[i]) {
+ const element = document.querySelector(modificationRules[i]![0]) as HTMLElement | null;
+ if (element) {
+ const attributes = modificationRules[i]![1] as DOMReplacements;
+ if (attributes.innerHTML) {
+ element.innerHTML = attributes.innerHTML as string;
+ }
+ delete attributes.innerHTML;
+ for (const attribute in attributes) {
+ if (attributes[attribute] === null) {
+ element.removeAttribute(attribute);
+ } else {
+ element.setAttribute(attribute, attributes[attribute] as string);
+ }
+ }
+ modificationRules[i] = null;
+ count++;
+ if (count >= modificationRules.length) {
+ return true;
+ }
+ }
+ }
+ }
+
+ return false;
+ });
+};
diff --git a/src/shared/observe-dom.ts b/src/shared/observe-dom.ts
new file mode 100644
index 0000000..4a447a0
--- /dev/null
+++ b/src/shared/observe-dom.ts
@@ -0,0 +1,12 @@
+export const observeDOM = (callback: () => boolean) => {
+ const observer = new MutationObserver(() => {
+ if (callback()) {
+ observer.disconnect();
+ }
+ });
+
+ observer.observe(document, {
+ childList: true,
+ subtree: true
+ });
+};
diff --git a/src/shared/styling.ts b/src/shared/styling.ts
new file mode 100644
index 0000000..0e527d9
--- /dev/null
+++ b/src/shared/styling.ts
@@ -0,0 +1,8 @@
+export const styles = (selector: string, styles: { [key: string]: string }) => {
+ const elements = Array.from(document.querySelectorAll(selector)) as HTMLElement[];
+ for (const element of elements) {
+ for (const styleType in styles) {
+ element.style[styleType as any] = styles[styleType];
+ }
+ }
+};