summaryrefslogtreecommitdiffstats
path: root/typescript/src/reRenderComponent.ts
diff options
context:
space:
mode:
Diffstat (limited to 'typescript/src/reRenderComponent.ts')
-rw-r--r--typescript/src/reRenderComponent.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/typescript/src/reRenderComponent.ts b/typescript/src/reRenderComponent.ts
new file mode 100644
index 0000000..f73fd1a
--- /dev/null
+++ b/typescript/src/reRenderComponent.ts
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: AGPL-3.0-or-later */
+
+import podsClassName from "./podsClassName.js";
+import typescriptNever from "./typescriptNever.js";
+
+export default (newHTML: string): void => {
+ try {
+ console.assert(
+ Array.from(
+ document.querySelectorAll(
+ `html > body > #__next > div > main > main > div.${podsClassName}`
+ )
+ )
+ .filter((element: Element): boolean => {
+ if (element instanceof HTMLDivElement) {
+ return true;
+ } else if (element instanceof Element) {
+ console.warn({ element });
+ } else {
+ typescriptNever(element);
+ }
+
+ return false;
+ })
+ .map((element: Element): void => {
+ if (element instanceof Element) {
+ element.remove();
+ } else {
+ typescriptNever(element);
+ }
+ }).length <= 1
+ );
+
+ console.assert(
+ Array.from(
+ document.querySelectorAll(
+ "html > body > #__next > div > main > main > div:nth-of-type(1)"
+ )
+ )
+ .filter((element: Element): boolean => {
+ if (element instanceof HTMLDivElement) {
+ return true;
+ } else if (element instanceof Element) {
+ console.warn({ element });
+ } else if (element === null) {
+ console.warn({ element });
+ } else {
+ typescriptNever(element);
+ }
+
+ return false;
+ })
+ .map((element: Element): void => {
+ if (element instanceof Element) {
+ element.insertAdjacentHTML(
+ "afterend",
+ // npm i @types/dompurify
+ globalThis.DOMPurify.sanitize(newHTML)
+ );
+ } else if (element === null) {
+ console.warn({ element });
+ } else {
+ typescriptNever(element);
+ }
+ }).length === 1
+ );
+ } catch (error) {
+ console.warn({ error });
+ }
+};