Skip to content

Instantly share code, notes, and snippets.

@iusmac
Last active June 15, 2024 04:05
Show Gist options
  • Save iusmac/77066a9ea6829b3b6c0e4005868c8d50 to your computer and use it in GitHub Desktop.
Save iusmac/77066a9ea6829b3b6c0e4005868c8d50 to your computer and use it in GitHub Desktop.
Print GitHub release download count per each release found on the "Releases" page.
let getReleaseTag = () => {
let url = document.URL;
const delim = '/releases/tag/';
if (url.includes(delim)) {
return url.split(delim)[1];
}
};
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
switch (xmlHttp.readyState) {
case 0: // UNINITIALIZED
case 1: // LOADING
case 2: // LOADED
case 3: // INTERACTIVE
break;
case 4: // COMPLETED
const releases = JSON.parse(xmlHttp.responseText);
const tag = getReleaseTag();
for (var i in releases) {
for (var j in releases[i].assets) {
if (tag && releases[i].tag_name != tag) {
continue
}
console.log(releases[i].assets[j].download_count,
decodeURI(releases[i].assets[j].name));
}
}
break;
default:
console.error('GitHub API request failed.');
}
};
let setOriginToAPIUrl = (loc) => loc.replace('//github.com', '//api.github.com/repos');
let url = document.URL;
let base = window.location.pathname.split("/");
let owner = base[1];
let repo = base[2];
url = [setOriginToAPIUrl(window.location.origin), owner, repo, "releases"];
xmlHttp.open('GET', url.join("/"), true);
xmlHttp.send(null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment