🏠 Home 

GitHubReleasesCount

To show download counter for each attachment from Releases page on GitHub.com


Installer dette script?
  1. // ==UserScript==
  2. // @name GitHubReleasesCount
  3. // @description To show download counter for each attachment from Releases page on GitHub.com
  4. // @namespace net.r_eg.GitHubReleasesCount
  5. // @version 0.4
  6. // @grant none
  7. // @include https://github.com/*/*/releases*
  8. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
  9. // @author https://github.com/3F
  10. // @license MIT
  11. // ==/UserScript==
  12. /*
  13. Here's sandbox as the main page of this script at this time:
  14. - https://github.com/3F/sandbox/tree/master/javascript/_user_scripts/GitHubReleasesCount
  15. Tested via:
  16. - Firefox 61.0.2 + Greasemonkey 4.6
  17. - Firefox 59.0.2 + Greasemonkey 4.3
  18. - Firefox 55.0.3 & 54.0.1 + Greasemonkey 3.11
  19. Changes:
  20. * 0.4: Fixed the work with modern GitHub Releases ~ Aug 2018
  21. * 0.3: Fixed the work with modern GitHub Releases ~(Jan 2018 - Apr 2018)
  22. * 0.2: individual tags & multi-pages support (+async).
  23. * 0.1: first idea.
  24. */
  25. (function()
  26. {
  27. 'use strict';
  28. class GhrCounter extends function(){ this.constructor.prototype.$ =
  29. {
  30. /** assets from the releases page */
  31. pageFiles: ".release-header + :first-of-type li a",
  32. /** 'user/project' from url */
  33. usrprj: /^\/([^/]+)\/([^/]+)/g,
  34. apiserver: 'api.github.com',
  35. out: {
  36. class: 'Label Label--outline Label--outline-green text-gray',
  37. style: 'margin-right: 3px;',
  38. },
  39. debug: false,
  40. /* --------------- */ }}{ /* --------------- */
  41. showCount(elem, count)
  42. {
  43. let _= this;
  44. if(_.isNull(elem) || _.isNull(count)) {
  45. throw new GhrcNullException('elem', 'count');
  46. }
  47. elem.prepend("<span class='" + _.$.out.class + "' style='" + _.$.out.style
  48. + "'>" + count + "</span>");
  49. }
  50. process()
  51. {
  52. let _= this;
  53. _.dbg('started for: ' + location.pathname);
  54. let url = _.getInfoAPI();
  55. _.dbg('get info: ' + url);
  56. $.get(url, function(apidata)
  57. {
  58. $(_.$.pageFiles).each(function()
  59. {
  60. let root = $(this);
  61. let durl = root.attr('href');
  62. if(durl.indexOf('releases/download') == -1) {
  63. return true; // means 'continue' statement
  64. }
  65. for(let idx in apidata)
  66. for(let asset in apidata[idx].assets)
  67. {
  68. let lnk = apidata[idx].assets[asset];
  69. if(!lnk.browser_download_url.endsWith(durl)) {
  70. continue;
  71. };
  72. _.dbg('insert data for #' + lnk.id + ': ' + durl);
  73. _.showCount(root, lnk.download_count);
  74. }
  75. });
  76. });
  77. }
  78. constructor(debug)
  79. {
  80. super();
  81. this.$.debug = debug;
  82. this.process();
  83. }
  84. getInfoAPI()
  85. {
  86. let l = this.$.usrprj.exec(location.pathname);
  87. return location.protocol
  88. + '//' + this.$.apiserver + '/repos/' + l[1] + '/' + l[2] + '/releases';
  89. }
  90. dbg(msg, ...args)
  91. {
  92. if(!this.$.debug) {
  93. return;
  94. }
  95. let stamp = new Date().toISOString().substr(11, 12) + '] ';
  96. if(this.isNull(args) || args.length < 1) {
  97. console.log(stamp + msg);
  98. }
  99. else {
  100. console.log(stamp + msg, args);
  101. }
  102. }
  103. isNull(val)
  104. {
  105. return val == null || val === undefined;
  106. }
  107. };
  108. class GhrcException
  109. {
  110. constructor(message, arg)
  111. {
  112. this.message = message;
  113. this.arg = arg;
  114. }
  115. }
  116. class GhrcNullException extends GhrcException
  117. {
  118. constructor(...args)
  119. {
  120. super("'" + args + "' cannot be null.", null);
  121. }
  122. }
  123. // ...
  124. let ghrс = new GhrCounter(false);
  125. // when async loading
  126. $(window).bind('pjax:success', function() { ghrс.process(); });
  127. })();