blob: b3baffbae899c10ba7c4af7e012d878f2a24c0ef (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
});
};
|