aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/replace-attributes.ts
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-02-14 09:46:34 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-02-14 09:46:34 +0530
commita30b28d40e5bacdbbbcad3c9de8f1fe3e083119c (patch)
treec2f367c5ea4179911a0c2d279a2e980a96414d26 /src/utils/replace-attributes.ts
parent34ad953c953a395ba3e51c8125034a6bb65b2a79 (diff)
downloadbanglar-bhumi-utils-a30b28d40e5bacdbbbcad3c9de8f1fe3e083119c.tar.gz
banglar-bhumi-utils-a30b28d40e5bacdbbbcad3c9de8f1fe3e083119c.zip
Added ability to load static assets from extension's assets.
Diffstat (limited to 'src/utils/replace-attributes.ts')
-rw-r--r--src/utils/replace-attributes.ts34
1 files changed, 34 insertions, 0 deletions
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;
+ });
+};