-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (52 loc) · 1.93 KB
/
script.js
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
document.addEventListener("DOMContentLoaded", () => {
const buttons = document.querySelectorAll(".module-nav button");
const loadModule = (moduleName) => {
window.location.href = `?m=${moduleName}`;
};
buttons.forEach((button) => {
button.addEventListener("click", () => {
const moduleName = button.getAttribute("data-module");
loadModule(moduleName);
});
});
const loadModuleContent = async (moduleName) => {
const moduleContent = document.getElementById("module-content");
try {
moduleContent.innerHTML = "";
if (window.cleanup) {
window.cleanup();
}
const oldScript = document.getElementById("module-script");
const oldStyle = document.getElementById("module-style");
if (oldScript) oldScript.remove();
if (oldStyle) oldStyle.remove();
await Promise.all([
loadScript("/configPanel.js"),
loadScript("/moduleManager.js")
]);
const html = await fetch(`modules/${moduleName}/index.html`).then(res => res.text());
moduleContent.innerHTML = html;
await loadScript(`modules/${moduleName}/script.js?v=${Date.now()}`);
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = `modules/${moduleName}/style.css?v=${Date.now()}`;
link.id = "module-style";
document.head.appendChild(link);
buttons.forEach(btn => {
btn.classList.toggle("active", btn.getAttribute("data-module") === moduleName);
});
} catch (error) {
moduleContent.innerHTML = `<div class="default">Failed to load module: ${moduleName}</div>`;
}
};
function loadScript(src) {
const script = document.createElement("script");
script.src = src;
document.body.appendChild(script);
}
const urlParams = new URLSearchParams(window.location.search);
const initialModule = urlParams.get("m");
if (initialModule) {
loadModuleContent(initialModule);
}
});