Github Commit Whitespace

Adds button to hide whitespaces from commit

目前为 2024-02-01 提交的版本。查看 最新版本

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