-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathscript.js
More file actions
182 lines (173 loc) · 6.51 KB
/
script.js
File metadata and controls
182 lines (173 loc) · 6.51 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const container = document.getElementById('container');
const zoneViewer = document.getElementById('zoneViewer');
let zoneFrame = document.getElementById('zoneFrame');
const searchBar = document.getElementById('searchBar');
const sortOptions = document.getElementById('sortOptions');
// https://www.jsdelivr.com/tools/purge
const zonesURL = "https://cdn.jsdelivr.net/gh/gn-math/assets@main/zones.json";
const coverURL = "https://cdn.jsdelivr.net/gh/gn-math/covers@main";
const htmlURL = "https://cdn.jsdelivr.net/gh/gn-math/html@main";
let zones = [];
let popularityData = {};
async function listZones() {
try {
const response = await fetch(zonesURL);
const json = await response.json();
zones = json;
await fetchPopularity();
sortZones();
const search = new URLSearchParams(window.location.search);
const id = search.get('id');
if (id) {
const zone = zones.find(zone => zone.id + '' == id + '');
if (zone) {
openZone(zone);
}
}
} catch (error) {
container.innerHTML = `Error loading zones: ${error}`;
}
}
async function fetchPopularity() {
try {
const response = await fetch("https://data.jsdelivr.com/v1/stats/packages/gh/gn-math/html@main/files?period=year");
const data = await response.json();
data.forEach(file => {
const idMatch = file.name.match(/\/(\d+)\.html$/);
if (idMatch) {
const id = parseInt(idMatch[1]);
popularityData[id] = file.hits.total;
}
});
} catch (error) {
popularityData[0] = 0;
}
}
function sortZones() {
const sortBy = sortOptions.value;
if (sortBy === 'name') {
zones.sort((a, b) => a.name.localeCompare(b.name));
} else if (sortBy === 'id') {
zones.sort((a, b) => a.id - b.id);
} else if (sortBy === 'popular') {
zones.sort((a, b) => (popularityData[b.id] || 0) - (popularityData[a.id] || 0));
}
zones.sort((a, b) => (a.id === -1 ? -1 : b.id === -1 ? 1 : 0));
displayZones(zones);
}
function displayZones(zones) {
container.innerHTML = "";
zones.forEach(file => {
const zoneItem = document.createElement("div");
zoneItem.className = "zone-item";
zoneItem.onclick = () => openZone(file);
const img = document.createElement("img");
img.src = file.cover.replace("{COVER_URL}", coverURL).replace("{HTML_URL}", htmlURL);
zoneItem.appendChild(img);
const button = document.createElement("button");
button.textContent = file.name;
button.onclick = (event) => {
event.stopPropagation();
openZone(file);
};
zoneItem.appendChild(button);
container.appendChild(zoneItem);
});
if (container.innerHTML === "") {
container.innerHTML = "No zones found.";
} else {
document.getElementById("zoneCount").textContent = `Zones Loaded: ${zones.length}`;
}
}
function filterZones() {
const query = searchBar.value.toLowerCase();
const filteredZones = zones.filter(zone => zone.name.toLowerCase().includes(query));
displayZones(filteredZones);
}
function openZone(file) {
if (file.url.startsWith("http")) {
window.location.href = file.url;
} else {
const url = file.url.replace("{COVER_URL}", coverURL).replace("{HTML_URL}", htmlURL);
fetch(url).then(response => response.text()).then(html => {
if (zoneFrame.contentDocument === null) {
zoneFrame = document.createElement("iframe");
zoneFrame.id = "zoneFrame";
zoneViewer.appendChild(zoneFrame);
}
zoneFrame.contentDocument.open();
zoneFrame.contentDocument.write(html);
zoneFrame.contentDocument.close();
document.getElementById('zoneName').textContent = file.name;
document.getElementById('zoneId').textContent = file.id;
zoneViewer.style.display = "block";
}).catch(error => alert("Failed to load zone: " + error));
}
}
function aboutBlank() {
const newWindow = window.open("about:blank", "_blank");
let zone = zones.find(zone => zone.id + '' === document.getElementById('zoneId').textContent).url.replace("{COVER_URL}", coverURL).replace("{HTML_URL}", htmlURL);
fetch(zone).then(response => response.text()).then(html => {
if (newWindow) {
newWindow.document.open();
newWindow.document.write(html);
newWindow.document.close();
}
})
}
function closeZone() {
zoneViewer.style.display = "none";
zoneViewer.removeChild(zoneFrame);
}
function fullscreenZone() {
if (zoneFrame.requestFullscreen) {
zoneFrame.requestFullscreen();
} else if (zoneFrame.mozRequestFullScreen) {
zoneFrame.mozRequestFullScreen();
} else if (zoneFrame.webkitRequestFullscreen) {
zoneFrame.webkitRequestFullscreen();
} else if (zoneFrame.msRequestFullscreen) {
zoneFrame.msRequestFullscreen();
}
}
function saveData() {
let data = JSON.stringify(localStorage) + "\n\n|\n\n" + document.cookie;
const link = document.createElement("a");
link.href = URL.createObjectURL(new Blob([data], {
type: "text/plain"
}));
link.download = `${Date.now()}.data`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function loadData(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (e) {
const content = e.target.result;
const [localStorageData, cookieData] = content.split("\n\n|\n\n");
try {
const parsedData = JSON.parse(localStorageData);
for (let key in parsedData) {
localStorage.setItem(key, parsedData[key]);
}
} catch (error) {
console.error(error);
}
if (cookieData) {
const cookies = cookieData.split("; ");
cookies.forEach(cookie => {
document.cookie = cookie;
});
}
alert("Data loaded");
};
reader.readAsText(file);
}
const darkModeToggle = document.getElementById('darkModeToggle');
darkModeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
});
listZones();