Github Pull Request From Link

Make pull request original branch linkable

当前为 2015-03-05 提交的版本,查看 最新版本

  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. // @supportURL https://github.com/jerone/UserScripts/issues
  11. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
  12. // @version 12
  13. // @grant none
  14. // @include https://github.com/*/*
  15. // ==/UserScript==
  16. /* global unsafeWindow */
  17.  
  18. (function(unsafeWindow) {
  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. // init;
  28. function init() {
  29. var repo = document.querySelector(".js-current-repository").textContent;
  30. Array.prototype.forEach.call(document.querySelectorAll("span.commit-ref.current-branch"), function(treeSpan) {
  31. if (treeSpan.querySelector(".unknown-repo")) { return; }
  32. var tree = treeSpan.textContent.trim().split(":");
  33. var treeLink = document.createElement("a");
  34. treeLink.setAttribute("href", String.format("https://github.com/{0}/{1}/tree/{2}",
  35. tree.shift(), // user;
  36. repo, // repository;
  37. tree.join(":"))); // branch;
  38. treeLink.innerHTML = treeSpan.innerHTML;
  39. treeSpan.innerHTML = "";
  40. treeSpan.appendChild(treeLink);
  41. });
  42. }
  43. init();
  44.  
  45. // on pjax;
  46. unsafeWindow.$(document).on("pjax:end", init); // `pjax:end` also runs on history back;
  47.  
  48. })(typeof unsafeWindow !== "undefined" ? unsafeWindow : window);