GitHub Diff Files Filter

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

Ekde 2019/01/11. Vidu La ĝisdata versio.

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