diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/modify-dom.ts | 19 | ||||
| -rw-r--r-- | src/utils/styling.ts | 8 |
2 files changed, 12 insertions, 15 deletions
diff --git a/src/utils/modify-dom.ts b/src/utils/modify-dom.ts index 4e36008..da0944b 100644 --- a/src/utils/modify-dom.ts +++ b/src/utils/modify-dom.ts @@ -1,20 +1,15 @@ import { observeDOM } from "@/utils/observe-dom"; type Replacements = { - innerHTML?: string; - styles?: { - [key: string]: string - }; -} | { [key: string]: string | null }; export class DOMModifier { selector: string; - replaced: Replacements; - constructor(selector: string, replaced: Replacements) { + attributes: Replacements; + constructor(selector: string, attributes: Replacements) { this.selector = selector; - this.replaced = replaced; + this.attributes = attributes; } } @@ -26,17 +21,11 @@ export const modifyDOM = (elements: (DOMModifier | null)[]) => { if (elements[i]) { const element = document.querySelector(elements[i]!.selector) as HTMLElement | null; if (element) { - const attributes = elements[i]!.replaced as any; + const attributes = elements[i]!.attributes as any; if (attributes.innerHTML) { element.innerHTML = attributes.innerHTML; } - if (attributes.styles) { - for (const styleType in attributes.styles) { - element.style[styleType as any] = attributes.styles[styleType]; - } - } delete attributes.innerHTML; - delete attributes.styles; for (const attribute in attributes) { if (attributes[attribute] === null) { element.removeAttribute(attribute); diff --git a/src/utils/styling.ts b/src/utils/styling.ts new file mode 100644 index 0000000..0e527d9 --- /dev/null +++ b/src/utils/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]; + } + } +}; |
