// ==UserScript== // @name Widen Code Container and Hide Whitespace (GitHub) // @namespace chriskim06 // @description Adds buttons to allow you to widen the container when viewing files and hide whitespace when viewing pull request diffs // @include https://github.com/* // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js // @version 1.3.9 // @grant none // @locale en // @downloadURL none // ==/UserScript== this.$ = this.jQuery = jQuery.noConflict(true); $(function() { if ($('#user-links').length) { // Add buttons in the header navbar for widening and hiding whitespace $('#user-links').prepend( '
  • ' + '' + '' + '' + '
  • ' ); $('#user-links').prepend( '
  • ' + '' + '' + '' + '
  • ' ); // Toggle code container width on click $('#code-widen-button').click(function() { var container = $('.container.new-discussion-timeline.experiment-repo-nav'); var expanded = $(window).width() * 0.95; if ($('#files').is(':visible') || $('.repository-content').find('.file').is(':visible')) { if ($('meta[name="diff-view"]').attr('content') === 'unified') { // Only widen if viewing a single file or changes in unified mode container.css('width', (container.width() < expanded) ? expanded : 980); } } else if (container.width() >= expanded) { // Reduce the width on a page if needed container.css('width', 980); } $(this).blur(); }); // Toggle page with the w=1 query param in the url to show/hide whitespace $('#hide-whitespace-button').click(function() { if ($('#files').is(':visible')) { var url = window.location.href; if (url.includes('?w=1')) { // Check if there is more to the query and remove the whitespace query param window.location.href = url.replace((url.includes('&') ? /w=1\&/ : /\?w=1/), ''); } else if (url.includes('&w=1')) { // Remove the appended whitespace query param window.location.href = url.replace(/\&w=1/, ''); } else { // Add the whitespace query param var query = url.includes('?') ? '&w=1' : '?w=1'; if (url.includes('#')) { // Insert before any anchors in the url window.location.href = url.slice(0, url.indexOf('#')) + query + url.substr(url.indexOf('#')); } else { // Append to the url window.location.href = url + query; } } } $(this).blur(); }); } });