Greasy Fork is available in English.

Collapsable Diffs and Linked Branches (GitHub)

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

נכון ליום 11-02-2017. ראה הגרסה האחרונה.

  1. // ==UserScript==
  2. // @name Collapsable Diffs and Linked Branches (GitHub)
  3. // @namespace chriskim06
  4. // @description Adds a toggle to collapse diffs in GitHub's pull request and commit diff interfaces
  5. // @include 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.4
  9. // @grant none
  10. // @locale en
  11. // ==/UserScript==
  12.  
  13. this.$ = this.jQuery = jQuery.noConflict(true);
  14.  
  15. $(function() {
  16.  
  17. waitForKeyElements('#files div[id^="diff-"]', addDiffCollapseButtons);
  18. waitForKeyElements('.pr-review-tools > .diffbar-item:nth-child(1)', addCollapseAllButton);
  19. waitForKeyElements('#partial-discussion-header .commit-ref', makeLinks);
  20.  
  21. function addDiffCollapseButtons(jNode) {
  22. var expanded = 'M0 5l6 6 6-6H0z';
  23. var collapsed = 'M0 14l6-6L0 2v12z';
  24.  
  25. // Add buttons in each header to allow folding the diff
  26. var info = jNode.find('.file-info');
  27. if (!info.children().first().is('a')) {
  28. // Add the button if it isn't there
  29. info.prepend(
  30. '<a class="octicon-btn custom-collapsable" href="javascript:void(0)">' +
  31. '<svg height="16" width="12" xmlns="http://www.w3.org/2000/svg">' +
  32. '<path d="' + expanded + '" />' +
  33. '</svg>' +
  34. '</a>'
  35. );
  36. }
  37.  
  38. // Add the event handler if its not already bound to the arrow
  39. var btn = jNode.find('.octicon-btn.custom-collapsable');
  40. if (btn.length) {
  41. var events = $.data(btn.get(0), 'events');
  42. if (!events || !events.click) {
  43. btn.on('click', function() {
  44. // Toggle the visibility of the diff and the direction of the arrow
  45. var icon = $(this).find('path');
  46. jNode.children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper, .js-file-content').toggle();
  47. icon.attr('d', (icon.attr('d') === collapsed) ? expanded : collapsed);
  48. var collapseAllButton = $('#diff-collapse-button');
  49. if (collapseAllButton.length) {
  50. var state = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper').is(':visible');
  51. collapseAllButton.attr('data-toggle-state', state ? 'expanded' : 'collapsed');
  52. collapseAllButton.text(state ? 'Fold All' : 'Show All');
  53. }
  54. });
  55. }
  56. }
  57. }
  58.  
  59. function addCollapseAllButton(jNode) {
  60. // Add a Show/Fold All button if its not there
  61. if (!$('#diff-collapse-button').length) {
  62. var blobs = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper, .js-file-content');
  63. jNode.after(
  64. '<div class="diffbar-item">' +
  65. '<button id="diff-collapse-button"' +
  66. 'class="btn btn-sm btn-outline BtnGroup-item" style="width: 75px"' +
  67. 'data-toggle-state="' + (blobs.is(':visible') ? 'expanded' : 'collapsed') + '">' +
  68. (blobs.is(':visible') ? 'Fold All' : 'Show All') +
  69. '</button>' +
  70. '</div>'
  71. );
  72. $('#diff-collapse-button').on('click', function() {
  73. // Toggle the visibility of all diffs, directions of arrows, and the button
  74. var diffs = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper, .js-file-content');
  75. var state = ($(this).attr('data-toggle-state') === 'expanded');
  76. if (state) {
  77. diffs.hide();
  78. } else {
  79. diffs.show();
  80. }
  81. var expanded = 'M0 5l6 6 6-6H0z';
  82. var collapsed = 'M0 14l6-6L0 2v12z';
  83. $('.octicon-btn.custom-collapsable').find('path').attr('d', state ? collapsed : expanded);
  84. $(this).attr('data-toggle-state', state ? 'collapsed' : 'expanded');
  85. $(this).text(state ? 'Show All' : 'Fold All');
  86. });
  87. }
  88. }
  89.  
  90. function makeLinks(jNode) {
  91. if (!jNode.children(':first-child').is('a')) {
  92. // Turn the branches being compared into links if they aren't already
  93. var url = window.location.href.match(/(https:\/\/github\.com\/)[A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+)/);
  94. if (url) {
  95. var branch = jNode.contents().text();
  96. if (!branch.includes(':')) {
  97. jNode.contents().wrapAll('<a href="' + url[0] + '/tree/' + branch + '"></a>');
  98. } else {
  99. jNode.contents().wrapAll('<a href="' + url[1] + branch.replace(':', url[2] + '/tree/') + '"></a>');
  100. }
  101. }
  102. }
  103. }
  104.  
  105. });