GitHub Diff Files Filter

A userscript that adds filters that toggle diff & PR folders, and files by extension

As of 2021-02-21. See the latest version.

  1. // ==UserScript==
  2. // @name GitHub Diff Files Filter
  3. // @version 2.1.3
  4. // @description A userscript that adds filters that toggle diff & PR folders, and files by extension
  5. // @license MIT
  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. // @require https://greatest.deepsurf.us/scripts/28721-mutations/code/mutations.js?version=882023
  12. // @icon https://github.githubassets.com/pinned-octocat.svg
  13. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  14. // ==/UserScript==
  15. (() => {
  16. "use strict";
  17.  
  18. // Example page: https://github.com/julmot/mark.js/pull/250/files
  19. GM_addStyle(".gdf-extension-hidden, .gdf-folder-hidden { display: none; }");
  20.  
  21. const allLabel = "\u00ABall\u00BB",
  22. rootLabel = "\u00ABroot\u00BB",
  23. noExtLabel = "\u00ABno-ext\u00BB",
  24. dotExtLabel = "\u00ABdot-files\u00BB",
  25. renameFileLabel = "\u00ABrenamed\u00BB",
  26. minFileLabel = "\u00ABmin\u00BB";
  27.  
  28. let exts = {};
  29. let folders = {};
  30.  
  31. function toggleBlocks({subgroup, type, show}) {
  32. if (type === allLabel) {
  33. // Toggle "all" blocks
  34. $$("#files div[id*='diff']").forEach(el => {
  35. el.classList.toggle(`gdf-${subgroup}-hidden`, !show);
  36. });
  37. // update filter buttons
  38. $$(`#files .gdf-${subgroup}-filter a`).forEach(el => {
  39. el.classList.toggle("selected", show);
  40. });
  41. } else if (subgroup === "folder") {
  42. Object.keys(folders)
  43. .reduce((acc, folder) => {
  44. if (folders[folder].length && !folder.includes("→")) {
  45. acc.push({
  46. folder,
  47. show: $(`.gdf-folder-filter a[data-item=${folder}]`).classList.contains("selected")
  48. });
  49. }
  50. return acc;
  51. }, [])
  52. // sort show:true to the end; to fix hiding files that should be shown
  53. .sort((a, b) => {
  54. if (a.show && b.show) {
  55. return 0;
  56. }
  57. return a.show && !b.show ? 1 : -1;
  58. })
  59. .forEach(({folder, show}) => {
  60. toggleGroup({group: folders[folder], subgroup, show });
  61. });
  62. } else if (exts[type]) {
  63. toggleGroup({group: exts[type], subgroup, show});
  64. }
  65. updateAllButton(subgroup);
  66. }
  67.  
  68. function toggleGroup({group, subgroup, show}) {
  69. const files = $("#files");
  70. /* group contains an array of div ids used to target the
  71. * hidden link added immediately above each file div container
  72. * <a name="diff-xxxxx"></a>
  73. * <div id="diff-#" class="file js-file js-details container">
  74. */
  75. group.forEach(id => {
  76. const file = $(`#${id}`, files);
  77. if (file) {
  78. file.classList.toggle(`gdf-${subgroup}-hidden`, !show);
  79. }
  80. });
  81. }
  82.  
  83. function updateAllButton(subgroup) {
  84. const buttons = $(`#files .gdf-${subgroup}-filter`),
  85. filters = $$(`a:not(.gdf-${subgroup}-all)`, buttons),
  86. selected = $$(`a:not(.gdf-${subgroup}-all).selected`, buttons);
  87. // set "all" button
  88. $(`.gdf-${subgroup}-all`, buttons).classList.toggle(
  89. "selected",
  90. filters.length === selected.length
  91. );
  92. }
  93.  
  94. function getSHA(file) {
  95. return file.hash
  96. // #toc points to "a"
  97. ? file.hash.slice(1)
  98. // .pr-toolbar points to "a > div > div.filename"
  99. : file.closest("a").hash.slice(1);
  100. }
  101.  
  102. function buildList() {
  103. exts = {};
  104. folders = {};
  105. // make noExtLabel the first element in the object
  106. exts[noExtLabel] = [];
  107. exts[dotExtLabel] = [];
  108. exts[renameFileLabel] = [];
  109. exts[minFileLabel] = [];
  110. folders[rootLabel] = [];
  111. // TOC in file diffs and pr-toolbar in Pull requests
  112. $$(".file-header .file-info > a").forEach(file => {
  113. let txt = (file.title || file.textContent || "").trim();
  114. if (txt) {
  115. const path = txt.split("/");
  116. const filename = path.splice(-1)[0];
  117. // test for no extension, then get extension name
  118. // regexp from https://github.com/silverwind/file-extension
  119. let ext = /\./.test(filename) ? /[^./\\]*$/.exec(filename)[0] : noExtLabel;
  120. const min = /\.min\./.test(filename);
  121. // Add filter for renamed files: {old path} → {new path}
  122. if (txt.indexOf(" → ") > -1) {
  123. ext = renameFileLabel;
  124. } else if (ext === filename.slice(1)) {
  125. ext = dotExtLabel;
  126. }
  127. const sha = getSHA(file);
  128. if (ext) {
  129. if (!exts[ext]) {
  130. exts[ext] = [];
  131. }
  132. exts[ext].push(sha);
  133. if (min) {
  134. exts[minFileLabel].push(sha);
  135. }
  136. }
  137. if (path.length > 0) {
  138. path.forEach(folder => {
  139. if (!folders[folder]) {
  140. folders[folder] = [];
  141. }
  142. folders[folder].push(sha);
  143. });
  144. } else {
  145. folders[rootLabel].push(sha);
  146. }
  147. }
  148. });
  149. }
  150.  
  151. function makeFilter({subgroup, label}) {
  152. const files = $("#files");
  153. let filters = 0;
  154. const group = subgroup === "folder" ? folders : exts;
  155. const keys = Object.keys(group);
  156. let html = `${label}: <div class="BtnGroup gdf-${subgroup}-filter">`;
  157. const btnClass = "btn btn-sm selected BtnGroup-item tooltipped tooltipped-n";
  158. // get length, but don't count empty arrays
  159. keys.forEach(item => {
  160. filters += group[item].length > 0 ? 1 : 0;
  161. });
  162. // Don't bother showing the filter if only one extension is found
  163. if (files && filters > 1) {
  164. filters = $(`.gdf-${subgroup}-filter-wrapper`);
  165. if (!filters) {
  166. filters = document.createElement("p");
  167. filters.className = `gdf-${subgroup}-filter-wrapper`;
  168. files.insertBefore(filters, files.firstChild);
  169. filters.addEventListener("click", event => {
  170. if (event.target.nodeName === "A") {
  171. event.preventDefault();
  172. event.stopPropagation();
  173. const el = event.target;
  174. el.classList.toggle("selected");
  175. toggleBlocks({
  176. subgroup: el.dataset.subgroup,
  177. type: el.textContent.trim(),
  178. show: el.classList.contains("selected")
  179. });
  180. }
  181. });
  182. }
  183. // add a filter "all" button to the beginning
  184. html += `
  185. <a class="${btnClass} gdf-${subgroup}-all" data-subgroup="${subgroup}" data-item="${allLabel}" aria-label="Toggle all files" href="#">
  186. ${allLabel}
  187. </a>`;
  188. keys.forEach(item => {
  189. if (group[item].length) {
  190. html += `
  191. <a class="${btnClass}" aria-label="${group[item].length}" data-subgroup="${subgroup}" data-item="${item}" href="#">
  192. ${item}
  193. </a>`;
  194. }
  195. });
  196. // prepend filter buttons
  197. filters.innerHTML = html + "</div>";
  198. }
  199. }
  200.  
  201. function init() {
  202. if ($("#files.diff-view") || $(".pr-toolbar")) {
  203. buildList();
  204. makeFilter({subgroup: "folder", label: "Filter file folder"});
  205. makeFilter({subgroup: "extension", label: "Filter file extension"});
  206. }
  207. }
  208.  
  209. function $(str, el) {
  210. return (el || document).querySelector(str);
  211. }
  212.  
  213. function $$(str, el) {
  214. return [...(el || document).querySelectorAll(str)];
  215. }
  216.  
  217. document.addEventListener("ghmo:container", init);
  218. document.addEventListener("ghmo:diff", init);
  219. init();
  220.  
  221. })();