Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lsloan/e51ac64706b86cc1942616004f2be3bd to your computer and use it in GitHub Desktop.
Save lsloan/e51ac64706b86cc1942616004f2be3bd to your computer and use it in GitHub Desktop.
Add a button in GitHub UI to ignore whitespace in diffs. To install in a browser with a UserScript extension (e.g., Tampermonkey), click the "Raw" button.
// ==UserScript==
// @name Ignore whitespace button on GitHub
// @description Adds a button in GitHub UI to ignore whitespace changes in commits
// @author lsloan
// @namespace https://gist.github.com/lsloan/e51ac64706b86cc1942616004f2be3bd
// @updateURL https://gist.github.com/lsloan/e51ac64706b86cc1942616004f2be3bd/raw/github_whitespace_button.user.js
// @match https://github.com/*
// @version 1.5
// @run-at document-end
// @grant none
// ==/UserScript==
function AddWhitespaceButton()
{
var hasWhitespaceSwitch = window.location.search.match( /[\?&]w=/ ) !== null;
AddButtonInPullRequestReview( hasWhitespaceSwitch );
AddButtonInCommits( hasWhitespaceSwitch );
}
function AddButtonInPullRequestReview( hasWhitespaceSwitch )
{
var toc = document.querySelector( '.pr-toolbar .dropdown-menu' );
if( !toc || document.getElementById( 'js-lsloan-whitespace-button' ) )
{
return;
}
var button = CreateButton( hasWhitespaceSwitch );
button.className = 'dropdown-item' + ( hasWhitespaceSwitch ? ' selected' : '' );
if( hasWhitespaceSwitch )
{
var checkmark = document.createElement( 'b' );
checkmark.className = 'octicon';
checkmark.textContent = '✓';
button.appendChild( checkmark );
}
toc.appendChild( button );
}
function AddButtonInCommits( hasWhitespaceSwitch )
{
var toc = document.querySelector( '#toc > .btn-group.right' );
if( !toc || document.getElementById( 'js-lsloan-whitespace-button' ) )
{
return;
}
var button = CreateButton( hasWhitespaceSwitch );
button.className = 'btn btn-sm right' + ( hasWhitespaceSwitch ? ' selected' : '' );
button.style.marginRight = '10px';
//$( document ).pjax( button, '#js-repo-pjax-container' );
toc.parentNode.insertBefore( button, toc.nextSibling );
var pr = $( '.js-pull-request-tab' );
if( pr && !pr.RegisteredWhitespaceClick )
{
pr.RegisteredWhitespaceClick = true;
$( document ).on( 'click', pr, function( e )
{
setTimeout( function()
{
button.href = UpdateQueryString( 'w', hasWhitespaceSwitch ? null : '1' );
}, 0 );
} );
}
}
function CreateButton( hasWhitespaceSwitch )
{
var button = document.createElement( 'a' );
button.id = 'js-lsloan-whitespace-button';
button.textContent = 'Ignore whitespace';
button.href = UpdateQueryString( 'w', hasWhitespaceSwitch ? null : '1' );
return button;
}
AddWhitespaceButton();
$( document ).on( 'pjax:complete', AddWhitespaceButton );
// http://stackoverflow.com/a/11654596/2200891
function UpdateQueryString(key, value)
{
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
url = window.location.href,
hash;
if (re.test(url))
{
if (typeof value !== 'undefined' && value !== null)
{
url = url.replace(re, '$1' + key + "=" + value + '$2$3');
}
else
{
hash = url.split('#');
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
if (typeof hash[1] !== 'undefined' && hash[1] !== null)
{
url += '#' + hash[1];
}
}
}
else if (typeof value !== 'undefined' && value !== null)
{
var separator = url.indexOf('?') !== -1 ? '&' : '?';
hash = url.split('#');
url = hash[0] + separator + key + '=' + value;
if (typeof hash[1] !== 'undefined' && hash[1] !== null)
{
url += '#' + hash[1];
}
}
return url;
}
@lsloan
Copy link
Author

lsloan commented Aug 5, 2016

Issues:

  1. When the page refreshes with "w=1" URL, "Ignore whitespace" no longer appears in the list of options. However, manually reloading the page with that URL, "Ignore whitespace" does appear, along with a check mark to show it's active.
  2. GitHub bug: When ignore whitespace mode is enabled, the "line notes" feature stops working. That makes it difficult (impossible?) to give code review feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment