Greasy Fork is available in English.

GitHub Issue Counts

A userscript that adds a repo issues count to the repository tab & organization page (https://github.com/:user)

  1. // ==UserScript==
  2. // @name GitHub Issue Counts
  3. // @version 3.0.20
  4. // @description A userscript that adds a repo issues count to the repository tab & organization page (https://github.com/:user)
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @match https://github.com/*
  9. // @run-at document-idle
  10. // @grant GM_addStyle
  11. // @grant GM_xmlhttpRequest
  12. // @connect api.github.com
  13. // @require https://greatest.deepsurf.us/scripts/28721-mutations/code/mutations.js?version=1108163
  14. // @icon https://github.githubassets.com/pinned-octocat.svg
  15. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  16. // ==/UserScript==
  17.  
  18. (() => {
  19. "use strict";
  20.  
  21. // issue count = get all repos from user => api v3
  22. // https://api.github.com/users/:user/repos
  23. // then look for "open_issues_count" in the named repos
  24. const api = "https://api.github.com/users",
  25. // bug icon
  26. icon = `<svg class="octicon octicon-repo-issues" xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 53 53">
  27. <path d="m39.2 0c2 0 3.7 1.5 3.7 3.4 0 1.9-1.7 3.4-3.7 3.4l-0.9-0.1-3.4 4c2.3 1.6 4.4 3.8 6 6.4-4 1.2-8.9 1.9-14.2 1.9-5.3 0-10.1-0.7-14.2-1.9 1.5-2.6 3.5-4.7 5.7-6.3l-3.5-4.1c-0.2 0-0.4 0.1-0.6 0.1-2 0-3.7-1.5-3.7-3.4 0-1.9 1.7-3.4 3.7-3.4 2 0 3.7 1.5 3.7 3.4 0 0.7-0.2 1.3-0.6 1.8l3.5 4.2c1.8-0.8 3.8-1.3 5.9-1.3 2 0 3.9 0.4 5.6 1.2l3.7-4.3c-0.3-0.5-0.4-1-0.4-1.6 0-1.9 1.6-3.4 3.7-3.4zm11.8 28.5c1.2 0 2.2 0.9 2.2 2 0 1.1-1 2-2.2 2l-6.7 0c-0.1 1.5-0.3 2.9-0.6 4.3l7.8 3.4c1.1 0.5 1.6 1.7 1.1 2.7-0.5 1-1.8 1.5-2.9 1l-7.2-3.1c-2.7 6.7-8 11.4-14.3 12.1l0-31.2c5.2-0.1 10-1 13.9-2.3l0.3 0.7 7.5-2.7c1.1-0.4 2.4 0.1 2.9 1.2 0.4 1.1-0.1 2.2-1.3 2.6l-7.9 2.8c0.3 1.4 0.6 2.9 0.7 4.5l6.7 0 0 0zm-48.7 0 6.7 0c0.1-1.5 0.3-3.1 0.7-4.5l-7.9-2.8c-1.1-0.4-1.7-1.6-1.3-2.6 0.4-1 1.7-1.6 2.9-1.2l7.5 2.7 0.3-0.7c3.9 1.3 8.7 2.1 13.9 2.3l0 31.2c-6.2-0.7-11.5-5.4-14.3-12.1l-7.2 3.1c-1.1 0.5-2.4 0-2.9-1-0.5-1 0-2.2 1.1-2.7l7.8-3.4c-0.3-1.4-0.5-2.8-0.6-4.3l-6.7 0c-1.2 0-2.2-0.9-2.2-2 0-1.1 1-2 2.2-2z" />
  28. </svg>`,
  29.  
  30. repoSelectors = `
  31. #user-repositories-list li,
  32. #org-repositories li,
  33. ol.pinned-repos-list li
  34. `;
  35.  
  36. // add bug image styling
  37. GM_addStyle(`
  38. .repo-list-stats a.issues svg {
  39. position: relative;
  40. top: 2px;
  41. fill: #888;
  42. }
  43. .repo-list-stats a.issues:hover svg {
  44. fill: #4078C0;
  45. }
  46. `);
  47.  
  48. /*
  49. * Org repos
  50. * container = div#org-repositories > div > div.org-repos.repo-list > li
  51. * User repos
  52. * container = div#user-repositories-list > div.js-repo-list > li
  53. * Common org/user
  54. * repo url = container h3 a (first a)
  55. * issue link location = container div[3] a[last]:after
  56. * fork link HTML - both user/org (Dec 2016)
  57. * <a class="muted-link tooltipped tooltipped-s mr-3" href="/:user/:repo/network" aria-label="Forks">
  58. * <svg class="octicon octicon-repo-forked">...</svg> :fork-count
  59. * </a>
  60. *
  61. * Pinned repos
  62. * container = ol.pinned-repos-list li.pinned-repo-item
  63. * repo url = container span span a (first a)
  64. * issue link location = container > span.pinned-repo-item-content p[last] a[last]:after
  65. * fork link HTML
  66. * <a href="/:user/:repo/network" class="pinned-repo-meta muted-link">
  67. * <svg aria-label="forks" class="octicon octicon-repo-forked">...</svg> :fork-count
  68. * </a>
  69. */
  70. function addLinks(data, repos) {
  71. repos.forEach(repo => {
  72. let wrapper, el, html, setClass;
  73. const url = ($("a", repo).getAttribute("href") || "").slice(1),
  74. result = url && data.find(item => {
  75. return item.full_name === url;
  76. });
  77. // pinned
  78. if (repo.classList.contains("pinned-repo-item")) {
  79. el = $$(".pinned-repo-item-content p:last-child", repo);
  80. setClass = "muted-link ghic2-issue-link";
  81. } else {
  82. // user/org list = last div in repo list contains links
  83. wrapper = $$("div", repo);
  84. el = wrapper && $$("a", wrapper[wrapper.length - 1]);
  85. setClass = "muted-link tooltipped tooltipped-s mr-3 ghic2-issue-link";
  86. }
  87. if (el) {
  88. if (result && typeof result.open_issues_count === "number") {
  89. html = `<a class="${setClass}" href="/${url}/issues" aria-label="Issues">
  90. ${icon} ${result.open_issues_count}
  91. </a>`;
  92. // target the last "a"
  93. el = el[el.length - 1];
  94. // add after last link, sometimes there is no fork
  95. if (el) {
  96. if (el.tagName === "P") {
  97. wrapper = document.createElement("span");
  98. wrapper.className = "ghic-link pinned-repo-meta";
  99. el.appendChild(wrapper);
  100. el.querySelector(".ghic-link").innerHTML = html;
  101. } else {
  102. el.insertAdjacentHTML("afterend", html);
  103. }
  104. }
  105. }
  106. }
  107. });
  108. }
  109.  
  110. function addIssues() {
  111. let user, url,
  112. repos = $$(repoSelectors);
  113. if (
  114. // look for user overview, user repositories & organization repo page
  115. repos.length &&
  116. // and not already applied
  117. !$$(".ghic2-issue-link").length
  118. ) {
  119. // no issue count for non-public & forks
  120. repos = repos.filter(repo => {
  121. let list = repo.classList;
  122. return list.contains("public") && !list.contains("fork");
  123. });
  124. if (repos.length) {
  125. url = $("a", repos[ 0 ]).getAttribute("href");
  126. user = (url || "").match(/^\/[^/]+/);
  127.  
  128. if (user && user.length) {
  129. GM_xmlhttpRequest({
  130. method : "GET",
  131. url : api + user[0] + "/repos",
  132. onload : response => {
  133. const data = JSON.parse(response.responseText || "null");
  134. if (data) {
  135. addLinks(data, repos);
  136. }
  137. }
  138. });
  139. }
  140. }
  141. }
  142. }
  143.  
  144. function $(str, el) {
  145. return (el || document).querySelector(str);
  146. }
  147.  
  148. function $$(str, el) {
  149. return Array.from((el || document).querySelectorAll(str));
  150. }
  151.  
  152. document.addEventListener("ghmo:container", addIssues);
  153. addIssues();
  154.  
  155. })();