Github Pages Linker

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

  1. // ==UserScript==
  2. // @name Github Pages Linker
  3. // @id Github_Pages_Linker@https://github.com/jerone/UserScripts
  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 (https://github.com/jerone)
  8. // @license CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
  9. // @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  10. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Pages_Linker
  11. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Pages_Linker
  12. // @supportURL https://github.com/jerone/UserScripts/issues
  13. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
  14. // @icon https://github.githubassets.com/pinned-octocat.svg
  15. // @version 1.2.5
  16. // @grant none
  17. // @run-at document-end
  18. // @include https://github.com/*
  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 addLink() {
  32. if (document.getElementById("GithubPagesLinker")) {
  33. return;
  34. }
  35.  
  36. var meta = document.querySelector("main h1");
  37. if (!meta) {
  38. return;
  39. }
  40.  
  41. var branchSelector = document.querySelector("#branch-select-menu");
  42. if (!branchSelector) {
  43. return;
  44. }
  45.  
  46. var branch = document.querySelector(
  47. '.SelectMenu-item[href$="/tree/gh-pages"]',
  48. );
  49. if (branch) {
  50. createLink(branch);
  51. } else {
  52. const observer = new MutationObserver(function () {
  53. var branch2 = document.querySelector(
  54. '.SelectMenu-item[href$="/tree/gh-pages"]',
  55. );
  56. if (branch2) {
  57. observer.disconnect();
  58. createLink(branch2);
  59. }
  60. });
  61.  
  62. observer.observe(branchSelector, {
  63. subtree: true,
  64. childList: true,
  65. });
  66.  
  67. var dropdown = branchSelector.querySelector("ref-selector");
  68. window.setTimeout(function () {
  69. dropdown.dispatchEvent(
  70. new CustomEvent("container-mouseover", { bubbles: true }),
  71. );
  72. }, 100);
  73. }
  74.  
  75. function createLink(branch2) {
  76. var tree = branch2.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
  77. var url = String.format(
  78. "{0}//{1}.github.io/{2}/",
  79. tree[0],
  80. tree[3],
  81. tree[4],
  82. );
  83.  
  84. var div = document.createElement("small");
  85. div.id = "GithubPagesLinker";
  86. meta.parentNode.insertBefore(div, meta.nextSibling);
  87.  
  88. var img = document.createElement("img");
  89. img.setAttribute(
  90. "src",
  91. "https://github.githubassets.com/images/icons/emoji/octocat.png",
  92. );
  93. img.setAttribute("align", "absmiddle");
  94. img.classList.add("emoji");
  95. img.style.height = "16px";
  96. img.style.width = "16px";
  97. div.appendChild(img);
  98.  
  99. div.appendChild(document.createTextNode(" "));
  100.  
  101. var a = document.createElement("a");
  102. a.setAttribute("href", "{https}://pages.github.com");
  103. a.setAttribute("title", "More info about gh-pages...");
  104. a.style.color = "inherit";
  105. a.appendChild(document.createTextNode("Github Pages"));
  106. div.appendChild(a);
  107.  
  108. div.appendChild(document.createTextNode(": "));
  109.  
  110. var aa = document.createElement("a");
  111. aa.setAttribute("href", url);
  112. aa.appendChild(document.createTextNode(url));
  113. div.appendChild(aa);
  114. }
  115. }
  116.  
  117. // Init;
  118. addLink();
  119.  
  120. // On pjax;
  121. document.addEventListener("pjax:end", addLink);
  122. })();