Github Commit Diff

Adds button to show diff (or patch) file for commit

Version vom 27.01.2018. Aktuellste Version

  1. // ==UserScript==
  2. // @name Github Commit Diff
  3. // @namespace https://github.com/jerone/UserScripts
  4. // @description Adds button to show diff (or patch) file for 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_Diff
  9. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Commit_Diff
  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://assets-cdn.github.com/pinned-octocat.svg
  13. // @include https://github.com/*
  14. // @exclude https://github.com/*/*.diff
  15. // @exclude https://github.com/*/*.patch
  16. // @version 1.6.6
  17. // @grant none
  18. // ==/UserScript==
  19.  
  20. (function() {
  21.  
  22. function addButton() {
  23. var e
  24. if ((/\/commit\//.test(location.href) || /\/compare\//.test(location.href)) && (e = document.getElementById('toc'))) {
  25.  
  26. var r = e.querySelector('.GithubCommitDiffButton')
  27. if (r) {
  28. r.parentElement.removeChild(r)
  29. }
  30.  
  31. var b = e.querySelector('.toc-diff-stats')
  32.  
  33. const s = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  34. s.classList.add('octicon', 'octicon-diff')
  35. s.setAttributeNS(null, 'height', 16)
  36. s.setAttributeNS(null, 'width', 14)
  37. s.setAttributeNS(null, 'viewBox', '0 0 14 16')
  38.  
  39. const p = document.createElementNS('http://www.w3.org/2000/svg', 'path')
  40. p.setAttributeNS(null, 'd', 'M6 7h2v1H6v2h-1V8H3v-1h2V5h1v2zM3 13h5v-1H3v1z m4.5-11l3.5 3.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V3c0-0.55 0.45-1 1-1h6.5z m2.5 4L7 3H1v12h9V6zM8.5 0S3 0 3 0v1h5l4 4v8h1V4.5L8.5 0z')
  41. s.appendChild(p)
  42.  
  43. var a = document.createElement('a')
  44. a.classList.add('btn', 'btn-sm', 'tooltipped', 'tooltipped-n')
  45. a.setAttribute('href', getPatchOrDiffHref('diff'))
  46. a.setAttribute('rel', 'nofollow')
  47. a.setAttribute('aria-label', 'Show commit diff.\r\nHold Shift to open commit patch.')
  48. a.appendChild(s)
  49. a.appendChild(document.createTextNode(' Diff'))
  50.  
  51. var g = document.createElement('div')
  52. g.classList.add('GithubCommitDiffButton', 'float-right')
  53. g.style.margin = '0 10px 0 0' // Give us some room
  54. g.appendChild(a)
  55.  
  56. b.parentNode.insertBefore(g, b)
  57.  
  58. a.addEventListener('mousedown', mousedownEvent, false)
  59. a.addEventListener('mouseout', function() {
  60. a.setAttribute('href', getPatchOrDiffHref('diff'))
  61. }, false)
  62. } else if (/\/pull\/\d*\/(files|commits)/.test(location.href) && (e = document.querySelector('#files_bucket .pr-toolbar .diffbar > .float-right'))) {
  63.  
  64. var r = e.querySelector('.GithubCommitDiffButton')
  65. if (r) {
  66. r.parentElement.removeChild(r)
  67. }
  68.  
  69. const s = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  70. s.classList.add('octicon', 'octicon-diff')
  71. s.setAttributeNS(null, 'height', 16)
  72. s.setAttributeNS(null, 'width', 14)
  73. s.setAttributeNS(null, 'viewBox', '0 0 14 16')
  74.  
  75. const p = document.createElementNS('http://www.w3.org/2000/svg', 'path')
  76. p.setAttributeNS(null, 'd', 'M6 7h2v1H6v2h-1V8H3v-1h2V5h1v2zM3 13h5v-1H3v1z m4.5-11l3.5 3.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V3c0-0.55 0.45-1 1-1h6.5z m2.5 4L7 3H1v12h9V6zM8.5 0S3 0 3 0v1h5l4 4v8h1V4.5L8.5 0z')
  77. s.appendChild(p)
  78.  
  79. var a = document.createElement('a')
  80. a.classList.add('btn', 'btn-sm', 'btn-outline', 'tooltipped', 'tooltipped-s')
  81. a.setAttribute('href', getPatchOrDiffHref('diff'))
  82. a.setAttribute('rel', 'nofollow')
  83. a.setAttribute('aria-label', 'Show commit diff.\r\nHold Shift to open commit patch.')
  84. a.appendChild(s)
  85. a.appendChild(document.createTextNode(' Diff'))
  86.  
  87. var g = document.createElement('div')
  88. g.classList.add('GithubCommitDiffButton', 'diffbar-item')
  89. g.appendChild(a)
  90.  
  91. e.insertBefore(g, e.firstChild)
  92.  
  93. a.addEventListener('mousedown', mousedownEvent, false)
  94. a.addEventListener('mouseout', function() {
  95. a.setAttribute('href', getPatchOrDiffHref('diff'))
  96. }, false)
  97. }
  98. }
  99.  
  100. function mousedownEvent(e) {
  101. if (e.shiftKey) {
  102. var patch = getPatchOrDiffHref('patch')
  103. e.preventDefault()
  104. this.setAttribute('href', patch)
  105. if (e.which === 1) { // left click
  106. location.href = patch
  107. // To prevent Firefox default behavior (opening a new window)
  108. // when pressing shift-click on a link, delete the link.
  109. this.parentElement.removeChild(this)
  110. } else if (e.which === 2) { // Middle click
  111. window.open(patch, 'GithubCommitDiff')
  112. }
  113. } else {
  114. this.setAttribute('href', getPatchOrDiffHref('diff'))
  115. }
  116. }
  117.  
  118. function getPatchOrDiffHref(type) {
  119. return (document.querySelector('link[type="text/plain+' + type + '"]') || document.querySelector('link[type="text/x-' + type + '"]') || {
  120. href: location.href + '.' + type
  121. }).href
  122. }
  123.  
  124. // Init
  125. addButton()
  126.  
  127. // Pjax
  128. document.addEventListener('pjax:end', addButton)
  129.  
  130. })()