-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
79 lines (71 loc) · 2.67 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<html>
<head>
<meta http-equiv="refresh" content="600">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<script type="text/javascript">
var URL_REGEX = /(https?:\/\/[^ ]*)>/;
var STARGAZER_PER_PAGE = 30;
var FETCH_INIT = { cache: 'no-store' };
function setTotalStars(totalStars) {
document.getElementById("stars").innerHTML = totalStars;
}
function setRecentStargazer(gazers) {
var numbers = new Array("first", "second", "third", "fourth", "fith");
for (var i = 0; i < gazers.length; i++) {
document.getElementById(numbers[gazers.length - 1 - i] + "-login").innerHTML = gazers[i].login;
document.getElementById(numbers[gazers.length - 1 - i] + "-avatar").src = gazers[i].avatar_url;
}
}
function loadLastPage(lastPage, lastPageNum) {
fetch(lastPage, FETCH_INIT).then(function(response) {
return response.json()
}
).then(function(json) {
setTotalStars((lastPageNum-1) * STARGAZER_PER_PAGE + json.length);
setRecentStargazer(json.slice(-5));
console.log(json[json.length-1].login);
console.log(json[json.length-1].avatar_url);
}
).catch(function(error) {
console.log(error);
}
)
}
function processFirstPage(response) {
var link = response.headers.get("link");
var lastPage = link.split(",")[1].match(URL_REGEX)[1];
var lastPageNum = lastPage.split("?")[1].split("=")[1];
loadLastPage(lastPage, lastPageNum);
return response.json()
}
function onPageLoaded(event) {
var repo = window.location.href.split("?")[1];
document.getElementById("repo").innerHTML = repo;
var url = 'https://api.github.com/repos/' + repo + '/stargazers';
fetch(url, FETCH_INIT).then(processFirstPage)
.catch(function(error) {
console.log(error);
})
}
document.addEventListener("DOMContentLoaded", onPageLoaded);
</script>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<div id="box">
<div id="repo">...</div>
<div id="stars-wrapper">
<span id="star">★</span><span id="stars">...</span>
</div>
<div id="recent_stargazers">
<div class="stargazer"><img src="" id="first-avatar"></img><span id="first-login"></span></div>
<div class="stargazer"><img src="" id="second-avatar"></img><span id="second-login"></span></div>
<div class="stargazer"><img src="" id="third-avatar"></img><span id="third-login"></span></div>
<div class="stargazer"><img src="" id="fourth-avatar"></img><span id="fourth-login"></span></div>
<div class="stargazer"><img src="" id="fith-avatar"></img><span id="fith-login"></span></div>
</div>
</div>
</body>
</html>