Collapsable Diffs and Linked Branches (GitHub)

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

As of 2016-02-04. 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.4.0
  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;"><svg height="16" width="12" xmlns="http://www.w3.org/2000/svg"><path d="M0 5l6 6 6-6H0z" /></svg></a>';
  19. $('#files').find('div[id^="diff-"]').each(function() {
  20. var diff = $(this);
  21. var info = diff.find('.file-info');
  22. if (!info.children().first().is('a')) {
  23. info.prepend(expanded);
  24. diff.find('.octicon-btn.custom-collapsable').on('click', function() {
  25. var icon = $(this).children().first().find('path');
  26. if (icon.attr('d') === 'M0 5l6 6 6-6H0z') {
  27. icon.attr('d', 'M0 14l6-6L0 2v12z');
  28. } else {
  29. icon.attr('d', 'M0 5l6 6 6-6H0z');
  30. }
  31. diff.children('.data.highlight.blob-wrapper').slideToggle('fast');
  32. });
  33. }
  34. });
  35. }
  36. }
  37. function makeLinks() {
  38. if ($('#partial-discussion-header').length) {
  39. $('span.commit-ref.current-branch').each(function() {
  40. var elem = $(this);
  41. var repo = $('.entry-title').find('a[data-pjax]');
  42. var url = 'https://github.com';
  43. var branch = elem.text();
  44. if (branch.indexOf(':') === -1) {
  45. url += repo.attr('href') + '/tree/' + branch;
  46. } else {
  47. var fork = branch.split(':');
  48. url += '/' + fork[0] + '/' + repo.text() + '/tree/' + fork[1];
  49. }
  50. elem.wrap('<a href="' + url + '"></a>');
  51. });
  52. }
  53. }
  54. makeLinks();
  55. collapsable();
  56.  
  57. window.$(document).on('pjax:end pjax:complete', function() {
  58. makeLinks();
  59. collapsable();
  60. });
  61. });