Github Pull Request From Link

Make pull request branches linkable

当前为 2020-07-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Github Pull Request From Link
  3. // @namespace https://github.com/jerone/UserScripts/
  4. // @description Make pull request branches linkable
  5. // @author jerone
  6. // @copyright 2014+, jerone (https://github.com/jerone)
  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_Pull_Request_From
  10. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Pull_Request_From
  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://github.githubassets.com/pinned-octocat.svg
  14. // @version 20.1
  15. // @grant none
  16. // @include https://github.com/*/pull/*
  17. // @exclude https://github.com/*/*.diff
  18. // @exclude https://github.com/*/*.patch
  19. // ==/UserScript==
  20.  
  21. (function () {
  22.  
  23. String.format = function (string) {
  24. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  25. return string.replace(/{(\d+)}/g, function (match, number) {
  26. return typeof args[number] !== 'undefined' ? args[number] : match;
  27. });
  28. };
  29.  
  30. function init() {
  31. Array.prototype.filter.call(document.querySelectorAll('.commit-ref[title], .base-ref[title], .head-ref[title]'), function (treeSpan) {
  32. return !treeSpan.querySelector('.unknown-repo');
  33. }).forEach(function (treeSpan) {
  34. const [repo, branch] = treeSpan.title.split(':');
  35. var treeParts = treeSpan.querySelectorAll('.css-truncate-target');
  36. var treeLink = document.createElement('a');
  37.  
  38. // Show underline on hover.
  39. Array.prototype.forEach.call(treeParts, function (part) {
  40. part.style.display = 'inline';
  41. });
  42.  
  43. treeLink.setAttribute('href', String.format('/{0}/tree/{1}', repo, branch));
  44. treeLink.innerHTML = treeSpan.innerHTML;
  45. treeSpan.innerHTML = '';
  46. treeSpan.appendChild(treeLink);
  47. });
  48. }
  49.  
  50. // Page load.
  51. init();
  52.  
  53. // On pjax.
  54. document.addEventListener('pjax:end', init);
  55.  
  56. })();