GitHub Diff Links

A userscript that adds links to diff and pull request headers to jump back & forth between files

目前为 2023-07-01 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Diff Links
  3. // @version 1.2.18
  4. // @description A userscript that adds links to diff and pull request headers to jump back & forth between files
  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. // @require https://gf.qytechs.cn/scripts/28721-mutations/code/mutations.js?version=1108163
  12. // @require https://gf.qytechs.cn/scripts/398877-utils-js/code/utilsjs.js?version=1079637
  13. // @icon https://github.githubassets.com/pinned-octocat.svg
  14. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  15. // ==/UserScript==
  16. /* global $ $$ on */
  17. (() => {
  18. "use strict";
  19.  
  20. // sometimes tooltips are too narrow...
  21. // and move diff anchors below sticky header
  22. GM_addStyle(`
  23. .gh-diff-links:after { min-width: 120px; }
  24. a[name*="diff-"]:before {
  25. padding-top: 60px !important;
  26. margin-top: -60px !important;
  27. content: "";
  28. position: absolute;
  29. }
  30. div.file-actions > div {
  31. display: inline;
  32. }`
  33. );
  34.  
  35. const button = document.createElement("a"),
  36. // button [ InnerHTML, tooltip ]
  37. nextBtn = ["Next", "Jump to next file\n("],
  38. prevBtn = ["Prev", "Jump to previous file\n(" ];
  39.  
  40. button.className = "btn btn-sm tooltipped tooltipped-s tooltipped-multiline gh-diff-links";
  41. button.setAttribute("rel", "nofollow");
  42.  
  43. function addButton(el, content, link) {
  44. let btn = button.cloneNode(),
  45. txt = el.classList.contains("select-menu-item") ?
  46. $(".description", el).textContent :
  47. link.textContent || "";
  48. // clean up whitespace
  49. txt = txt.replace(/\s+/g, " ").trim();
  50. // only add file name to tooltip
  51. txt = txt.substring(txt.lastIndexOf("/") + 1, txt.length);
  52. btn.innerHTML = content[0];
  53. btn.setAttribute("aria-label", content[1] + txt + ")" );
  54. btn.href = link.hash;
  55. // prepend button
  56. el.insertBefore(btn, el.childNodes[0]);
  57. }
  58.  
  59. function addSpace(el, content) {
  60. let btn = button.cloneNode();
  61. btn.disabled = true;
  62. btn.className = "btn btn-sm gh-diff-links disabled";
  63. btn.innerHTML = content[0];
  64. el.insertBefore(btn, el.childNodes[0]);
  65. }
  66.  
  67. function addLinks() {
  68. let last, temp,
  69. links = $$(".file-header .file-info a");
  70. if (links.length) {
  71. // links & file-actions "should" be the same length
  72. last = links.length - 1;
  73. $$(".file-actions").forEach((wrap, indx) => {
  74. const el = wrap.firstElementChild;
  75. // remove disabled buttons added before progressive
  76. // content has completed loading
  77. temp = $(".gh-diff-links.disabled", el);
  78. if (temp) {
  79. temp.parentNode.removeChild(temp);
  80. // remove both buttons to allow updating
  81. temp = $(".gh-diff-links", el);
  82. temp.parentNode.removeChild(temp);
  83. }
  84. if (!$(".gh-diff-links", el)) {
  85. if (indx === 0) {
  86. addButton(el, nextBtn, links[indx + 1]);
  87. addSpace(el, prevBtn);
  88. } else if (indx === last) {
  89. // add dummy "next" button to keep spacing
  90. addSpace(el, nextBtn);
  91. addButton(el, prevBtn, links[indx - 1]);
  92. } else {
  93. addButton(el, nextBtn, links[indx + 1]);
  94. addButton(el, prevBtn, links[indx - 1]);
  95. }
  96. }
  97. });
  98. }
  99. }
  100.  
  101. function init() {
  102. if ($("#files.diff-view") || $(".pr-toolbar")) {
  103. addLinks();
  104. }
  105. }
  106.  
  107. // DOM targets - to detect GitHub dynamic ajax page loading
  108. on(document, "ghmo:container", init);
  109. on(document, "ghmo:diff", addLinks);
  110. init();
  111.  
  112. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址