skinswitcher/src/skin.js

145 lines
4.0 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-unused-vars */
const {
BrowserWindow,
ipcRenderer,
remote
} = require("electron");
const axios = require("axios").default;
const path = require("path");
2020-05-12 19:02:20 +02:00
const skinview3d = require("skinview3d/dist/skinview3d.min.js");
const session = ipcRenderer.sendSync("getSession");
if (!session.accessToken) {
console.log("Session does not exist, return to auth");
remote.getCurrentWindow().loadURL(path.join(`file://${__dirname}/auth.hbs`));
} else {
console.log("Session does exist, continue");
}
const regex = /(?!\w*_)\w*(?=\.\w*)/g;
var action = __filename.match(regex)[0];
var parser = new DOMParser();
var randomUrls = [];
// Switch button and views
var btn = document.querySelectorAll("div.content > div.left > button#switch")[0];
var view = {
flat: document.querySelectorAll("div.content > div.left > img.skin#flat")[0],
mesh: document.querySelectorAll("div.content > div.left > div#mesh")[0]
};
function setView(url, controls = true) {
btn.style.display = "initial";
view.flat.src = url;
view.mesh.innerHTML = "";
// Set mesh view
let skinViewer = new skinview3d.SkinViewer({
domElement: view.mesh,
width: 300,
height: 250,
skinUrl: url
});
let control = skinview3d.createOrbitControls(skinViewer);
control.enableRotate = controls;
control.enableZoom = false;
control.enablePan = false;
}
switch (action) {
case "current":
axios({
method: "GET",
url: "https://sessionserver.mojang.com/session/minecraft/profile/" + session.selectedProfile.id
}).then((data) => {
setView(JSON.parse(atob(data.data.properties[0].value)).textures.SKIN.url);
});
break;
case "upload":
document.querySelectorAll("div.content > div.right > button").forEach(element => {
element.addEventListener("click", (e) => {
document.querySelectorAll("div.content > div.right > button").forEach(button => {
if (button.id != "main") {
button.style.display = "none";
}
});
document.querySelectorAll(`div.content > div.right > form#${element.id}`)[0].style.display = "block";
2020-05-12 19:02:20 +02:00
});
});
2020-05-12 19:02:20 +02:00
break;
case "random":
axios({
method: "GET",
url: "https://nl.namemc.com/minecraft-skins/random"
}).then((data) => {
var namemc = parser.parseFromString(data.data, "text/html");
namemc.querySelectorAll("a").forEach((e) => {
var href = e.getAttribute("href");
if (href.includes("skin")) {
var id = href.split("/")[2];
if (id && id.length == 16 && !(id.includes("-"))) {
randomUrls.push("https://nl.namemc.com/texture/" + id + ".png");
}
2020-05-12 19:02:20 +02:00
}
});
function random() {
console.log(randomUrls.length);
if (randomUrls.length == 0) {
window.location.reload();
} else {
var id = Math.floor(Math.random() * (randomUrls.length - 0)) + 0;
setView(randomUrls[id]);
randomUrls.splice(id, 1);
}
}
random();
2020-05-12 19:02:20 +02:00
document.querySelectorAll("div.content > div.right > button#random")[0].addEventListener("click", (e) => {
random();
});
});
break;
}
// Download button action
document.querySelectorAll("div.content > div.right > button#download")[0].addEventListener("click", (e) => {
ipcRenderer.send("download", {
url: view.flat.src,
properties: {
saveAs: true
}
});
ipcRenderer.on("downloadResult", (event, arg) => {
var msg = document.querySelectorAll("div.content > p.message")[0];
console.log(msg);
msg.classList.add("success");
msg.innerHTML = "Downloaded successfully";
});
});
// Switch button action
btn.addEventListener("click", (e) => {
if (view.flat.style.display == "inline") {
view.flat.style.display = "none";
view.mesh.style.display = "inline";
btn.innerHTML = "Switch to 2D";
} else {
view.flat.style.display = "inline";
view.mesh.style.display = "none";
btn.innerHTML = "Switch to 3D";
}
});
// Back button action
document.querySelectorAll("div.content > div.right > button#main")[0].addEventListener("click", (e) => {
remote.getCurrentWindow().loadURL(path.join(`file://${__dirname}/main.hbs`));
});