aboutsummaryrefslogtreecommitdiff
path: root/static/js/domfilter.ts
blob: 8900e2f45bc4fa17a09c7e1e2deb0cef24f374fa (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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * DOM Filter Copyright (C) 2024 Thedro Neely
 * Licence: AGPL | https://www.gnu.org/licenses/agpl-3.0.txt
*/

(function () {
  type millisecond = number;
  const timeout: millisecond = 300;

  const state = "on";
  const key = "config.navigation.fast";

  function fetch(url, method, callback) {
    const http = new XMLHttpRequest();
    http.onreadystatechange = function () {
      if (callback && http.readyState === 4) {
        if (http.status === 200) callback(http);
        else {
          console.error("ERROR: Unable to navigate", http);
          self.location.href = url;
        }
      }
    };
    http.open(method, url);
    http.timeout = timeout;
    http.send();
    return http;
  }

  function styles(node) {
    return (node.nodeName === "LINK") && node.rel && node.rel.includes("stylesheet");
  }

  function scripts(node) {
    return (node.nodeName === "SCRIPT") && node.hasAttribute("src");
  }

  function filter(url, http) {
    let live = document;
    let node = live.head.childNodes.length;
    let next = (new DOMParser()).parseFromString(http.responseText, "text/html");

    if (next.doctype === null || !http.getResponseHeader("content-type").includes("text/html")) return false;

    while (node--) {
      if (styles(live.head.childNodes[node]) || scripts(live.head.childNodes[node])) {
        // console.log("INFO: Persist:", live.head.childNodes[node]);
      } else {
        live.head.removeChild(live.head.childNodes[node]);
      }
    }

    while (next.head.firstChild) {
      if (styles(next.head.firstChild) || scripts(next.head.firstChild)) {
        next.head.removeChild(next.head.firstChild);
      } else {
        live.head.appendChild(next.head.firstChild);
      }
    }

    while (live.documentElement.attributes.length > 0) {
      live.documentElement.removeAttribute(
        live.documentElement.attributes[live.documentElement.attributes.length - 1].name,
      );
    }

    while (next.documentElement.attributes.length > 0) {
      live.documentElement.setAttribute(
        next.documentElement.attributes[next.documentElement.attributes.length - 1].name,
        next.documentElement.attributes[next.documentElement.attributes.length - 1].value,
      );
      next.documentElement.removeAttribute(
        next.documentElement.attributes[next.documentElement.attributes.length - 1].name,
      );
    }

    live.body.parentElement.replaceChild(next.body, live.body);
  }

  function persist() {
    let persist = document.createElement("link");
    persist.rel = "location";
    persist.target = JSON.stringify(self.location);
    document.head.appendChild(persist);
  }

  self.addEventListener("DOMContentLoaded", function () {
    if (localStorage[key] !== state) return;
    persist();
  });

  self.addEventListener("popstate", function (event) {
    if (localStorage[key] !== state) return;
    const link = JSON.parse(document.querySelector('link[rel="location"]').target);
    const url = event.target.location;
    const hashed = link.pathname === url.pathname;
    if (hashed) return;
    fetch(url, "GET", function (http) {
      if (filter(url, http) === false) return self.location.href = url;
      persist();
      self.document.dispatchEvent(new CustomEvent("URLChangedCustomEvent", { bubbles: true }));
    });
  });

  self.addEventListener("click", function (event) {
    if (localStorage[key] !== state) return;
    const links = document.querySelectorAll("a");
    for (let i = 0; i < links.length; i++) {
      const active = links[i].contains(event.target);
      const change = self.location.href !== links[i].href;
      const view = links[i].attributes.hasOwnProperty("download") === false;
      const local = self.location.origin === links[i].origin && links[i].target !== "_self";
      const hashed =  self.location.pathname === links[i].pathname && links[i].href.includes("#");
      if (active && local && change && view && (hashed === false)) {
        event.preventDefault();
        const url = links[i].href;
        links[i].style.cursor = "wait";
        fetch(url, "GET", function (http) {
          links[i].removeAttribute("style")
          if (filter(url, http) === false) return self.location.href = url;
          history.pushState({}, "", links[i].href);
          persist();
          self.document.dispatchEvent(new CustomEvent("URLChangedCustomEvent", { bubbles: true }));
        });
      }
    }
  });
})();

/*
  * Copyright (C) 2024 Thedro Neely
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Affero General Public License for more details.
  *
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/