forked from Egezenn/dota2-minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmods.js
More file actions
124 lines (109 loc) · 4.64 KB
/
Copy pathmods.js
File metadata and controls
124 lines (109 loc) · 4.64 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const modButtonsContainer = document.getElementById("mod-buttons");
const notesModal = new bootstrap.Modal(document.getElementById("notesModal"));
const notesModalLabel = document.getElementById("notesModalLabel");
const notesContent = document.getElementById("notes-content");
const cacheKey = "github-mods";
const cachedData = JSON.parse(localStorage.getItem(cacheKey));
const now = new Date().getTime();
function processMods(mods) {
modButtonsContainer.innerHTML = "";
mods.forEach((mod) => {
const card = document.createElement("div");
card.className = "card h-100 text-center mod-card";
card.style.cursor = "pointer";
card.setAttribute("role", "button");
const img = document.createElement("img");
img.src = `https://raw.githubusercontent.com/Egezenn/dota2-minify/main/Minify/mods/${encodeURIComponent(mod)}/preview.jpg`;
img.className = "card-img-top";
img.alt = `${mod} image`;
img.style.objectFit = "cover";
img.style.height = "140px";
img.onerror = function () {
this.style.display = "none";
};
const cardBody = document.createElement("div");
cardBody.className = "card-body d-flex align-items-center justify-content-center";
const title = document.createElement("h5");
title.className = "card-title mb-0";
title.textContent = mod;
cardBody.appendChild(title);
card.appendChild(img);
card.appendChild(cardBody);
card.addEventListener("click", () => {
const notesCacheKey = `mod-notes-${mod}`;
const cachedNotes = JSON.parse(sessionStorage.getItem(notesCacheKey));
const now = new Date().getTime();
const imageUrl = `https://raw.githubusercontent.com/Egezenn/dota2-minify/main/Minify/mods/${encodeURIComponent(mod)}/preview.jpg`;
const imageHtml = `<img src="${imageUrl}" class="mb-3" style="display: block; margin-left: auto; margin-right: auto;" onerror="this.style.display='none'" alt="${mod}">`;
notesModalLabel.textContent = `${mod} Notes`;
notesModal.show();
if (cachedNotes && now - cachedNotes.timestamp < 60000) {
notesContent.innerHTML =
imageHtml + marked.parse(cachedNotes.content).replace(/<p>\s*!!:\s*/g, '<p class="note-emphasized">');
} else {
const notesUrl = `https://raw.githubusercontent.com/Egezenn/dota2-minify/main/Minify/mods/${encodeURIComponent(mod)}/notes.md`;
notesContent.innerHTML = "<p>Loading...</p>";
fetch(notesUrl)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.text();
})
.then((text) => {
const enMatch = text.match(/<!-- LANG:en -->([\s\S]*?)(?=<!-- LANG:\w+ -->|$)/i);
const content = enMatch ? enMatch[1].trim() : text;
notesContent.innerHTML =
imageHtml + marked.parse(content).replace(/<p>\s*!!:\s*/g, '<p class="note-emphasized">');
sessionStorage.setItem(notesCacheKey, JSON.stringify({ timestamp: now, content: content }));
})
.catch((error) => {
notesContent.innerHTML = "<p>Could not load notes. The file might not exist for this mod.</p>";
console.error("Error fetching notes:", error);
});
}
});
modButtonsContainer.appendChild(card);
});
}
if (cachedData && now - cachedData.timestamp < 60000) {
processMods(cachedData.mods);
} else {
const fetchMods = (retry = true) => {
fetch("https://api.github.com/repos/egezenn/dota2-minify/contents/Minify/mods")
.then((response) => {
if (response.status !== 200) {
if (retry) {
setTimeout(() => fetchMods(false), 3000);
return null;
}
throw new Error(`HTTP status ${response.status}`);
}
return response.json();
})
.then((data) => {
if (!data) return;
if (Array.isArray(data)) {
const mods = data
.filter((item) => item.type === "dir" && item.name !== "#base" && item.name !== "User Styles")
.map((item) => item.name);
if (mods.length > 0) {
localStorage.setItem(cacheKey, JSON.stringify({ timestamp: new Date().getTime(), mods: mods }));
processMods(mods);
} else {
throw new Error("No mods found");
}
} else {
throw new Error("Invalid mods data");
}
})
.catch((error) => {
if (cachedData) {
processMods(cachedData.mods);
}
modButtonsContainer.textContent = "Could not load mods list.";
console.error("Error fetching mods list:", error);
});
};
fetchMods();
}