aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/modify-dom.ts32
-rw-r--r--src/utils/observe-dom.ts4
2 files changed, 15 insertions, 21 deletions
diff --git a/src/utils/modify-dom.ts b/src/utils/modify-dom.ts
index da0944b..f08f7d9 100644
--- a/src/utils/modify-dom.ts
+++ b/src/utils/modify-dom.ts
@@ -1,41 +1,35 @@
import { observeDOM } from "@/utils/observe-dom";
-type Replacements = {
- [key: string]: string | null
+type DOMReplacements = {
+ innerHTML?: string;
+ [key: string]: string | undefined | null;
};
-export class DOMModifier {
- selector: string;
- attributes: Replacements;
- constructor(selector: string, attributes: Replacements) {
- this.selector = selector;
- this.attributes = attributes;
- }
-}
+type DOMModificaitonRule = readonly [string, DOMReplacements];
-export const modifyDOM = (elements: (DOMModifier | null)[]) => {
+export const modifyDOM = (modificationRules: (DOMModificaitonRule | null)[]) => {
let count = 0;
observeDOM(() => {
- for (let i = 0; i < elements.length; i++) {
- if (elements[i]) {
- const element = document.querySelector(elements[i]!.selector) as HTMLElement | null;
+ 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 = elements[i]!.attributes as any;
+ const attributes = modificationRules[i]![1] as DOMReplacements;
if (attributes.innerHTML) {
- element.innerHTML = 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]);
+ element.setAttribute(attribute, attributes[attribute] as string);
}
}
- elements[i] = null;
+ modificationRules[i] = null;
count++;
- if (count >= elements.length) {
+ if (count >= modificationRules.length) {
return true;
}
}
diff --git a/src/utils/observe-dom.ts b/src/utils/observe-dom.ts
index 8ea1a98..4a447a0 100644
--- a/src/utils/observe-dom.ts
+++ b/src/utils/observe-dom.ts
@@ -1,11 +1,11 @@
-export const observeDOM = (callback: () => boolean, element: HTMLElement = document as unknown as HTMLElement) => {
+export const observeDOM = (callback: () => boolean) => {
const observer = new MutationObserver(() => {
if (callback()) {
observer.disconnect();
}
});
- observer.observe(element, {
+ observer.observe(document, {
childList: true,
subtree: true
});