GitHub Network Shortcut

Adds a shortcut to the network graph on the repository's main page

当前为 2025-02-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Network Shortcut
  3. // @namespace https://mkps.app/
  4. // @version 0.1
  5. // @description Adds a shortcut to the network graph on the repository's main page
  6. // @author MK
  7. // @match *://github.com/*
  8. // @icon https://github.com/favicon.ico
  9. // ==/UserScript==
  10.  
  11. (() => {
  12. "use strict";
  13.  
  14. const ELEMENT_CLASS = "MK_userscript_ghNetworkShortcut_element";
  15. const CONTAINER_SELECTOR_LG = `#repo-content-turbo-frame .Layout-sidebar .about-margin > div.BorderGrid-row > div.BorderGrid-cell > div.hide-sm.hide-md`;
  16. const CONTAINER_SELECTOR_SM = `#responsive-meta-container ul[aria-label="Repository details"]`;
  17.  
  18. function main() {
  19. forMatchingElementsForever(CONTAINER_SELECTOR_LG, (element) => {
  20. if (element.querySelector(`.${ELEMENT_CLASS}`)) {return;}
  21. for (const child of element.children) {
  22. if (child.children[0]?.getAttribute("href")?.endsWith("/forks")) {
  23. child.insertAdjacentElement("afterend", createShortcutLg());
  24. break;
  25. }
  26. }
  27. });
  28. forMatchingElementsForever(CONTAINER_SELECTOR_SM, (element) => {
  29. if (element.querySelector(`.${ELEMENT_CLASS}`)) {return;}
  30. for (const child of element.children) {
  31. if (child.getAttribute("href")?.endsWith("/activity")) {
  32. child.insertAdjacentElement("afterend", createShortcutSm());
  33. break;
  34. }
  35. }
  36. });
  37. }
  38.  
  39. function createShortcutLg() {
  40. const div = document.createElement("div");
  41. div.classList.add(ELEMENT_CLASS);
  42. div.classList.add("mt-2");
  43. const a = document.createElement("a");
  44. a.setAttribute("href", `${location.pathname}/network`);
  45. a.setAttribute("data-view-component", "true");
  46. a.classList.add("Link", "Link--muted");
  47. a.appendChild(createCommitIcon("mr-2"));
  48. a.appendChild(document.createTextNode(" "));
  49. const span = document.createElement("span");
  50. span.classList.add("color-fg-muted");
  51. span.textContent = "Network";
  52. a.appendChild(span);
  53. div.appendChild(a);
  54. return div;
  55. }
  56.  
  57. function createShortcutSm() {
  58. const a = document.createElement("a");
  59. a.classList.add(ELEMENT_CLASS, "Link--secondary", "no-underline", "d-block", "mr-2");
  60. a.setAttribute("role", "listitem");
  61. a.setAttribute("href", `${location.pathname}/network`);
  62. a.appendChild(createCommitIcon("mr-1"));
  63. a.appendChild(document.createTextNode(" "));
  64. const span = document.createElement("span");
  65. span.textContent = "Network";
  66. a.appendChild(span);
  67. return a;
  68. }
  69.  
  70. function createCommitIcon(marginClass) {
  71. const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  72. svg.setAttribute("version", "1.1");
  73. svg.setAttribute("width", "16");
  74. svg.setAttribute("height", "16");
  75. svg.setAttribute("viewBox", "0 0 16 16");
  76. svg.setAttribute("aria-hidden", "true");
  77. svg.setAttribute("data-view-component", "true");
  78. svg.classList.add("octicon", "octicon-commit", marginClass);
  79. svg.innerHTML = `<path d="M11.93 8.5a4.002 4.002 0 0 1-7.86 0H.75a.75.75 0 0 1 0-1.5h3.32a4.002 4.002 0 0 1 7.86 0h3.32a.75.75 0 0 1 0 1.5Zm-1.43-.75a2.5 2.5 0 1 0-5 0 2.5 2.5 0 0 0 5 0Z"></path>`;
  80. return svg;
  81. }
  82.  
  83. function forMatchingElementsForever(selector, fn) {
  84. const observer = new MutationObserver((list, observer) => {
  85. for (const mutation of list) {
  86. for (const addedNode of mutation.addedNodes) {
  87. if (addedNode.nodeType !== Node.ELEMENT_NODE) {continue;}
  88. for (const newNode of addedNode.querySelectorAll(selector)) {
  89. fn(newNode);
  90. }
  91. }
  92. }
  93. });
  94. observer.observe(document.body, {childList: true, subtree: true});
  95.  
  96. for (const element of document.querySelectorAll(selector)) {
  97. fn(element);
  98. }
  99. }
  100.  
  101. main();
  102. })();