GitHub Diff File Toggle

A userscript that adds a toggle to show or hide diff files

当前为 2018-11-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Diff File Toggle
  3. // @version 1.2.6
  4. // @description A userscript that adds a toggle to show or hide diff files
  5. // @license MIT
  6. // @author StylishThemes
  7. // @namespace https://github.com/StylishThemes
  8. // @include https://github.com/*
  9. // @run-at document-end
  10. // @grant GM_addStyle
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_registerMenuCommand
  14. // @require https://gf.qytechs.cn/scripts/28721-mutations/code/mutations.js?version=634242
  15. // @icon https://avatars3.githubusercontent.com/u/6145677?v=3&s=200
  16. // ==/UserScript==
  17. /* jshint esnext:true, unused:true */
  18. (() => {
  19. "use strict";
  20. /*
  21. This code is also part of the GitHub-Dark Script
  22. (https://github.com/StylishThemes/GitHub-Dark-Script)
  23. Extracted out into a separate userscript in case users only want to add this
  24. functionality
  25. */
  26. const icon =
  27. `<svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="10" height="6.5" viewBox="0 0 10 6.5">
  28. <path d="M0 1.5L1.5 0l3.5 3.7L8.5.0 10 1.5 5 6.5 0 1.5z"/>
  29. </svg>`;
  30.  
  31. // Add file diffs toggle
  32. function addFileToggle() {
  33. const files = $$("#files .file-actions");
  34. let button = document.createElement("button");
  35. let updated = false;
  36. button.type = "button";
  37. button.className = "ghd-file-toggle btn btn-sm tooltipped tooltipped-n";
  38. button.setAttribute("aria-label", "Click to Expand or Collapse file");
  39. button.setAttribute("tabindex", "-1");
  40. button.innerHTML = icon;
  41. files.forEach(el => {
  42. if (!$(".ghd-file-toggle", el)) {
  43. // hide GitHub toggle view button
  44. el.querySelector(".js-details-target").style.display = "none";
  45. el.appendChild(button.cloneNode(true));
  46. updated = true;
  47. }
  48. });
  49. // start with all but first entry collapsed
  50. if (updated && files.length) {
  51. if (/^t/.test(GM_getValue("accordion"))) {
  52. toggleFile({
  53. target: $(".ghd-file-toggle")
  54. }, "init");
  55. }
  56. }
  57. }
  58.  
  59. function toggleSibs(target, state) {
  60. const isCollapsed = state,
  61. toggles = $$(".file");
  62. let el,
  63. indx = toggles.length;
  64. while (indx--) {
  65. el = toggles[indx];
  66. if (el !== target) {
  67. el.classList.toggle("Details--on", isCollapsed);
  68. }
  69. }
  70. }
  71.  
  72. function toggleFile(event, init) {
  73. const accordion = GM_getValue("accordion"),
  74. el = event.target.closest(".file");
  75. if (el && accordion) {
  76. if (!init) {
  77. el.classList.toggle("Details--on");
  78. }
  79. toggleSibs(el, false);
  80. } else if (el) {
  81. el.classList.toggle("Details--on");
  82. // shift+click toggle all files!
  83. if (event.shiftKey) {
  84. toggleSibs(el, el.classList.contains("Details--on"));
  85. }
  86. }
  87. document.activeElement.blur();
  88. // move current open panel to the top
  89. if (el.classList.contains("Details--on")) {
  90. location.hash = el.id;
  91. }
  92. }
  93.  
  94. function addBindings() {
  95. $("body").addEventListener("click", event => {
  96. const target = event.target;
  97. if (target && target.classList.contains("ghd-file-toggle")) {
  98. toggleFile(event);
  99. return false;
  100. }
  101. });
  102. }
  103.  
  104. function $(str, el) {
  105. return (el || document).querySelector(str);
  106. }
  107.  
  108. function $$(str, el) {
  109. return [...(el || document).querySelectorAll(str)];
  110. }
  111.  
  112. // Don't initialize if GitHub Dark Script is active
  113. if (!$("#ghd-menu")) {
  114. GM_addStyle(`
  115. .Details--on .ghd-file-toggle svg {
  116. -webkit-transform:rotate(90deg); transform:rotate(90deg);
  117. }
  118. .ghd-file-toggle svg.octicon {
  119. pointer-events: none;
  120. vertical-align: middle;
  121. }
  122. `);
  123.  
  124. document.addEventListener("ghmo:container", addFileToggle);
  125. document.addEventListener("ghmo:diff", addFileToggle);
  126.  
  127. // Add GM options
  128. GM_registerMenuCommand("GitHub Diff File Toggle", () => {
  129. let result = "" + (GM_getValue("accordion") || false);
  130. const val = prompt("Accordion Mode? (true/false):", result);
  131. if (val) {
  132. result = /^t/.test(val);
  133. GM_setValue("accordion", result);
  134. }
  135. });
  136.  
  137. addBindings();
  138. addFileToggle();
  139. }
  140.  
  141. })();

QingJ © 2025

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