-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement-loader.js
More file actions
75 lines (52 loc) · 1.9 KB
/
Copy pathelement-loader.js
File metadata and controls
75 lines (52 loc) · 1.9 KB
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
// Start Time
const startTime = new Date();
// Attempting to Read Cookie
import { readCookie } from "./cookie-reader.js";
const elementsCookie = readCookie("elements");
let text;
if (elementsCookie != "") {
text = elementsCookie.replaceAll("?", " ").replaceAll("^", ",").replaceAll("%", ";").trim();
} else {
// Fetching Elements
const response = await fetch('/elements.html');
text = await response.text();
// Removing Comments, Indents, and Empty Lines
while (true) {
const startIndex = text.indexOf("<!--")
if (startIndex == -1) break;
const endIndex = text.indexOf("-->", startIndex + 4);
if (endIndex == -1) break;
text = text.substring(0, startIndex) + text.substring(endIndex + 4);
}
text = text.replaceAll(" ", "").replaceAll("\n", "");
// Saving Cookie
document.cookie =
"elements=" +
text.replaceAll(" ", "?").replaceAll(",", "^").replaceAll(";", "%") +
"; path=/;";
}
// Loading Elements
const parser = new DOMParser();
const documentElements = parser.parseFromString(text, 'text/html');
const elements = ['first-header', 'second-header', 'footer'];
for (let i in elements) {
const elementId = elements[i];
document.getElementById(elementId + '-loader').innerHTML =
documentElements.getElementById(elementId).outerHTML;
}
// Getting Website Version Data
let websiteVersion;
const projectsCookie = readCookie("projects");
if (projectsCookie != "") {
websiteVersion = projectsCookie.split("_")[6];
} else {
const { projects } = await import("/data-loader.js");
websiteVersion = projects[2].version;
}
document.getElementById("first-header-version").textContent = "Version " + websiteVersion;
// Log Script Time
const endTime = new Date();
console.log(
("/element-loader.js: ").padEnd(35) + // script path
(endTime - startTime).toString().padStart(4) + "ms" // time
);