-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
55 lines (52 loc) · 2.2 KB
/
index.html
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的 GitHub 仓库内容</title>
<style>
body { font-family: Arial, sans-serif; }
pre { background: #f0f0f0; padding: 10px; }
</style>
</head>
<body>
<h1>我的 GitHub 仓库内容</h1>
<div id="repo-content"></div>
<script>
const username = 'xiaoyaoyufeng0'; // 替换为你的 GitHub 用户名
const repo = '2'; // 替换为你的仓库名
fetch(`https://api.github.com/repos/${username}/${repo}/contents`)
.then(response => {
if (!response.ok) throw new Error('网络响应不正常');
return response.json();
})
.then(data => {
const contentDiv = document.getElementById('repo-content');
data.forEach(file => {
const fileLink = document.createElement('a');
fileLink.textContent = file.name;
fileLink.href = "#";
fileLink.onclick = () => fetchFileContent(file.path);
contentDiv.appendChild(fileLink);
contentDiv.appendChild(document.createElement('br'));
});
})
.catch(error => {
document.getElementById('repo-content').textContent = '获取内容时出错: ' + error.message;
});
function fetchFileContent(filePath) {
fetch(`https://api.github.com/repos/${username}/${repo}/contents/${filePath}`)
.then(response => response.json())
.then(data => {
const contentDiv = document.getElementById('repo-content');
const fileContent = atob(data.content); // 解码文件内容
contentDiv.innerHTML = `<h2>${filePath}</h2><pre>${fileContent}</pre>`;
})
.catch(error => {
const contentDiv = document.getElementById('repo-content');
contentDiv.innerHTML = '获取文件内容时出错: ' + error.message;
});
}
</script>
</body>
</html>