Skip to content

Instantly share code, notes, and snippets.

@jphastings
Last active August 27, 2015 10:25
Show Gist options
  • Save jphastings/1f3f63031a031c756da8 to your computer and use it in GitHub Desktop.
Save jphastings/1f3f63031a031c756da8 to your computer and use it in GitHub Desktop.
Hide files in github PRs by file extension. Super hacky, YMMV!
// ==UserScript==
// @name Github file extension hider
// @namespace http://toys.byjp.me/
// @version 0.1.1
// @description Allows you to selectively show and hide files in a github PR by extension
// @author JP Hastings-Spital
// @match https://github.com/*/*/pull/*
// ==/UserScript==
(function() {
var lastPathname = window.location.pathname;
function displayIfNecessary() {
if (window.location.pathname != lastPathname) {
lastPathname = window.location.pathname;
var existingSelector = jQuery('#hider-selector');
if (window.location.pathname.match(/files$/)) {
if (existingSelector[0]) {
existingSelector.show();
} else {
displaySelector();
}
} else {
existingSelector.hide();
}
}
}
setInterval(displayIfNecessary, 250);
function displaySelector() {
var extensions = [];
jQuery('#files .js-details-container').each(function(i, el) {
var filename = jQuery.trim(jQuery(el).find('.file-info > span:not(.diffstat)').text());
var parts = filename.split('.');
var ext = parts[parts.length - 1];
extensions.push(ext);
jQuery(el)
.attr('data-file-extension', ext)
.attr('data-filename', filename);
});
extensions = jQuery.unique(extensions);
var selector = jQuery('<div id="hider-selector" class="flex-table gh-header-meta"><span>Show/Hide files with these extensions: </span></div>');
jQuery.each(extensions, function(i, ext) {
var name = 'ext-' + ext;
var check = jQuery('<span style="margin: 2px 10px"></span>');
var input = jQuery('<input type="checkbox" checked name="' + name + '"/>');
var label = jQuery('<label for="' + name + '">' + ext + '</label>');
input.bind('change', function (cb) {
jQuery('[data-file-extension='+ext+']').toggleClass('hidden', !input.prop('checked'));
});
check.append(input);
check.append(label);
selector.append(check);
});
var existingSelector = jQuery('#hider-selector')[0];
if (existingSelector) {
jQuery(existingSelector).replaceWith(selector);
} else {
selector.insertAfter('.gh-header-meta');
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment