-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsharex.downloads.js
125 lines (109 loc) · 4.56 KB
/
sharex.downloads.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
$(document).ready(function() {
let repo = GetParameterByName("repo");
if (!repo) {
repo = "ShareX/ShareX";
}
let repoInput = $("#downloads-repo");
repoInput.val(repo);
repoInput.keyup(function(e) {
if (e.keyCode == 13) {
repo = repoInput.val();
GetReleases(repo);
if (repo) {
SetParameter("repo", repo);
}
else {
ClearParameters();
}
}
});
GetReleases(repo);
});
async function GetReleases(repo) {
if (!repo) {
repo = "ShareX/ShareX";
}
$(".table-downloads tbody tr").remove();
$(".total-downloads").hide();
$(".total-downloads-value").text("");
$(".table-downloads").hide();
$(".fa-spin").show();
let perPage = 100;
let page = 1;
let totalDownloadCount = 0;
let previousPublishedAt = new Date();
let latest = true;
while (true) {
let response = await fetch(`https://api.github.com/repos/${repo}/releases?per_page=${perPage}&page=${page}`);
if (!response.ok) break;
let releases = await response.json();
if (releases.length > 0) {
releases = releases.sort((a, b) => new Date(b.published_at) - new Date(a.published_at));
for (let i = 0; i < releases.length; i++) {
let release = releases[i];
if (release.assets.length === 0) {
continue;
}
let assets = release.assets.sort((a, b) => b.download_count - a.download_count || a.name.localeCompare(b.name));
let downloadCount = 0;
let releaseInfo = `
<div class="downloads-asset-info">
<a href="${release.html_url}">
<div>
${EscapeHtml(release.name)}
<span class="downloads-badge"><i class="fa-solid fa-tag"></i>${EscapeHtml(release.tag_name)}</span>
</div>
</a>
</div>
`;
for (let i2 = 0; i2 < assets.length; i2++) {
let asset = assets[i2];
downloadCount += asset.download_count;
releaseInfo += `
<div class="downloads-asset-info">
<a href="${asset.browser_download_url}">
<div>
${EscapeHtml(asset.name)}
<span class="downloads-badge"><i class="fa-solid fa-file"></i>${FormatBytes(asset.size, 2)}</span>
<span class="downloads-badge"><i class="fa-solid fa-arrow-down"></i>${asset.download_count.toLocaleString()}</span>
</div>
</a>
</div>
`;
}
totalDownloadCount += downloadCount;
let publishedAt = new Date(release.published_at);
let activeDays = (previousPublishedAt - publishedAt) / (1000 * 60 * 60 * 24);
previousPublishedAt = publishedAt;
$(".table-downloads tbody").append(`
<tr class="downloads-release-info collapsed" data-toggle="collapse" data-target="#collapse${release.id}">
<td>
<i class="fa fa-fw"></i>${EscapeHtml(release.name)}
${latest ? '<span class="badge badge-success">Latest</span>' : ""}
${release.prerelease ? '<span class="badge badge-warning">Pre-release</span>' : ""}
</td>
<td>${publishedAt.toLocaleDateString("en-CA")}</td>
<td>${activeDays.toFixed(1)}</td>
<td>${downloadCount.toLocaleString()}</td>
</tr>
<tr class="downloads-assets">
<td colspan="100%">
<div class="collapse" id="collapse${release.id}" data-parent=".table-downloads">
<div class="downloads-assets-container">
${releaseInfo}
</div>
</div>
</td>
</tr>
`);
latest = false;
}
}
$(".total-downloads-value").text(totalDownloadCount.toLocaleString());
$(".total-downloads").fadeIn();
$(".table-downloads").fadeIn();
if (releases.length < perPage) break;
page++;
}
$(".fa-spin").hide();
}