-
Notifications
You must be signed in to change notification settings - Fork 0
/
DPXGithubRating.js
82 lines (48 loc) · 2.05 KB
/
DPXGithubRating.js
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
(async () => {
const repository = document.currentScript.getAttribute('repository');
const icon_star = document.currentScript.getAttribute('star') ?? null;
const text = document.currentScript.getAttribute('text') ?? null;
if (repository) {
let request = await fetch(`https://api.github.com/repos/${ repository }`);
try {
let data = await request.json();
if (document.querySelector(".dpxgithubrating")) {
let elements;
if (document.querySelector(`.dpxgithubrating[repository='${ repository }']`)) {
elements = document.querySelectorAll(`.dpxgithubrating[repository='${ repository }']`);
}else{
elements = document.querySelectorAll(`.dpxgithubrating`);
}
for (let i = 0; i < elements.length; i++) {
elements[i].appendChild(DPXCreateGithubRatingElement(data.html_url, data.stargazers_count, data.full_name, text, icon_star));
}
}else{
document.body.appendChild(DPXCreateGithubRatingElement(data.html_url, data.stargazers_count, data.full_name, text, icon_star));
}
}catch(e) {
console.log("DPXGithubRating Error : ", e);
}
}
})();
const DPXCreateGithubRatingElement = (repository_url, stargazers_count, title, text = null, star = null) => {
let anchor = document.createElement('a');
anchor.href = repository_url;
anchor.title = title;
anchor.target = "_blank";
anchor.rel = "noopener";
anchor.classList.add('DPXGithubRatingButton');
if (star) {
let i = document.createElement('i');
i.classList.add(star);
anchor.appendChild(i);
}
if (text) {
let span = document.createElement('span');
span.innerText = text;
anchor.appendChild(span);
}
let b = document.createElement('b');
b.innerText = stargazers_count;
anchor.appendChild(b);
return anchor;
};