forked from GouGoGoal/SHELL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (56 loc) · 1.64 KB
/
index.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
// index.js
const apiUrl = 'https://api.github.com/repos/wangchunin/SHELL/contents';
async function fetchDirectory(path) {
const response = await fetch(apiUrl + path, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Cloudflare-Worker/1.0)',
'Accept': 'application/vnd.github.v3+json'
}
});
if (!response.ok) {
throw new Error('Failed to fetch directory');
}
const data = await response.json();
return data;
}
function generateDirectoryPage(data, path) {
const container = document.getElementById('container');
container.innerHTML = '';
const title = document.createElement('h1');
title.textContent = 'Directory';
container.appendChild(title);
const ul = document.createElement('ul');
container.appendChild(ul);
data.forEach(item => {
const li = document.createElement('li');
ul.appendChild(li);
const itemName = item.name;
const itemPath = item.path;
const isDirectory = item.type === 'dir';
if (isDirectory) {
const link = document.createElement('a');
link.textContent = itemName;
link.href = itemPath;
link.addEventListener('click', async (event) => {
event.preventDefault();
await showDirectory(itemPath);
});
li.appendChild(link);
} else {
const link = document.createElement('a');
link.textContent = itemName;
link.href = itemPath;
link.target = '_blank';
li.appendChild(link);
}
});
}
async function showDirectory(path) {
try {
const data = await fetchDirectory(path);
generateDirectoryPage(data, path);
} catch (error) {
console.error(error);
}
}
showDirectory('/');