skinswitcher/src/index.js

109 lines
3.2 KiB
JavaScript

/* eslint-disable no-unused-vars */
const {
shell,
ipcRenderer
} = require("electron");
const fs = require("fs");
const path = require("path");
const $ = require("jquery");
const skinview3d = require("skinview3d/dist/skinview3d.min.js");
const axios = require("axios");
const uuid = require("uuid");
const MojangAPI = require(path.join(__dirname + "/../src/api.js"));
var settings = JSON.parse(fs.readFileSync(path.join(__dirname + "/../assets/data.json")));
var api;
var theme = document.createElement("link");
theme.rel = "stylesheet";
switch (settings.theme) {
case "light":
theme.href = "../node_modules/bulmaswatch/default/bulmaswatch.min.css";
break;
case "dark":
theme.href = "../node_modules/bulmaswatch/cyborg/bulmaswatch.min.css";
break;
}
document.head.appendChild(theme);
if (settings.clientId == "") {
notify("warning", "Generating client id");
settings.clientId = uuid.v4().replace(/-/g, "");
save();
api = new MojangAPI(settings.clientId);
} else {
api = new MojangAPI(settings.clientId);
}
function load(url) {
var re = new RegExp("^(http|https)://", "i");
if (re.test(url)) {
shell.openExternal(url);
} else {
if (!api.accessToken && url != "/partials/auth.html") {
notify("warning", "You need to login first");
} else {
$("#content").load(path.join(__dirname + url), () => {
$("a").each((i, e) => {
$(e).off("click");
$(e).on("click", (event) => {
event.preventDefault();
load(e.getAttribute("href"));
});
if (!re.test($(e).attr("href"))) {
if ($(e).is("div.tabs > ul > li > a")) {
if (url == $(e).attr("href")) {
$(e).parent().addClass("is-active");
} else {
$(e).parent().removeClass("is-active");
}
}
}
});
});
}
}
}
function notify(type, message) {
var notificationArea = document.getElementById("notification-area");
var notification = document.createElement("div");
notification.className = `notification is-${type}`;
var close = document.createElement("div");
close.className = "delete";
notification.appendChild(close);
notification.innerHTML += message;
notificationArea.appendChild(notification);
const delay = setTimeout(() => {
notificationArea.removeChild(notification);
}, 5000);
notification.addEventListener("click", (e) => {
if ($(e.target).is("div.notification > div.delete")) {
clearTimeout(delay);
notificationArea.removeChild(notification);
}
});
}
function save() {
fs.writeFile(path.join(__dirname + "/../assets/data.json"), JSON.stringify(settings, null, " "), (e) => {
if (e) {
notify("danger", "Setting failed to save");
} else {
notify("success", "Settings saved successfully");
}
});
}
load("/partials/auth.html");