Github Commit Diff

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

Versione datata 27/09/2018. Vedi la nuova versione l'ultima versione.

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