GitHub Show Repo Issues

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

目前為 2017-03-25 提交的版本,檢視 最新版本

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