Github Pull Request From Link

Make pull request original branch linkable

当前为 2014-11-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Github Pull Request From Link
  3. // @namespace https://github.com/jerone/UserScripts/
  4. // @description Make pull request original branch 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. // @version 12
  11. // @grant none
  12. // @include https://github.com/*/*
  13. // ==/UserScript==
  14. /* global unsafeWindow */
  15.  
  16. (function(unsafeWindow) {
  17.  
  18. String.format = function(string) {
  19. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  20. return string.replace(/{(\d+)}/g, function(match, number) {
  21. return typeof args[number] !== "undefined" ? args[number] : match;
  22. });
  23. };
  24.  
  25. // init;
  26. function init() {
  27. var repo = document.querySelector(".js-current-repository").textContent;
  28. Array.prototype.forEach.call(document.querySelectorAll("span.commit-ref.current-branch"), function(treeSpan) {
  29. if (treeSpan.querySelector(".unknown-repo")) { return; }
  30. var tree = treeSpan.textContent.trim().split(":");
  31. var treeLink = document.createElement("a");
  32. treeLink.setAttribute("href", String.format("https://github.com/{0}/{1}/tree/{2}",
  33. tree.shift(), // user;
  34. repo, // repository;
  35. tree.join(":"))); // branch;
  36. treeLink.innerHTML = treeSpan.innerHTML;
  37. treeSpan.innerHTML = "";
  38. treeSpan.appendChild(treeLink);
  39. });
  40. }
  41. init();
  42.  
  43. // on pjax;
  44. unsafeWindow.$(document).on("pjax:end", init); // `pjax:end` also runs on history back;
  45.  
  46. })(typeof unsafeWindow !== "undefined" ? unsafeWindow : window);