-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeachapi.html
62 lines (57 loc) · 2.51 KB
/
feachapi.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Github Repo Fetcher</title>
</head>
<body>
<div class="container">
<form id="github-form">
<input type="text" id="username" placeholder="GitHub username...">
<button type="submit">Submit</button>
</form>
</div>
<ul id="repo-list"></ul>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('github-form').addEventListener('submit', function(e) {
e.preventDefault();
const username = document.getElementById('username').value;
fetch(`https://api.github.com/users/${username}/repos`)
.then(response => {
if (!response.ok) {
throw new Error(`Failed to fetch data: ${response.status} - ${response.statusText}`);
}
return response.json();
})
.then(data => {
const repoList = document.getElementById('repo-list');
repoList.innerHTML = '';
if (data.length === 0) {
const listItem = document.createElement('li');
listItem.textContent = 'No repositories found for this user.';
repoList.appendChild(listItem);
} else {
data.forEach(repo => {
const listItem = document.createElement('li');
const link = document.createElement('a');
link.href = repo.html_url;
link.target = '_blank';
link.textContent = repo.name;
listItem.appendChild(link);
repoList.appendChild(listItem);
});
}
})
.catch(error => {
const repoList = document.getElementById('repo-list');
repoList.innerHTML = '';
const errorItem = document.createElement('li');
errorItem.textContent = error.message;
repoList.appendChild(errorItem);
});
});
});
</script>
</body>
</html>