Github Pages Linker

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

当前为 2024-02-01 提交的版本,查看 最新版本

  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. (function () {
  22.  
  23. String.format = function (string) {
  24. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  25. return string.replace(/{(\d+)}/g, function (match, number) {
  26. return typeof args[number] !== "undefined" ? args[number] : match;
  27. });
  28. };
  29.  
  30. function addLink() {
  31. if (document.getElementById("GithubPagesLinker")) {
  32. return;
  33. }
  34.  
  35. var meta = document.querySelector('main h1');
  36. if (!meta) {
  37. return;
  38. }
  39.  
  40. var branchSelector = document.querySelector('#branch-select-menu');
  41. if (!branchSelector) {
  42. return;
  43. }
  44.  
  45. var branch = document.querySelector('.SelectMenu-item[href$="/tree/gh-pages"]');
  46. if (branch) {
  47. createLink(branch);
  48. } else {
  49. const observer = new MutationObserver(function () {
  50. var branch2 = document.querySelector('.SelectMenu-item[href$="/tree/gh-pages"]');
  51. if (branch2) {
  52. observer.disconnect();
  53. createLink(branch2);
  54. }
  55. });
  56.  
  57. observer.observe(branchSelector, { subtree: true, childList: true });
  58.  
  59. var dropdown = branchSelector.querySelector('ref-selector');
  60. window.setTimeout(function () {
  61. dropdown.dispatchEvent(new CustomEvent('container-mouseover', { bubbles: true }));
  62. }, 100);
  63. }
  64.  
  65. function createLink(branch2) {
  66. var tree = branch2.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
  67. var url = String.format("{0}//{1}.github.io/{2}/", tree[0], tree[3], tree[4]);
  68.  
  69. var div = document.createElement("small");
  70. div.id = "GithubPagesLinker";
  71. meta.parentNode.insertBefore(div, meta.nextSibling);
  72.  
  73. var img = document.createElement("img");
  74. img.setAttribute("src", "https://github.githubassets.com/images/icons/emoji/octocat.png");
  75. img.setAttribute("align", "absmiddle");
  76. img.classList.add("emoji");
  77. img.style.height = "16px";
  78. img.style.width = "16px";
  79. div.appendChild(img);
  80.  
  81. div.appendChild(document.createTextNode(" "));
  82.  
  83. var a = document.createElement("a");
  84. a.setAttribute("href", "{https}://pages.github.com");
  85. a.setAttribute("title", "More info about gh-pages...");
  86. a.style.color = "inherit";
  87. a.appendChild(document.createTextNode("Github Pages"));
  88. div.appendChild(a);
  89.  
  90. div.appendChild(document.createTextNode(": "));
  91.  
  92. var aa = document.createElement("a");
  93. aa.setAttribute("href", url);
  94. aa.appendChild(document.createTextNode(url));
  95. div.appendChild(aa);
  96. }
  97. }
  98.  
  99. // Init;
  100. addLink();
  101.  
  102. // On pjax;
  103. document.addEventListener('pjax:end', addLink);
  104.  
  105. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址