Github Pages Linker

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

目前为 2016-02-20 提交的版本。查看 最新版本

  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.2.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. if(document.getElementById("GithubPagesLinker")) {
  31. return;
  32. }
  33.  
  34. var meta = document.querySelector(".repository-meta");
  35. if (!meta) {
  36. return;
  37. }
  38.  
  39. var branch = document.querySelector(".js-navigation-open[data-name='gh-pages']");
  40. if (!branch) {
  41. return;
  42. }
  43.  
  44. var tree = branch.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
  45. var url = String.format("https://{0}.github.io/{1}/", tree[1], tree[2]);
  46.  
  47. var div = document.createElement("div");
  48. div.id = "GithubPagesLinker";
  49. div.style.margin = "-10px 0px 10px";
  50. meta.parentNode.insertBefore(div, meta.nextSibling);
  51.  
  52. var img = document.createElement("img");
  53. img.setAttribute("src", "https://assets-cdn.github.com/images/icons/emoji/octocat.png");
  54. img.setAttribute("align", "absmiddle");
  55. img.classList.add("emoji");
  56. img.style.height = "16px";
  57. img.style.width = "16px";
  58. div.appendChild(img);
  59.  
  60. div.appendChild(document.createTextNode(" "));
  61.  
  62. var a = document.createElement("a");
  63. a.setAttribute("href", "https://pages.github.com");
  64. a.setAttribute("title", "More info about gh-pages...");
  65. a.style.color = "inherit";
  66. a.appendChild(document.createTextNode("Github Pages"));
  67. div.appendChild(a);
  68.  
  69. div.appendChild(document.createTextNode(": "));
  70.  
  71. var aa = document.createElement("a");
  72. aa.setAttribute("href", url);
  73. aa.appendChild(document.createTextNode(url));
  74. div.appendChild(aa);
  75. }
  76.  
  77. // Page load;
  78. console.log('GithubPagesLinker', 'page load');
  79. addLink();
  80.  
  81. // On pjax;
  82. unsafeWindow.$(document).on("pjax:end", function() {
  83. console.log('GithubPagesLinker', 'pjax');
  84. addLink();
  85. });
  86.  
  87. })();