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
|
import {Toaster} from "./toaster";
import {configuration} from "./configuration";
export const toaster = new Toaster();
export const doc = document,
$$ = (s, o = doc) => o.querySelectorAll(s),
$ = (s, o = doc) => o.querySelector(s);
function toggleDarkTheme() {
if (localStorage["dark-theme"] === "true") localStorage["dark-theme"] = "false";
else localStorage["dark-theme"] = "true";
setTheme();
}
function setTheme() {
if (localStorage["dark-theme"] === "true") {
document.getElementsByTagName("html")[0].setAttribute("dark-theme", "");
} else {
document.getElementsByTagName("html")[0].removeAttribute("dark-theme");
}
}
window.addEventListener("error", (e) => {
if (configuration.analytics.error) {
(async () => {
const error = {
msg: e.message,
line: e.lineno,
path: location.pathname,
filename: e.filename,
};
await fetch("/api/analytics/error", {
method: "post",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(error),
});
})();
}
});
window.addEventListener("load", () => {
if (configuration.analytics.pageload && !location.pathname.startsWith("/kontoret")) {
(async () => {
//const loadTime = window.performance.timing.domContentLoadedEventEnd -
// window.performance.timing.navigationStart;
const loadTime = window.performance.timeOrigin - window.performance.now();
await fetch("/api/analytics/pageload", {
method: "post",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({time: loadTime, path: location.pathname}),
});
})();
}
});
// https://stackoverflow.com/a/18234317/11961742
String.prototype.formatUnicorn = String.prototype.formatUnicorn || function () {
"use strict";
let str = this.toString();
if (arguments.length) {
const t = typeof arguments[0];
const args = ("string" === t || "number" === t) ?
Array.prototype.slice.call(arguments)
: arguments[0];
for (const key in args) {
str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), args[key]);
}
}
return str;
};
setTheme();
export function pageInit(cb) {
if (typeof cb === "function") cb();
}
doc.addEventListener("DOMContentLoaded", pageInit);
|