Collapsable Diffs and Linked Branches (GitHub)

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

2017/07/01のページです。最新版はこちら

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
  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.5
  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-"] .file-header > .file-actions > button', 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. // Add the event handler if its not already bound to the arrow
  23. if (jNode.length) {
  24. var events = $.data(jNode.get(0), 'events');
  25. if (!events || !events.click) {
  26. jNode.on('click', function() {
  27. // Set the collapse all button text
  28. var collapseAllButton = $('#diff-collapse-button');
  29. if (collapseAllButton.length) {
  30. var state = ($('#diff-collapse-button').attr('data-toggle-state') === 'expanded');
  31. collapseAllButton.attr('data-toggle-state', state ? 'collapsed' : 'expanded');
  32. collapseAllButton.text(state ? 'Show All' : 'Fold All');
  33. }
  34. });
  35. }
  36. }
  37. }
  38.  
  39. function addCollapseAllButton(jNode) {
  40. // Add a Show/Fold All button if its not there
  41. if (!$('#diff-collapse-button').length) {
  42. var blobs = $('#files').find('div[id^="diff-"]').children('.data.highlight.blob-wrapper, .data.highlight.empty, .render-wrapper, .js-file-content');
  43. jNode.after(
  44. '<div class="diffbar-item">' +
  45. '<button id="diff-collapse-button"' +
  46. 'class="btn btn-sm btn-outline BtnGroup-item" style="width: 75px"' +
  47. 'data-toggle-state="' + (blobs.is(':visible') ? 'expanded' : 'collapsed') + '">' +
  48. (blobs.is(':visible') ? 'Fold All' : 'Show All') +
  49. '</button>' +
  50. '</div>'
  51. );
  52. $('#diff-collapse-button').on('click', function() {
  53. // Toggle the visibility of all diffs
  54. var state = ($(this).attr('data-toggle-state') === 'expanded');
  55. $('#files div[id^="diff-"] > .file-header > .file-actions > button[aria-expanded="' + state + '"]').click();
  56. $(this).attr('data-toggle-state', state ? 'collapsed' : 'expanded');
  57. $(this).text((state) ? 'Show All' : 'Fold All');
  58. });
  59. }
  60. }
  61.  
  62. function makeLinks(jNode) {
  63. if (!jNode.children(':first-child').is('a')) {
  64. // Turn the branches being compared into links if they aren't already
  65. var url = window.location.href.match(/(https:\/\/github\.com\/)[A-Za-z0-9_-]+(\/[A-Za-z0-9_-]+)/);
  66. if (url) {
  67. var branch = jNode.contents().text();
  68. if (!branch.includes(':')) {
  69. jNode.contents().wrapAll('<a href="' + url[0] + '/tree/' + branch + '"></a>');
  70. } else {
  71. jNode.contents().wrapAll('<a href="' + url[1] + branch.replace(':', url[2] + '/tree/') + '"></a>');
  72. }
  73. }
  74. }
  75. }
  76.  
  77. });