GitHub Issue Counts

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

目前为 2021-07-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Issue Counts
  3. // @version 3.0.19
  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. // @include https://github.com/*
  9. // @run-at document-idle
  10. // @grant GM_addStyle
  11. // @grant GM_xmlhttpRequest
  12. // @connect api.github.com
  13. // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=952601
  14. // @icon https://github.githubassets.com/pinned-octocat.svg
  15. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  16. // ==/UserScript==
  17. (() => {
  18. "use strict";
  19.  
  20. // issue count = get all repos from user => api v3
  21. // https://api.github.com/users/:user/repos
  22. // then look for "open_issues_count" in the named repos
  23. const api = "https://api.github.com/users",
  24. // bug icon
  25. icon = `<svg class="octicon octicon-repo-issues" xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 53 53">
  26. <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" />
  27. </svg>`,
  28.  
  29. repoSelectors = `
  30. #user-repositories-list li,
  31. #org-repositories li,
  32. ol.pinned-repos-list li
  33. `;
  34.  
  35. // add bug image styling
  36. GM_addStyle(`
  37. .repo-list-stats a.issues svg {
  38. position: relative;
  39. top: 2px;
  40. fill: #888;
  41. }
  42. .repo-list-stats a.issues:hover svg {
  43. fill: #4078C0;
  44. }
  45. `);
  46.  
  47. /*
  48. * Org repos
  49. * container = div#org-repositories > div > div.org-repos.repo-list > li
  50. * User repos
  51. * container = div#user-repositories-list > div.js-repo-list > li
  52. * Common org/user
  53. * repo url = container h3 a (first a)
  54. * issue link location = container div[3] a[last]:after
  55. * fork link HTML - both user/org (Dec 2016)
  56. * <a class="muted-link tooltipped tooltipped-s mr-3" href="/:user/:repo/network" aria-label="Forks">
  57. * <svg class="octicon octicon-repo-forked">...</svg> :fork-count
  58. * </a>
  59. *
  60. * Pinned repos
  61. * container = ol.pinned-repos-list li.pinned-repo-item
  62. * repo url = container span span a (first a)
  63. * issue link location = container > span.pinned-repo-item-content p[last] a[last]:after
  64. * fork link HTML
  65. * <a href="/:user/:repo/network" class="pinned-repo-meta muted-link">
  66. * <svg aria-label="forks" class="octicon octicon-repo-forked">...</svg> :fork-count
  67. * </a>
  68. */
  69. function addLinks(data, repos) {
  70. repos.forEach(repo => {
  71. let wrapper, el, html, setClass;
  72. const url = ($("a", repo).getAttribute("href") || "").slice(1),
  73. result = url && data.find(item => {
  74. return item.full_name === url;
  75. });
  76. // pinned
  77. if (repo.classList.contains("pinned-repo-item")) {
  78. el = $$(".pinned-repo-item-content p:last-child", repo);
  79. setClass = "muted-link ghic2-issue-link";
  80. } else {
  81. // user/org list = last div in repo list contains links
  82. wrapper = $$("div", repo);
  83. el = wrapper && $$("a", wrapper[wrapper.length - 1]);
  84. setClass = "muted-link tooltipped tooltipped-s mr-3 ghic2-issue-link";
  85. }
  86. if (el) {
  87. if (result && typeof result.open_issues_count === "number") {
  88. html = `<a class="${setClass}" href="/${url}/issues" aria-label="Issues">
  89. ${icon} ${result.open_issues_count}
  90. </a>`;
  91. // target the last "a"
  92. el = el[el.length - 1];
  93. // add after last link, sometimes there is no fork
  94. if (el) {
  95. if (el.tagName === "P") {
  96. wrapper = document.createElement("span");
  97. wrapper.className = "ghic-link pinned-repo-meta";
  98. el.appendChild(wrapper);
  99. el.querySelector(".ghic-link").innerHTML = html;
  100. } else {
  101. el.insertAdjacentHTML("afterend", html);
  102. }
  103. }
  104. }
  105. }
  106. });
  107. }
  108.  
  109. function addIssues() {
  110. let user, url,
  111. repos = $$(repoSelectors);
  112. if (
  113. // look for user overview, user repositories & organization repo page
  114. repos.length &&
  115. // and not already applied
  116. !$$(".ghic2-issue-link").length
  117. ) {
  118. // no issue count for non-public & forks
  119. repos = repos.filter(repo => {
  120. let list = repo.classList;
  121. return list.contains("public") && !list.contains("fork");
  122. });
  123. if (repos.length) {
  124. url = $("a", repos[ 0 ]).getAttribute("href");
  125. user = (url || "").match(/^\/[^/]+/);
  126.  
  127. if (user && user.length) {
  128. GM_xmlhttpRequest({
  129. method : "GET",
  130. url : api + user[0] + "/repos",
  131. onload : response => {
  132. const data = JSON.parse(response.responseText || "null");
  133. if (data) {
  134. addLinks(data, repos);
  135. }
  136. }
  137. });
  138. }
  139. }
  140. }
  141. }
  142.  
  143. function $(str, el) {
  144. return (el || document).querySelector(str);
  145. }
  146.  
  147. function $$(str, el) {
  148. return Array.from((el || document).querySelectorAll(str));
  149. }
  150.  
  151. document.addEventListener("ghmo:container", addIssues);
  152. addIssues();
  153.  
  154. })();