-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (72 loc) · 2.36 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const capeImg = document.getElementById('cape');
const skinImg = document.getElementById('skin');
const button = document.getElementById('btn');
const usernameInput = document.getElementById('username');
let textureData = null;
let skinBlob = null;
let capeBlob = null;
function buttonState(state) {
button.textContent = state ? 'Get Skin' : 'Loading...';
button.disabled = !state;
}
async function fetchProfile(identifier) {
const response = await fetch(`https://crafthead.net/profile/${identifier}`);
if (!response.ok) throw new Error("Error while getting player data.");
const data = await response.json();
if (data.error === "User does not exist") {
alert("User does not exist. Please check the username or UUID and try again.");
throw new Error("User does not exist.");
}
return data;
}
async function execute(identifier) {
if (button.disabled) return;
buttonState(false);
skinImg.style.display = 'none';
capeImg.style.display = 'none';
skinImg.src = '';
capeImg.src = '';
textureData = null;
skinBlob = null;
capeBlob = null;
try {
const profile = await fetchProfile(identifier);
const properties = profile.properties.find(p => p.name === 'textures');
if (!properties) throw new Error("Error while getting skin data.");
usernameInput.value = profile.name;
const decoded = JSON.parse(atob(properties.value));
textureData = decoded.textures;
if (textureData.SKIN?.url) skinBlob = await displayImg(textureData.SKIN.url, skinImg);
if (textureData.CAPE?.url) capeBlob = await displayImg(textureData.CAPE.url, capeImg);
} catch (e) {
console.error(e.message);
} finally {
buttonState(true);
}
}
async function displayImg(url, element) {
const response = await fetch(url);
const blob = await response.blob();
element.src = URL.createObjectURL(blob);
element.style.display = 'block';
return blob;
}
function dl(type) {
const blob = type === "skin" ? skinBlob : capeBlob;
if (blob) {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `${usernameInput.value}'s ${type}.png`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
setTimeout(() => {
document.addEventListener("keydown", function(e) {
if (e.key === "Enter") {
e.preventDefault();
execute(usernameInput.value.trim());
};
});
}, 300);