Github Commit Diff

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

ของเมื่อวันที่ 04-09-2016 ดู เวอร์ชันล่าสุด

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