Github Pull Request From Link

Make pull request branches linkable

  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. /* eslint security/detect-object-injection: "off" */
  22.  
  23. (function () {
  24. String.format = function (string) {
  25. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  26. return string.replace(/{(\d+)}/g, function (match, number) {
  27. return typeof args[number] !== "undefined" ? args[number] : match;
  28. });
  29. };
  30.  
  31. function init() {
  32. Array.prototype.filter
  33. .call(
  34. document.querySelectorAll(
  35. ".commit-ref[title], .base-ref[title], .head-ref[title]",
  36. ),
  37. function (treeSpan) {
  38. return !treeSpan.querySelector(".unknown-repo");
  39. },
  40. )
  41. .forEach(function (treeSpan) {
  42. const [repo, branch] = treeSpan.title.split(":");
  43. var treeParts = treeSpan.querySelectorAll(
  44. ".css-truncate-target",
  45. );
  46. var treeLink = document.createElement("a");
  47.  
  48. // Show underline on hover.
  49. Array.prototype.forEach.call(treeParts, function (part) {
  50. part.style.display = "inline";
  51. });
  52.  
  53. treeLink.setAttribute(
  54. "href",
  55. String.format("/{0}/tree/{1}", repo, branch),
  56. );
  57. treeLink.innerHTML = treeSpan.innerHTML;
  58. treeSpan.innerHTML = "";
  59. treeSpan.appendChild(treeLink);
  60. });
  61. }
  62.  
  63. // Page load.
  64. init();
  65.  
  66. // On pjax.
  67. document.addEventListener("pjax:end", init);
  68. })();