Pull request

Add extra copy buttons into pull request pages.

  1. // ==UserScript==
  2. // @name Pull request
  3. // @namespace GitHub Scripts
  4. // @description Add extra copy buttons into pull request pages.
  5. // @icon https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
  6. // @run-at document-start
  7. // @match *://github.com/*
  8. // @grant none
  9. // @version 1.1.5
  10. // ==/UserScript==
  11.  
  12. function appendFetchBranchName() {
  13. var remoteBranchEl = document.getElementsByClassName('gh-header-meta')[0];
  14. if (!document.getElementById('pr_reset_fetch') && remoteBranchEl) {
  15. var childRemoteNodes = remoteBranchEl.querySelectorAll('span:not(.d-inline-block)');
  16. var lastChildRemoteNode = childRemoteNodes[childRemoteNodes.length - 1];
  17. var cpSpanEl = lastChildRemoteNode.cloneNode(true);
  18. var cpResetSpanEl = lastChildRemoteNode.cloneNode(true);
  19. var cpEl = cpSpanEl.firstElementChild;
  20. var remoteBranchName = cpEl.getAttribute('value');
  21. var branchName = remoteBranchName.split(':')[1] ?? remoteBranchName.split(':')[0];
  22. var pullNumber = document.getElementsByClassName('js-issue-title')[0].nextElementSibling.textContent.substr(1);
  23. var pullName = 'pull/' + pullNumber + '/head:' + branchName;
  24. cpEl.setAttribute('value', branchName);
  25.  
  26. var branchNameEl = document.createElement('span');
  27. branchNameEl.innerText = branchName;
  28. branchNameEl.setAttribute('class', 'commit-ref');
  29.  
  30. var remoteRefEl = document.querySelector('[data-pjax="#repo-content-pjax-container"]');
  31. var remoteUrl = 'git@github.com:' + remoteRefEl.href.replace(/^.*\/\/[^\/]+/, '').substr(1); + '.git';
  32. var gitCommands = 'git stash';
  33. gitCommands += ' && git fetch ' + remoteUrl + ' +refs/pull/' + pullNumber + '/head';
  34. gitCommands += ' && git checkout FETCH_HEAD && git log --oneline -n 2';
  35. cpResetSpanEl.firstElementChild.setAttribute('value', gitCommands);
  36.  
  37. var resetFetchEl = document.createElement('span');
  38. resetFetchEl.innerText = 'Fetch and pull PR\'s branch commands';
  39. resetFetchEl.setAttribute('class', 'commit-ref');
  40. resetFetchEl.setAttribute('id', 'pr_reset_fetch');
  41.  
  42. var appendedEl = remoteBranchEl.querySelector('.flex-auto');
  43. appendedEl.appendChild(document.createElement('br'));
  44. if (remoteBranchName.split(':')[1]) {
  45. appendedEl.appendChild(branchNameEl);
  46. appendedEl.appendChild(cpSpanEl);
  47. appendedEl.appendChild(document.createTextNode('\u00A0\u00A0\u00A0'));
  48. }
  49. appendedEl.appendChild(resetFetchEl);
  50. appendedEl.appendChild(cpResetSpanEl);
  51. }
  52. }
  53.  
  54. document.addEventListener('DOMContentLoaded', function () {
  55. appendFetchBranchName();
  56. var targetNode = document.documentElement || document.body;
  57. var config = { attributes: true, childList: true, subtree: true };
  58. var callback = function (mutations) {
  59. var count = 0;
  60. for (var i = 0; i < mutations.length; i++) {
  61. var mutation = mutations[i];
  62. if (!count && mutation.type === 'attributes' && mutation.attributeName === 'tabindex') {
  63. count++;
  64. appendFetchBranchName();
  65. document.getElementById('files') && targetNode.children[1] && targetNode.children[1].classList.remove('container-lg');
  66. }
  67. }
  68. };
  69. var observer = new MutationObserver(callback);
  70. (targetNode instanceof Element || targetNode instanceof HTMLDocument) && observer.observe(targetNode, config);
  71. });

QingJ © 2025

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