Github Pull Request From Link

Make pull request branches linkable

当前为 2017-01-07 提交的版本,查看 最新版本

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