Github Pages Linker

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

目前为 2021-05-27 提交的版本。查看 最新版本

  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 (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.4
  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. var DELAY = 800;
  30.  
  31. var triggerEventClick = new MouseEvent('click', {
  32. view: window,
  33. bubbles: true,
  34. cancelable: true
  35. });
  36.  
  37. function addLink() {
  38. if(document.getElementById("GithubPagesLinker")) {
  39. return;
  40. }
  41.  
  42. var meta = document.querySelector('.file-navigation');
  43. if (!meta) {
  44. return;
  45. }
  46. var closeDropdown = () => {
  47. document.querySelector('[data-toggle-for="branch-select-menu"]').dispatchEvent(triggerEventClick);
  48. }
  49.  
  50. var dropdown = document.querySelector('[data-hotkey="w"]');
  51. dropdown.dispatchEvent(triggerEventClick); // open menu to load data
  52.  
  53. setTimeout(() => {
  54. var branch = document.querySelector('.SelectMenu-item[href$="/tree/gh-pages"]');
  55. if (!branch) {
  56. closeDropdown();
  57. return;
  58. }
  59.  
  60. var tree = branch.getAttribute("href").split("/"); // `/{user}/{repo}/tree/gh-pages`;
  61. var url = String.format("{0}//{1}.github.io/{2}/", tree[0], tree[3], tree[4]);
  62.  
  63. var div = document.createElement("div");
  64. div.id = "GithubPagesLinker";
  65. div.style.margin = "-10px 0px 10px";
  66. meta.parentNode.insertBefore(div, meta.nextSibling);
  67.  
  68. var img = document.createElement("img");
  69. img.setAttribute("src", "https://github.githubassets.com/images/icons/emoji/octocat.png");
  70. img.setAttribute("align", "absmiddle");
  71. img.classList.add("emoji");
  72. img.style.height = "16px";
  73. img.style.width = "16px";
  74. div.appendChild(img);
  75.  
  76. div.appendChild(document.createTextNode(" "));
  77.  
  78. var a = document.createElement("a");
  79. a.setAttribute("href", "{https}://pages.github.com");
  80. a.setAttribute("title", "More info about gh-pages...");
  81. a.style.color = "inherit";
  82. a.appendChild(document.createTextNode("Github Pages"));
  83. div.appendChild(a);
  84.  
  85. div.appendChild(document.createTextNode(": "));
  86.  
  87. var aa = document.createElement("a");
  88. aa.setAttribute("href", url);
  89. aa.appendChild(document.createTextNode(url));
  90. div.appendChild(aa);
  91. closeDropdown();
  92. }, DELAY);
  93. }
  94.  
  95. // Init;
  96. addLink();
  97.  
  98. // On pjax;
  99. document.addEventListener('pjax:end', addLink);
  100.  
  101. })();