Github Pull Request From Link

Make pull request branches linkable

当前为 2018-09-14 提交的版本,查看 最新版本

  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 (http://jeroenvanwarmerdam.nl)
  7. // @license GPL-3.0
  8. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Pull_Request_From
  9. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Pull_Request_From
  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://assets-cdn.github.com/pinned-octocat.svg
  13. // @version 19
  14. // @grant none
  15. // @include https://github.com/*/*
  16. // @exclude https://github.com/*/*.diff
  17. // @exclude https://github.com/*/*.patch
  18. // ==/UserScript==
  19.  
  20. (function() {
  21.  
  22. String.format = function(string) {
  23. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  24. return string.replace(/{(\d+)}/g, function(match, number) {
  25. return typeof args[number] !== "undefined" ? args[number] : match;
  26. });
  27. };
  28.  
  29. function init() {
  30. var repo = document.querySelector('.repohead-details-container h1 [itemprop="name"]').textContent,
  31. author = document.querySelector('.repohead-details-container h1 [itemprop="author"]').textContent;
  32. Array.prototype.filter.call(document.querySelectorAll("span.commit-ref"), function(treeSpan) {
  33. return !treeSpan.querySelector(".unknown-repo");
  34. }).forEach(function(treeSpan) {
  35. var treeUser = treeSpan.querySelector('.user');
  36. var treeParts = treeSpan.querySelectorAll('.css-truncate-target');
  37. var treeLink = document.createElement("a");
  38. Array.prototype.forEach.call(treeParts, function(part) {
  39. part.style.display = "inline";
  40. });
  41. treeLink.setAttribute("href", String.format("/{0}/{1}/tree/{2}",
  42. treeUser ? treeUser.textContent : author, // user
  43. repo, // repository
  44. escape(treeParts[treeParts.length - 1].textContent))); // branch
  45. treeLink.innerHTML = treeSpan.innerHTML;
  46. treeSpan.innerHTML = "";
  47. treeSpan.appendChild(treeLink);
  48. });
  49. }
  50.  
  51. // Page load.
  52. init();
  53.  
  54. // On pjax.
  55. document.addEventListener('pjax:end', init);
  56.  
  57. })();