|
// ==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; |
|
} |
Issues: