Github Pull Request From Link

Make pull request branches linkable

当前为 2018-11-25 提交的版本,查看 最新版本

  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 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://assets-cdn.github.com/pinned-octocat.svg
  14. // @version 19.1
  15. // @grant none
  16. // @include https://github.com/*/*
  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. if (!document.querySelector('.repohead-details-container h1 [itemprop="name"]')) return;
  32.  
  33. var repo = document.querySelector('.repohead-details-container h1 [itemprop="name"]').textContent,
  34. author = document.querySelector('.repohead-details-container h1 [itemprop="author"]').textContent;
  35. Array.prototype.filter.call(document.querySelectorAll("span.commit-ref"), function (treeSpan) {
  36. return !treeSpan.querySelector(".unknown-repo");
  37. }).forEach(function (treeSpan) {
  38. var treeUser = treeSpan.querySelector('.user');
  39. var treeParts = treeSpan.querySelectorAll('.css-truncate-target');
  40. var treeLink = document.createElement("a");
  41. Array.prototype.forEach.call(treeParts, function (part) {
  42. part.style.display = "inline";
  43. });
  44. treeLink.setAttribute("href", String.format("/{0}/{1}/tree/{2}",
  45. treeUser ? treeUser.textContent : author, // user
  46. repo, // repository
  47. escape(treeParts[treeParts.length - 1].textContent))); // branch
  48. treeLink.innerHTML = treeSpan.innerHTML;
  49. treeSpan.innerHTML = "";
  50. treeSpan.appendChild(treeLink);
  51. });
  52. }
  53.  
  54. // Page load.
  55. init();
  56.  
  57. // On pjax.
  58. document.addEventListener('pjax:end', init);
  59.  
  60. })();