69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
(() => {
|
|
// Theme switch
|
|
const body = document.body;
|
|
const lamp = document.getElementById("mode");
|
|
|
|
const toggleTheme = (state) => {
|
|
if (state === "dark") {
|
|
localStorage.setItem("theme", "light");
|
|
body.removeAttribute("data-theme");
|
|
} else if (state === "light") {
|
|
localStorage.setItem("theme", "dark");
|
|
body.setAttribute("data-theme", "dark");
|
|
} else {
|
|
initTheme(state);
|
|
}
|
|
};
|
|
|
|
lamp.addEventListener("click", () =>
|
|
toggleTheme(localStorage.getItem("theme"))
|
|
);
|
|
|
|
// Blur the content when the menu is open
|
|
const cbox = document.getElementById("menu-trigger");
|
|
|
|
cbox.addEventListener("change", function () {
|
|
const area = document.querySelector(".wrapper");
|
|
this.checked
|
|
? area.classList.add("blurry")
|
|
: area.classList.remove("blurry");
|
|
});
|
|
|
|
// Enable keyboard steering for the navigation menu
|
|
document.getElementById("menu-icon").addEventListener("keydown", (evt) => {
|
|
if (evt.key == "Enter" || evt.key == " ") {
|
|
cbox.checked = !cbox.checked;
|
|
}
|
|
});
|
|
|
|
if (typeof crTypes !== 'undefined') {
|
|
// Codersrank
|
|
const placeholders = document.getElementsByClassName("cr-placeholder");
|
|
if (localStorage.getItem("load-codersrank")) {
|
|
loadCodersrank(crTypes);
|
|
} else {
|
|
for (ph of placeholders) {
|
|
ph.addEventListener("click", e => {
|
|
e.preventDefault();
|
|
console.log(e);
|
|
localStorage.setItem("load-codersrank", true);
|
|
loadCodersrank(crTypes);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
function loadCodersrank(types) {
|
|
const scriptElems = [];
|
|
for (type of types) {
|
|
const script = document.createElement("script");
|
|
script.src = "https://unpkg.com/@codersrank/" + type + "/codersrank-" + type + ".min.js";
|
|
scriptElems.push(script);
|
|
}
|
|
for (ph of document.getElementsByClassName("cr-placeholder")) {
|
|
ph.parentElement.innerHTML = ph.childNodes[0].data;
|
|
}
|
|
document.head.append(...scriptElems);
|
|
}
|
|
})();
|