Github Pages Linker

Add a link to Github Pages (gh-pages) when available.

目前为 2014-11-18 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @id Github_Pages_Linker@https://github.com/jerone/UserScripts
  3. // @name Github Pages Linker
  4. // @namespace https://github.com/jerone/UserScripts/
  5. // @description Add a link to Github Pages (gh-pages) when available.
  6. // @author jerone
  7. // @copyright 2014+, jerone (http://jeroenvanwarmerdam.nl)
  8. // @license GNU GPLv3
  9. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Pages_Linker
  10. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Pages_Linker
  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. // @version 1
  14. // @grant none
  15. // @run-at document-end
  16. // @include https://github.com/*
  17. // ==/UserScript==
  18. /* global unsafeWindow */
  19.  
  20. (function() {
  21.  
  22. String.format = function(string) {
  23. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  24. return string.replace(/{(\d+)}/g, function(match, number) {
  25. return typeof args[number] !== "undefined" ? args[number] : match;
  26. });
  27. };
  28.  
  29. function addLink() {
  30. var meta = document.querySelector(".repository-meta");
  31. if (!meta) { return; }
  32.  
  33. var branch = document.querySelector(".js-navigation-open[data-name='gh-pages']");
  34. if (!branch) { return; }
  35.  
  36. var tree = branch.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
  37. var url = String.format("https://{0}.github.io/{1}", tree[1], tree[2]);
  38.  
  39. var div = document.createElement("div");
  40. div.style.margin = "-10px 0px 10px";
  41. meta.parentNode.insertBefore(div, meta.nextSibling);
  42.  
  43. var img = document.createElement("img");
  44. img.setAttribute("src", "https://assets-cdn.github.com/images/icons/emoji/octocat.png");
  45. img.setAttribute("align", "absmiddle");
  46. img.classList.add("emoji");
  47. img.style.height = "16px";
  48. img.style.width = "16px";
  49. div.appendChild(img);
  50.  
  51. div.appendChild(document.createTextNode(" "));
  52.  
  53. var a = document.createElement("a");
  54. a.setAttribute("href", "https://pages.github.com");
  55. a.setAttribute("title", "More info about gh-pages...");
  56. a.style.color = "inherit";
  57. a.appendChild(document.createTextNode("Github Pages"));
  58. div.appendChild(a);
  59.  
  60. div.appendChild(document.createTextNode(": "));
  61.  
  62. var aa = document.createElement("a");
  63. aa.setAttribute("href", url);
  64. aa.appendChild(document.createTextNode(url));
  65. div.appendChild(aa);
  66. }
  67.  
  68. // init;
  69. addLink();
  70.  
  71. // on pjax;
  72. unsafeWindow.$(document).on("pjax:end", addLink); // `pjax:end` also runs on history back;
  73.  
  74. })();