// ==UserScript== // @name GitHub HTML Preview // @version 0.1.1 // @description A userscript that adds preview links to HTML files // @license MIT // @author Rob Garrison // @namespace https://github.com/Mottie // @include https://github.com/* // @run-at document-idle // @grant none // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=666427 // @icon https://github.githubassets.com/pinned-octocat.svg // @downloadURL none // ==/UserScript== (() => { "use strict"; // Example page: https://github.com/codrops/DecorativeLetterAnimations // Source: https://github.com/htmlpreview/htmlpreview.github.com const prefix = "http://htmlpreview.github.io/?"; // html & htm extensions const regex = /\.html?$/; const link = document.createElement("a"); link.className = "ghhp-btn btn btn-sm py-0 px-1 mx-1 v-align-baseline tooltipped tooltipped-n"; link.setAttribute("aria-label", "Open in new tab"); link.target = "_blank"; link.innerHTML = ` `; function addLink(el) { const cell = el.closest(".js-navigation-item div:nth-child(2) span"); if (!$(".ghhp-btn", cell)) { const preview = link.cloneNode(true); preview.href = prefix + el.href; cell.appendChild(preview); } } function init() { if ($(".js-details-container")) { const files = $("#files").parentElement; $$(".js-navigation-item div:nth-child(2) .js-navigation-open", files).forEach(el => { if (regex.test(el.title)) { addLink(el); } }); } } function $(str, el) { return (el || document).querySelector(str); } function $$(str, el) { return [...(el || document).querySelectorAll(str)]; } document.addEventListener("ghmo:container", () => { // init after a short delay to allow rendering of file list setTimeout(() => { init(); }, 200); }); init(); })();