Skip to content

Commit c44abf8

Browse files
authored
Add files via upload
1 parent 217b4f5 commit c44abf8

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

DPXGithubRating.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
(async () => {
2+
3+
const repository = document.currentScript.getAttribute('repository');
4+
const icon_star = document.currentScript.getAttribute('star') ?? null;
5+
const text = document.currentScript.getAttribute('text') ?? null;
6+
7+
if (repository) {
8+
9+
let request = await fetch(`https://api.github.com/repos/${ repository }`);
10+
11+
try {
12+
13+
let data = await request.json();
14+
15+
if (document.querySelector(".dpxgithubrating")) {
16+
17+
let elements;
18+
19+
if (document.querySelector(`.dpxgithubrating[repository='${ repository }']`)) {
20+
21+
elements = document.querySelectorAll(`.dpxgithubrating[repository='${ repository }']`);
22+
23+
}else{
24+
25+
elements = document.querySelectorAll(`.dpxgithubrating`);
26+
27+
}
28+
29+
for (let i = 0; i < elements.length; i++) {
30+
31+
elements[i].appendChild(DPXCreateGithubRatingElement(data.html_url, data.stargazers_count, data.full_name, text, icon_star));
32+
33+
}
34+
35+
}else{
36+
37+
document.body.appendChild(DPXCreateGithubRatingElement(data.html_url, data.stargazers_count, data.full_name, text, icon_star));
38+
39+
}
40+
41+
}catch(e) {
42+
43+
console.log("DPXGithubRating Error : ", e);
44+
45+
}
46+
47+
}
48+
49+
})();
50+
51+
const DPXCreateGithubRatingElement = (repository_url, stargazers_count, title, text = null, star = null) => {
52+
53+
let anchor = document.createElement('a');
54+
anchor.href = repository_url;
55+
anchor.title = title;
56+
anchor.target = "_blank";
57+
anchor.rel = "noopener";
58+
anchor.classList.add('DPXGithubRatingButton');
59+
60+
if (star) {
61+
62+
let i = document.createElement('i');
63+
i.classList.add(star);
64+
anchor.appendChild(i);
65+
66+
}
67+
68+
if (text) {
69+
70+
let span = document.createElement('span');
71+
span.innerText = text;
72+
anchor.appendChild(span);
73+
74+
}
75+
76+
let b = document.createElement('b');
77+
b.innerText = stargazers_count;
78+
anchor.appendChild(b);
79+
80+
return anchor;
81+
82+
};

0 commit comments

Comments
 (0)