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-03. 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. diff.children('.data.highlight.blob-wrapper').hide();
  29. } else {
  30. icon.attr('d', 'M0 5l6 6 6-6H0z');
  31. diff.children('.data.highlight.blob-wrapper').show();
  32. }
  33. //diff.children('.data.highlight.blob-wrapper').slideToggle('fast');
  34. });
  35. }
  36. });
  37. }
  38. }
  39. function makeLinks() {
  40. if ($('#partial-discussion-header').length) {
  41. $('span.commit-ref.current-branch').each(function() {
  42. var elem = $(this);
  43. var repo = $('.entry-title').find('a[data-pjax]');
  44. var url = 'https://github.com';
  45. var branch = elem.text();
  46. if (branch.indexOf(':') === -1) {
  47. url += repo.attr('href') + '/tree/' + branch;
  48. } else {
  49. var fork = branch.split(':');
  50. url += '/' + fork[0] + '/' + repo.text() + '/tree/' + fork[1];
  51. }
  52. elem.wrap('<a href="' + url + '"></a>');
  53. });
  54. }
  55. }
  56. makeLinks();
  57. collapsable();
  58.  
  59. window.$(document).on('pjax:end pjax:complete', function() {
  60. makeLinks();
  61. collapsable();
  62. });
  63. });