Skip to content

Instantly share code, notes, and snippets.

@mosherbrian
Created January 23, 2022 19:33
Show Gist options
  • Save mosherbrian/a687614439bafae619be25a4179eb249 to your computer and use it in GitHub Desktop.
Save mosherbrian/a687614439bafae619be25a4179eb249 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Gist HTML Live Preview
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Display a button on GitHub Gist HTML files to preview the file using https://raw.githack.com to serve the page in developer mode.
// @author Brian Mosher
// @match https://gist.github.com/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
(function() {
'use strict';
let rawBtnLinks = document.querySelectorAll("div .file-actions > a");
for (let link of rawBtnLinks)
{
let rawURL = link.href;
let urlExtension = rawURL.split('.').pop();
if ( urlExtension.toLowerCase() === 'html' || urlExtension.toLowerCase() === 'htm' )
{
let newPreviewLink = link.cloneNode();
newPreviewLink.textContent = "Preview";
newPreviewLink.href = newPreviewLink.href.replace("gist.github.com", "gist.githack.com");
link.parentElement.appendChild(newPreviewLink);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment