Collapsable Diffs and Linked Branches (GitHub)

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

As of 2016-01-27. See the latest version.

  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. // @version 1.3.6
  8. // @grant none
  9. // @locale en
  10. // ==/UserScript==
  11.  
  12. this.$ = this.jQuery = jQuery.noConflict(true);
  13.  
  14. $(function() {
  15.  
  16. function collapsable() {
  17. if ($('#files').length) {
  18. var expanded = '<a class="octicon-btn custom-collapsable" href="javascript:void(0)" onclick="return false;"><span class="octicon octicon-triangle-down "></span></a>';
  19. $('#files').find('div[id^="diff-"]').each(function() {
  20. var diff = $(this);
  21. if (!diff.find('.file-info:first-child').is('a')) {
  22. diff.find('.file-info').prepend(expanded);
  23. var area = diff.children('.data.highlight.blob-wrapper');
  24. diff.find('.octicon-btn.custom-collapsable').on('click', function() {
  25. var icon = $(this).children(':first');
  26. if (icon.hasClass('octicon-triangle-down')) {
  27. icon.attr('class', 'octicon octicon-triangle-right');
  28. area.hide();
  29. } else {
  30. icon.attr('class', 'octicon octicon-triangle-down');
  31. area.show();
  32. }
  33. });
  34. }
  35. });
  36. }
  37. }
  38. function makeLinks() {
  39. if ($('#partial-discussion-header').length) {
  40. $('span.commit-ref.current-branch').each(function() {
  41. var repo = $('.entry-title a[data-pjax]').text();
  42. var baseUrl = 'https://github.com';
  43. var branch = $(this).text();
  44. if (branch.indexOf(':') === -1) {
  45. baseUrl += $('.entry-title a[data-pjax]').attr('href') + '/tree/';
  46. $(this).wrap('<a href="' + baseUrl + branch + '"></a>');
  47. } else {
  48. var fork = branch.split(':');
  49. baseUrl += '/' + fork[0] + '/' + repo + '/tree/';
  50. $(this).wrap('<a href="' + baseUrl + fork[1] + '"></a>');
  51. }
  52. });
  53. }
  54. }
  55. makeLinks();
  56. collapsable();
  57.  
  58. window.$(document).on('pjax:end', function() {
  59. makeLinks();
  60. collapsable();
  61. });
  62. });