Collapsable Diffs and Linked Branches (GitHub)

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

Pada tanggal 11 Mei 2016. Lihat %(latest_version_link).

  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.4.6
  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('.diffbar-item.dropdown.js-menu-container', addCollapseAllButton);
  19. waitForKeyElements('#partial-discussion-header', makeLinks);
  20. function addDiffCollapseButtons(jNode) {
  21. // Add buttons in each header to allow folding the diff
  22. var expanded = 'M0 5l6 6 6-6H0z';
  23. var collapsed = 'M0 14l6-6L0 2v12z';
  24. var info = jNode.find('.file-info');
  25. if (!info.children().first().is('a')) {
  26. // If the button isn't there add it and add the event handler
  27. info.prepend(
  28. '<a class="octicon-btn custom-collapsable" href="javascript:void(0)">' +
  29. '<svg height="16" width="12" xmlns="http://www.w3.org/2000/svg">' +
  30. '<path d="' + expanded + '" />' +
  31. '</svg>' +
  32. '</a>'
  33. );
  34. jNode.find('.octicon-btn.custom-collapsable').on('click', function() {
  35. // Toggle the visibility of the diff and the direction of the arrow
  36. var icon = $(this).find('path');
  37. jNode.children('.data.highlight.blob-wrapper').toggle();
  38. icon.attr('d', (icon.attr('d') === collapsed) ? expanded : collapsed);
  39. var collapseAllButton = $('#diff-collapse-button');
  40. if (collapseAllButton.length) {
  41. var state = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper').is(':visible');
  42. collapseAllButton.attr('data-toggle-state', state ? 'expanded' : 'collapsed');
  43. collapseAllButton.text(state ? 'Collapse All' : 'Expand All');
  44. }
  45. });
  46. }
  47. }
  48.  
  49. function addCollapseAllButton(jNode) {
  50. if (jNode.find('a').length === 2) {
  51. // Add an Expand/Collapse All button if its not there
  52. var blobs = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper');
  53. jNode.find('ul').append(
  54. '<a id="diff-collapse-button" ' +
  55. 'class="dropdown-item" ' +
  56. 'href="javascript:void(0)" ' +
  57. 'data-toggle-state="' + (blobs.is(':visible') ? 'expanded' : 'collapsed') + '">' +
  58. (blobs.is(':visible') ? 'Collapse All' : 'Expand All') +
  59. '</a>'
  60. );
  61. $('#diff-collapse-button').on('click', function() {
  62. // Toggle the visibility of all diffs, directions of arrows, and the button
  63. var state = ($(this).attr('data-toggle-state') === 'expanded');
  64. if (state) {
  65. blobs.hide();
  66. } else {
  67. blobs.show();
  68. }
  69. var expanded = 'M0 5l6 6 6-6H0z';
  70. var collapsed = 'M0 14l6-6L0 2v12z';
  71. $('.octicon-btn.custom-collapsable').find('path').attr('d', state ? collapsed : expanded);
  72. $(this).attr('data-toggle-state', state ? 'collapsed' : 'expanded');
  73. $(this).text(state ? 'Expand All' : 'Collapse All');
  74. });
  75. }
  76. }
  77.  
  78. function makeLinks(jNode) {
  79. if (jNode.find('.flex-table-item.flex-table-item-primary > a').length === 1) {
  80. // Turn the branches being compared into links if they aren't already
  81. var url = window.location.href.match(/(https:\/\/github\.com\/)([A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+))/);
  82. if (url) {
  83. $('span.commit-ref.current-branch').each(function() {
  84. var link;
  85. var branch = this.textContent;
  86. if (!branch.includes(':')) {
  87. link = url[0] + '/tree/' + branch;
  88. } else {
  89. var split = branch.split(':');
  90. link = url[1] + split[0] + url[3] + '/tree/' + split[1];
  91. }
  92. $(this).wrap('<a href="' + link + '"></a>');
  93. });
  94. }
  95. }
  96. }
  97.  
  98. });