Forked from kusaanko/Show download count of GitHub release for Tampermonkey.user.js
Last active
October 9, 2023 01:09
-
-
Save dscho/5d171ec286a52ca4c699477cceaebe20 to your computer and use it in GitHub Desktop.
Show download counts of GitHub releases (Tampermonkey script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Show download counts of GitHub releases | |
// @description Displays the download count of GitHub's release. | |
// @version 0.3 | |
// @source https://gist.github.com/dscho/5d171ec286a52ca4c699477cceaebe20 | |
// @updateURL https://gist.github.com/dscho/5d171ec286a52ca4c699477cceaebe20/raw/github-release-download-counts.user.js | |
// @downloadURL https://gist.github.com/dscho/5d171ec286a52ca4c699477cceaebe20/raw/github-release-download-counts.user.js | |
// @namespace http://tampermonkey.net/ | |
// @author Kusaanko, Johannes Schindelin | |
// @match https://github.com/*/*/releases* | |
// @grant none | |
// @require https://code.jquery.com/jquery-3.3.1.min.js | |
// ==/UserScript== | |
$(function() { | |
const match = location.href.match(/^https:\/\/github\.com\/([^/]+\/[^/]+)\//); | |
if (!match) return; | |
const downloadCounts = {}; | |
$.ajax({ | |
url: `https://api.github.com/repos/${match[1]}/releases`, | |
type: 'GET' | |
}).done((data) => { | |
for (const release of Object.values(data)) { | |
for (const asset of Object.values(release['assets'])) { | |
const fileName = asset['browser_download_url'].replace(/.*\//, ''); | |
downloadCounts[fileName] = asset['download_count']; | |
} | |
} | |
const updateSpans = () => { | |
$('span').each(function(i, elem) { | |
const count = downloadCounts[$(this).text()]; | |
if(count !== undefined) { | |
$(this).after(`<span style="color: #24292e; vertical-align: top; font-size: 70%"> ${count.toLocaleString()} downloads</span>`); | |
} | |
}); | |
} | |
updateSpans(); | |
new MutationObserver((events, observer) => { | |
events.map((event) => { | |
if (event.type === 'childList' && event.target && event.target.tagName === 'DIV') { | |
updateSpans(); | |
} | |
}) | |
}).observe(document, { attributes: true, childList: true, subtree: true }); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment