Github Commit Whitespace

Adds button to hide whitespaces from commit

As of 2018-01-27. See the latest version.

  1. // ==UserScript==
  2. // @name Github Commit Whitespace
  3. // @namespace https://github.com/jerone/UserScripts
  4. // @description Adds button to hide whitespaces from commit
  5. // @author jerone
  6. // @copyright 2014+, jerone (http://jeroenvanwarmerdam.nl)
  7. // @license GPL-3.0
  8. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Commit_Whitespace
  9. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Commit_Whitespace
  10. // @supportURL https://github.com/jerone/UserScripts/issues
  11. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
  12. // @icon https://github.com/fluidicon.png
  13. // @include https://github.com/*
  14. // @version 1.5.3
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. (function() {
  19.  
  20. function addButton() {
  21. var e
  22. if ((/\/commit\//.test(location.href) || /\/compare\//.test(location.href)) && (e = document.getElementById('toc'))) {
  23.  
  24. var r = e.querySelector('.GithubCommitWhitespaceButton')
  25. if (r) {
  26. r.parentElement.removeChild(r)
  27. }
  28.  
  29. var on = /w=/.test(location.search) // Any occurense results in enabling
  30.  
  31. var b = e.querySelector('.toc-diff-stats')
  32.  
  33. var a = document.createElement('a')
  34. a.classList.add('btn', 'btn-sm', 'tooltipped', 'tooltipped-n')
  35. if (on) {
  36. a.classList.add('selected')
  37. }
  38. a.setAttribute('href', url(on))
  39. a.setAttribute('rel', 'nofollow')
  40. a.setAttribute('aria-label', on ? 'Show commit whitespace' : 'Hide commit whitespace')
  41. a.appendChild(document.createTextNode('\u2423'))
  42.  
  43. var g = document.createElement('div')
  44. g.classList.add('GithubCommitWhitespaceButton', 'float-right')
  45. g.style.margin = '0 10px 0 0' // Give us some room
  46. g.appendChild(a)
  47.  
  48. b.parentNode.insertBefore(g, b)
  49. } else if (/\/pull\/\d*\/(files|commits)/.test(location.href) && (e = document.querySelector('#files_bucket .pr-toolbar .diffbar > .float-right'))) {
  50.  
  51. var r = e.querySelector('.GithubCommitWhitespaceButton')
  52. if (r) {
  53. r.parentElement.removeChild(r)
  54. }
  55.  
  56. var on = /w=/.test(location.search) // Any occurense result in enabling
  57.  
  58. var a = document.createElement('a')
  59. a.classList.add('btn', 'btn-sm', 'btn-outline', 'tooltipped', 'tooltipped-s')
  60. a.setAttribute('href', url(on))
  61. a.setAttribute('rel', 'nofollow')
  62. a.setAttribute('aria-label', on ? 'Show commit whitespace' : 'Hide commit whitespace')
  63. a.appendChild(document.createTextNode('\u2423'))
  64.  
  65. var g = document.createElement('div')
  66. g.classList.add('GithubCommitWhitespaceButton', 'diffbar-item')
  67. g.appendChild(a)
  68.  
  69. e.insertBefore(g, e.firstChild)
  70. }
  71. }
  72.  
  73. function url(on) {
  74. var searches = location.search.replace(/^\?/, '').split('&').filter(function(item) {
  75. return item && !/w=.*/.test(item)
  76. })
  77. if (!on) {
  78. searches.push('w=1')
  79. }
  80. return location.href.replace(location.search, '').replace(location.hash, '') + (searches.length > 0 ? '?' + searches.join('&') : '') + location.hash;
  81. }
  82.  
  83. // Init
  84. addButton()
  85.  
  86. // Pjax
  87. document.addEventListener('pjax:end', addButton)
  88.  
  89. })()