summaryrefslogtreecommitdiffstats
path: root/typescript/src/reRenderComponent.ts
blob: f73fd1a31847b2e881059b985c124362b903dea3 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 });
  }
};