-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplified.js
More file actions
58 lines (58 loc) · 2.43 KB
/
simplified.js
File metadata and controls
58 lines (58 loc) · 2.43 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
// Made by Jgc7 (https://github.com/jgc777)
console.log("Simplified GithubRepoAPI (https://jgc777.github.io/GitHubRepoAPI) loaded");
const username = document.querySelector('meta[name="github-username"]').getAttribute('content').toLowerCase();
async function getRepos() {
if (username === "") {
console.error("No username provided!");
return ["Error: no username provided"];
}
response = await fetch(`https://api.github.com/users/${username}/repos`);
if (response.status === 403) {
console.error("API rate limit exceeded!");
return ["Error: you have exceeded the API rate limit!"];
} else if (response.ok) {
const repos = await response.json();
return repos;
} else {
console.error(`Couldn't fetch repos: ${response.status} ${response.statusText}`);
return [`Error: couldn't fetch repos (${response.status})`];
}
}
async function appendRepos() {
const repoList = await getRepos();
const repoListElement = document.getElementById('repo-list');
if (repoListElement) {
repoListElement.innerHTML = '';
repoList.forEach(repo => {
const listItem = document.createElement("li");
if (repo.name) {
if (repo.name.toLowerCase() !== username && repo.name.toLowerCase() !== `${username}.github.io`) {
const link = document.createElement("a");
link.href = repo.homepage ? repo.homepage: repo.has_pages ? `https://${repo.owner.login}.github.io/${repo.name}` : repo.html_url;
link.textContent = repo.name;
listItem.appendChild(link);
repoListElement.appendChild(listItem);
} else console.log(`Skipping the repo "${repo.name}"`);
} else {
const errorText = document.createElement("p");
errorText.textContent = repo;
listItem.appendChild(errorText);
repoListElement.appendChild(listItem);
return;
}
});
} else console.error(`Couldn't find the repo list element!`);
}
let attempts = 0;
const interval = setInterval(() => {
const repoListElement = document.getElementById('repo-list');
if (repoListElement && repoListElement.children.length === 0) {
appendRepos().then(() => {
clearInterval(interval);
console.log(`Appended the repo list (#${++attempts})!`);
}).catch(e => console.error(`Couldn't append the repo list: ${e} (#${++attempts})`));
} else if (attempts > 10) {
clearInterval(interval);
console.error(`Couldn't append the repo list after 10 attempts (closing)!`);
}
}, 1000);