GitHub Commit Compare

Add controls to compare commits.

当前为 2018-05-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Commit Compare
  3. // @namespace https://github.com/jerone/UserScripts
  4. // @description Add controls to compare commits.
  5. // @author jerone
  6. // @contributor darkred
  7. // @copyright 2017+, jerone (http://jeroenvanwarmerdam.nl)
  8. // @license GPL-3.0
  9. // @homepage https://github.com/jerone/UserScripts/tree/master/GitHub_Commit_Compare
  10. // @homepageURL https://github.com/jerone/UserScripts/tree/master/GitHub_Commit_Compare
  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/*/*/commits
  15. // @include https://github.com/*/*/commits/*
  16. // @exclude https://github.com/*/*.diff
  17. // @exclude https://github.com/*/*.patch
  18. // @version 0.0.2
  19. // @grant none
  20. // ==/UserScript==
  21.  
  22. (function () {
  23.  
  24. function addButton() {
  25. var nav;
  26. if ((nav = document.querySelector('.file-navigation'))) {
  27.  
  28. // Check if our group of buttons are already attached.
  29. // Remove it, as the 'old' buttons don't have events anymore.
  30. const e = document.getElementById('GitHubCommitCompareGroup');
  31. if (e) {
  32. e.parentElement.removeChild(e);
  33. }
  34. Array.from(document.querySelectorAll('.GitHubCommitCompareButtonAB')).forEach(function (b) {
  35. b.parentElement.removeChild(b);
  36. });
  37.  
  38. const c = document.createElement('input');
  39. c.type = 'checkbox';
  40. c.addEventListener('change',
  41. function () {
  42. const bb = document.querySelectorAll('.GitHubCommitCompareButtonAB');
  43. if (this.checked) {
  44. if (bb.length === 0) {
  45. addCompareButtons();
  46. } else {
  47. Array.from(bb).forEach(function (b) {
  48. b.classList.remove('d-none');
  49. });
  50. }
  51. } else {
  52. Array.from(bb).forEach(function (b) {
  53. b.classList.add('d-none');
  54. });
  55. }
  56. const bbb = document.getElementById('GitHubCommitCompareButton');
  57. if (bbb) bbb.classList.remove('disabled');
  58. });
  59.  
  60. const l = document.createElement('label');
  61. l.classList.add('tooltipped', 'tooltipped-n');
  62. l.setAttribute('aria-label', 'Show commit compare buttons');
  63. l.style.cssText = `
  64. float: left;
  65. padding: 3px 10px;
  66. font-size: 12px;
  67. font-weight: 600;
  68. line-height: 20px;
  69. color: #24292e;
  70. vertical-align: middle;
  71. background-color: #fff;
  72. border: 1px solid rgba(27,31,35,0.2);
  73. border-right: 0;
  74. border-top-left-radius: 3px;
  75. border-bottom-left-radius: 3px;
  76. `;
  77. l.appendChild(c);
  78.  
  79. const p = document.createElementNS('http://www.w3.org/2000/svg', 'path');
  80. p.setAttributeNS(null,
  81. 'd',
  82. 'M5 12H4c-.27-.02-.48-.11-.69-.31-.21-.2-.3-.42-.31-.69V4.72A1.993 1.993 0 0 0 2 1a1.993 1.993 0 0 0-1 3.72V11c.03.78.34 1.47.94 2.06.6.59 1.28.91 2.06.94h1v2l3-3-3-3v2zM2 1.8c.66 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2C1.35 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2zm11 9.48V5c-.03-.78-.34-1.47-.94-2.06-.6-.59-1.28-.91-2.06-.94H9V0L6 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 12 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z');
  83.  
  84. const s = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  85. s.classList.add('octicon', 'octicon-diff');
  86. s.setAttributeNS(null, 'height', 16);
  87. s.setAttributeNS(null, 'width', 14);
  88. s.setAttributeNS(null, 'viewBox', '0 0 14 16');
  89. s.appendChild(p);
  90.  
  91. const a = document.createElement('a');
  92. a.id = 'GitHubCommitCompareButton';
  93. a.classList.add('btn', 'btn-sm', 'tooltipped', 'tooltipped-n', 'disabled');
  94. a.setAttribute('href', '#');
  95. a.setAttribute('rel', 'nofollow');
  96. a.setAttribute('aria-label', 'Compare these commits');
  97. a.style.cssText = `
  98. border-top-left-radius: 0;
  99. border-bottom-left-radius: 0;
  100. font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
  101. `;
  102. a.appendChild(s);
  103. a.appendChild(document.createElement("span"));
  104.  
  105. const g = document.createElement('div');
  106. g.id = 'GitHubCommitCompareGroup';
  107. g.classList.add('float-right');
  108. g.appendChild(l);
  109. g.appendChild(a);
  110.  
  111. nav.appendChild(g);
  112. }
  113. }
  114.  
  115. function updateRadioButtons() {
  116. var compareAdisabled = true;
  117. var compareBdisabled = false;
  118.  
  119. const compares = document.querySelectorAll('.GitHubCommitCompareButtonAB');
  120. Array.from(compares).forEach(function (compare) {
  121. const compareA = compare.querySelector('[name="GitHubCommitCompareButtonA"]');
  122. const compareB = compare.querySelector('[name="GitHubCommitCompareButtonB"]');
  123.  
  124. compareA.disabled = compareAdisabled;
  125. compareA.parentNode.classList.toggle('disabled', compareAdisabled);
  126.  
  127. if (compareA.checked) {
  128. compareBdisabled = true;
  129. }
  130. if (compareB.checked) {
  131. compareAdisabled = false;
  132. }
  133.  
  134. compareB.disabled = compareBdisabled;
  135. compareB.parentNode.classList.toggle('disabled', compareBdisabled);
  136. });
  137.  
  138. updateCompareButton();
  139. }
  140.  
  141. function updateCompareButton() {
  142. const repo = document.querySelector('meta[property="og:url"]').content;
  143.  
  144. const compareA = document.querySelector('.GitHubCommitCompareButtonAB [name="GitHubCommitCompareButtonA"]:checked');
  145. const hashA = compareA.parentNode.parentNode.parentNode.querySelector('clipboard-copy').value;
  146. const compareB = document.querySelector('.GitHubCommitCompareButtonAB [name="GitHubCommitCompareButtonB"]:checked');
  147. const hashB = compareB.parentNode.parentNode.parentNode.querySelector('clipboard-copy').value;
  148.  
  149. const a = document.getElementById('GitHubCommitCompareButton');
  150. a.setAttribute('href', `${repo}/compare/${hashA}...${hashB}`);
  151. a.querySelector('span').textContent = ` ${hashA.substring(0, 7)}...${hashB.substring(0, 7)}`;
  152.  
  153. //localStorage.setItem('GitHubCommitCompareButtonHashA', hashA);
  154. //localStorage.setItem('GitHubCommitCompareButtonHashB', hashB);
  155. }
  156.  
  157. function addCompareButtons() {
  158. const commits = document.querySelectorAll('.commits-list-item .commit-links-cell');
  159. Array.from(commits).forEach(function (item, index) {
  160. const c1 = document.createElement('input');
  161. c1.name = 'GitHubCommitCompareButtonA';
  162. c1.type = 'radio';
  163. c1.addEventListener('change', updateRadioButtons);
  164. if (index === 1) c1.checked = true;
  165.  
  166. const l1 = document.createElement('label');
  167. l1.classList.add('btn', 'btn-outline', 'BtnGroup-item', 'tooltipped', 'tooltipped-s');
  168. l1.setAttribute('aria-label', 'Choose a base commit');
  169. l1.appendChild(c1);
  170.  
  171. const c2 = document.createElement('input');
  172. c2.name = 'GitHubCommitCompareButtonB';
  173. c2.type = 'radio';
  174. c2.addEventListener('change', updateRadioButtons);
  175. if (index === 0) c2.checked = true;
  176.  
  177. const l2 = document.createElement('label');
  178. l2.classList.add('btn', 'btn-outline', 'BtnGroup-item', 'tooltipped', 'tooltipped-s');
  179. l2.setAttribute('aria-label', 'Choose a head commit');
  180. l2.appendChild(c2);
  181.  
  182. // const p3 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
  183. // p3.setAttributeNS(null,
  184. //'d',
  185. //'M5 12H4c-.27-.02-.48-.11-.69-.31-.21-.2-.3-.42-.31-.69V4.72A1.993 1.993 0 0 0 2 1a1.993 1.993 0 0 0-1 3.72V11c.03.78.34 1.47.94 2.06.6.59 1.28.91 2.06.94h1v2l3-3-3-3v2zM2 1.8c.66 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2C1.35 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2zm11 9.48V5c-.03-.78-.34-1.47-.94-2.06-.6-.59-1.28-.91-2.06-.94H9V0L6 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 12 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z');
  186.  
  187. // const s3 = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  188. // s3.classList.add('octicon', 'octicon-diff');
  189. // s3.setAttributeNS(null, 'height', 16);
  190. // s3.setAttributeNS(null, 'width', 14);
  191. // s3.setAttributeNS(null, 'viewBox', '0 0 14 16');
  192. // s3.appendChild(p3);
  193.  
  194. // const l3 = document.createElement('a');
  195. // l3.classList.add('btn', 'btn-outline', 'BtnGroup-item', 'tooltipped', 'tooltipped-sw');
  196. // l3.setAttribute('aria-label', 'TODO');
  197. // l3.appendChild(s3);
  198.  
  199. const gg = document.createElement('div');
  200. gg.classList.add('GitHubCommitCompareButtonAB', 'commit-links-group', 'BtnGroup');
  201. gg.appendChild(l1);
  202. gg.appendChild(l2);
  203. //gg.appendChild(l3);
  204.  
  205. //item.style.width = '350px';
  206. if (item.querySelector('.muted-link')) { // Insert after number of comments button.
  207. item.insertBefore(gg, item.querySelector('.muted-link').nextSibling);
  208. } else {
  209. item.insertBefore(gg, item.firstChild);
  210. }
  211. });
  212.  
  213. updateRadioButtons(); // Update radio buttons.
  214. }
  215.  
  216. // Init.
  217. addButton();
  218.  
  219. // Pjax.
  220. document.addEventListener('pjax:end', addButton);
  221.  
  222. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址