[New] GitHub Downloads Counter

Counts the total downloads of all published releases in a Search Result and in Repository view.

  1. // ==UserScript==
  2. // @name [New] GitHub Downloads Counter
  3. // @namespace https://greasyfork.org/users/1162863
  4. // @version 1.0
  5. // @description Counts the total downloads of all published releases in a Search Result and in Repository view.
  6. // @author Andrewblood
  7. // @match *://*.github.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  9. // @grant GM_xmlhttpRequest
  10. // @connect api.github.com
  11. // @license Andrewblood
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // Here your Access Token if you have one
  18. const accessToken = ''; // 'ghp_your_access_token_here'
  19.  
  20. setTimeout(function() {
  21. getRepoDownloads();
  22. processSearchResults();
  23. }, 1000 * 3);
  24.  
  25. // Funktion zum Abrufen der Downloads über die GitHub API
  26. function getDownloads(repoPath, callback) {
  27. const apiUrl = `https://api.github.com/repos/${repoPath}/releases`;
  28. let totalDownloads = 0;
  29.  
  30. // API-Anfrage senden
  31. GM_xmlhttpRequest({
  32. method: "GET",
  33. url: apiUrl,
  34. headers: accessToken ? { 'Authorization': `token ${accessToken}` } : {}, // Token hinzufügen, falls vorhanden
  35. onload: function(res) {
  36. if (res.status === 200) {
  37. const releases = JSON.parse(res.responseText);
  38.  
  39. // Downloads zählen
  40. releases.forEach(release => {
  41. release.assets.forEach(asset => {
  42. totalDownloads += asset.download_count || 0; // Zähle die Downloads
  43. });
  44. });
  45.  
  46. // Rückgabe der Gesamtzahl der Downloads
  47. callback(totalDownloads);
  48. } else {
  49. console.error(`Error fetching downloads: ${res.status}`); // Fehlermeldung
  50. callback(0); // Rückgabe 0 bei Fehler
  51. }
  52. }
  53. });
  54. }
  55.  
  56. // Funktion zum Anzeigen der Gesamtzahl der Downloads für jedes Repository
  57. function displayTotalDownloads(repoElement, total) {
  58. const totalElement = document.createElement('div');
  59. totalElement.textContent = `Total Downloads: ${total}`; // Anzeige der Gesamtzahl
  60. totalElement.style.marginTop = '5px';
  61. totalElement.style.fontWeight = 'bold';
  62. repoElement.appendChild(totalElement);
  63. }
  64.  
  65. // Funktion zum Verarbeiten der Suchergebnisse
  66. function processSearchResults() {
  67. const repoElements = document.querySelectorAll('.Box-sc-g0xbh4-0.iwUbcA');
  68. repoElements.forEach(repo => {
  69. const repoLink = repo.querySelector('.prc-Link-Link-85e08'); // Link zum Repository
  70. if (repoLink) {
  71. const repoPath = repoLink.href.replace('https://github.com/', ''); // Repository-Pfad
  72.  
  73. // Downloads abrufen und anzeigen
  74. getDownloads(repoPath, function(totalDownloads) {
  75. displayTotalDownloads(repo, totalDownloads); // Gesamtzahl anzeigen
  76. });
  77. }
  78. });
  79. }
  80.  
  81. // Funktion zum Abrufen der Downloads über die GitHub API
  82. function getRepoDownloads() {
  83. const header = document.querySelector('#repo-title-component');
  84. const repoPath = window.location.pathname.replace('/releases', '');
  85. const apiUrl = `https://api.github.com/repos${repoPath}/releases`;
  86. let totalDownloads = 0;
  87. if (header) {
  88.  
  89. // API-Anfrage senden
  90. GM_xmlhttpRequest({
  91. method: "GET",
  92. url: apiUrl,
  93. onload: function(res) {
  94. if (res.status === 200) {
  95. const releases = JSON.parse(res.responseText);
  96.  
  97. // Downloads zählen
  98. releases.forEach(release => {
  99. release.assets.forEach(asset => {
  100. totalDownloads += asset.download_count || 0; // Zähle die Downloads
  101. });
  102. });
  103.  
  104. const totalElement = document.createElement('div');
  105. totalElement.textContent = `Total Downloads: ${totalDownloads}`; // Anzeige der Gesamtzahl
  106. totalElement.style.marginLeft = '10px';
  107. header.appendChild(totalElement);
  108. } else {
  109. console.error(`Error: ${res.status}`);
  110. }
  111. }
  112. });
  113. }
  114. }
  115.  
  116. })();