Collapsable Diffs and Linked Branches (GitHub)

Adds a toggle to collapse diffs in GitHub's pull request and commit diff interfaces

  1. // ==UserScript==
  2. // @name Collapsable Diffs and Linked Branches (GitHub)
  3. // @namespace https://github.com/chriskim06/userscripts
  4. // @description Adds a toggle to collapse diffs in GitHub's pull request and commit diff interfaces
  5. // @match https://github.com/*
  6. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  7. // @require https://greatest.deepsurf.us/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=19641
  8. // @version 1.5.6
  9. // ==/UserScript==
  10.  
  11. this.$ = this.jQuery = jQuery.noConflict(true);
  12.  
  13. $(function() {
  14.  
  15. waitForKeyElements('#files div[id^="diff-"] .file-header > .file-actions > button', addDiffCollapseButtons);
  16. waitForKeyElements('.pr-review-tools > .diffbar-item:nth-child(1)', addCollapseAllButton);
  17. waitForKeyElements('#partial-discussion-header .commit-ref', makeLinks);
  18.  
  19. function addDiffCollapseButtons(jNode) {
  20. // Add the event handler if its not already bound to the arrow
  21. if (jNode.length) {
  22. var events = $.data(jNode.get(0), 'events');
  23. if (!events || !events.click) {
  24. jNode.on('click', function() {
  25. // Set the collapse all button text
  26. var collapseAllButton = $('#diff-collapse-button');
  27. if (collapseAllButton.length) {
  28. var state = ($('#diff-collapse-button').attr('data-toggle-state') === 'expanded');
  29. collapseAllButton.attr('data-toggle-state', state ? 'collapsed' : 'expanded');
  30. collapseAllButton.text(state ? 'Show All' : 'Fold All');
  31. }
  32. });
  33. }
  34. }
  35. }
  36.  
  37. function addCollapseAllButton(jNode) {
  38. // Add a Show/Fold All button if its not there
  39. if (!$('#diff-collapse-button').length) {
  40. var blobs = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper, .js-file-content');
  41. jNode.after(
  42. '<div class="diffbar-item">' +
  43. '<button id="diff-collapse-button"' +
  44. 'class="btn btn-sm btn-outline BtnGroup-item" style="width: 75px"' +
  45. 'data-toggle-state="' + (blobs.is(':visible') ? 'expanded' : 'collapsed') + '">' +
  46. (blobs.is(':visible') ? 'Fold All' : 'Show All') +
  47. '</button>' +
  48. '</div>'
  49. );
  50. $('#diff-collapse-button').on('click', function() {
  51. // Toggle the visibility of all diffs
  52. var state = ($(this).attr('data-toggle-state') === 'expanded');
  53. $('#files div[id^="diff-"] > .file-header > .file-actions > button[aria-expanded="' + state + '"]').click();
  54. $(this).attr('data-toggle-state', state ? 'collapsed' : 'expanded');
  55. $(this).text((state) ? 'Show All' : 'Fold All');
  56. });
  57. }
  58. }
  59.  
  60. function makeLinks(jNode) {
  61. if (!jNode.children(':first-child').is('a')) {
  62. // Turn the branches being compared into links if they aren't already
  63. var url = window.location.href.match(/(https:\/\/github\.com\/)[A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+)/);
  64. if (url) {
  65. var branch = jNode.contents().text();
  66. if (!branch.includes(':')) {
  67. jNode.contents().wrapAll('<a href="' + url[0] + '/tree/' + branch + '"></a>');
  68. } else {
  69. jNode.contents().wrapAll('<a href="' + url[1] + branch.replace(':', url[2] + '/tree/') + '"></a>');
  70. }
  71. }
  72. }
  73. }
  74.  
  75. });