GitHub Show Repo Issues

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

当前为 2017-04-14 提交的版本,查看 最新版本

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