aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/observe-dom.ts2
-rw-r--r--src/utils/replace-attributes.ts34
2 files changed, 35 insertions, 1 deletions
diff --git a/src/utils/observe-dom.ts b/src/utils/observe-dom.ts
index e375326..30a07b4 100644
--- a/src/utils/observe-dom.ts
+++ b/src/utils/observe-dom.ts
@@ -1,4 +1,4 @@
-export default (callback: () => boolean) => {
+export const observeDOM = (callback: () => boolean) => {
const observer = new MutationObserver(() => {
if (callback()) {
observer.disconnect();
diff --git a/src/utils/replace-attributes.ts b/src/utils/replace-attributes.ts
new file mode 100644
index 0000000..443c1d0
--- /dev/null
+++ b/src/utils/replace-attributes.ts
@@ -0,0 +1,34 @@
+import { observeDOM } from "@/utils/observe-dom";
+
+export const replaceAttributes = (elements: any[]) => {
+ let count = 0;
+
+ observeDOM(() => {
+ for (let i = 0; i < elements.length; i++) {
+ if (elements[i]) {
+ const element = document.querySelector(elements[i]![0]) as HTMLElement | null;
+ if (element) {
+ const attributes = elements[i]![1];
+ if (attributes.innerHTML) {
+ element.innerHTML = attributes.innerHTML;
+ }
+ delete attributes.innerHTML;
+ for (const attribute in attributes) {
+ if (attributes[attribute] === null) {
+ element.removeAttribute(attribute);
+ } else {
+ element.setAttribute(attribute, attributes[attribute]);
+ }
+ }
+ elements[i] = undefined;
+ count++;
+ if (count >= elements.length) {
+ return true;
+ }
+ }
+ }
+ }
+
+ return false;
+ });
+};