-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path404.html
167 lines (147 loc) · 4.79 KB
/
404.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub Repository Viewer</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
a {
color: #0366d6;
text-decoration: none;
cursor: pointer;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div id="errorPage" style="display: none;">
<h1>404 - Page Not Found</h1>
<p>Sorry, the requested page could not be found.</p>
</div>
<div id="repoList">
<h2>Repository Files:</h2>
<table id="fileTable">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Type</th>
<th>Size</th>
<th>Last Modified Date</th>
<th>SHA</th>
</tr>
</thead>
<tbody id="fileList"></tbody>
</table>
</div>
<!-- ... (your HTML head and body structure) ... -->
<script>
// Function to parse the username, repo name, and folder path from the URL
function parseUrl() {
const urlParts = window.location.pathname.split('/').filter(Boolean);
if (urlParts.length >= 1) {
return {
repoName: urlParts[0],
folderPath: urlParts.slice(1).join('/')
};
}
return {};
}
// Function to extract subdomain from URL
function extractSubdomain(url) {
const subdomainRegex = /^(https?:\/\/)?([a-zA-Z0-9-]+)\./;
const match = url.match(subdomainRegex);
return match ? match[2] : null;
}
// Function to fetch repository contents using GitHub API
async function getRepoContents(username, repoName, folderPath) {
const apiUrl = `https://api.github.com/repos/${username}/${repoName}/contents/${folderPath}`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error('Not Found');
}
const data = await response.json();
// Organize directories alphabetically
const sortedData = data.sort((a, b) => {
if (a.type === 'dir' && b.type !== 'dir') return -1;
if (a.type !== 'dir' && b.type === 'dir') return 1;
return a.name.localeCompare(b.name);
});
// Extract and display file details in a table
const fileList = document.getElementById('fileList');
fileList.innerHTML = await Promise.all(sortedData.map(async (item, index) => {
// Fetch the last modified date for each file
const commitDate = await getFileLastModifiedDate(username, repoName, item.path);
// Trim the trailing comma in SHA value
const trimmedSHA = item.sha.replace(/,$/, '');
// Create a link for files and directories
const linkElement = item.type === 'dir' ? `<a href="${repoName}/${folderPath}/${item.name}/" onclick="return getFileContents('${username}', '${repoName}', '${item.path}')">${item.name}</a>` : `<a href="${item.html_url}">${item.name}</a>` ;
return `
<tr>
<td>${index + 1}</td>
<td>${linkElement}</td>
<td>${item.type}</td>
<td>${item.type === 'dir' ? '' : item.size}</td>
<td>${commitDate}</td>
<td>${trimmedSHA}</td>
</tr>
`;
})).join('');
} catch (error) {
console.error('Error fetching repository contents:', error);
document.getElementById('errorPage').style.display = 'block';
}
}
// Function to fetch the last modified date of a file
async function getFileLastModifiedDate(username, repoName, filePath) {
const commitUrl = `https://api.github.com/repos/${username}/${repoName}/commits?path=${filePath}`;
try {
const response = await fetch(commitUrl);
const commitData = await response.json();
if (commitData.length > 0) {
return new Date(commitData[0].commit.author.date).toLocaleString();
} else {
return 'N/A';
}
} catch (error) {
console.error(`Error fetching commit information for file ${filePath}:`, error);
return 'N/A';
}
}
// Function to get contents when clicking on a directory link
function getFileContents(username, repoName, folderPath) {
getRepoContents(username, repoName, folderPath);
return false; // Prevent default link behavior
}
// Call the function to parse the URL and fetch and display repository contents
const { repoName, folderPath } = parseUrl();
const url = window.location.href;
const subdomain = extractSubdomain(url);
if (repoName) {
getRepoContents(subdomain, repoName, folderPath);
} else {
document.getElementById('errorPage').style.display = 'block';
}
</script>
</body>
</html>