Skip to content

Instantly share code, notes, and snippets.

@nkmathew
Last active November 10, 2018 22:19
Show Gist options
  • Save nkmathew/d70b55827ce9f6e9189593912eb4c787 to your computer and use it in GitHub Desktop.
Save nkmathew/d70b55827ce9f6e9189593912eb4c787 to your computer and use it in GitHub Desktop.
Adds button for serving the current repository as a static site using rawgit. Useful for viewing react/angular demo projects
// ==UserScript==
// @name Raw Git Button
// @description Open rawgit.com on the current repo serving its files
// @author nkmathew
// @namespace http://nkmathew.net
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @include /https:\/\/github.com\/[^/]+\/[^/]+[/]{0,1}$/
// @version 0.1.0
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
var pageUrl = location.href.replace(/github.com/, 'rawgit.com');
if (pageUrl.endsWith('/')) {
pageUrl += 'master/';
} else {
pageUrl += '/master/';
}
// Try to handle cases where the web root folder is in a folder called 'public'
var root = '';
var indexHtmlExists = false;
var htmlFiles = [];
jQuery('.content .css-truncate a').each(function () {
var fname = this.text;
if (fname === 'public') {
root = 'public/';
} else if (fname === 'index.html') {
indexHtmlExists = true;
} else if (fname.endsWith('html') || fname.endsWith('htm')) {
htmlFiles.push(fname);
}
});
// Find a default html file to serve in the root folder instead of hitting a 404
if (indexHtmlExists) {
root = '';
} else {
if (htmlFiles.length) {
root = htmlFiles[htmlFiles.length - 1];
}
}
var content =
'<li class="header-nav-item" id="rawgit-item">' +
' <a class="header-nav-link" id="rawgit-link" href="' + pageUrl + root +
'">Raw Git</a>' +
'</li>';
jQuery(content).insertAfter(jQuery('.header-nav.float-left'));
jQuery('#rawgit-item').css('list-style-type', 'none');
jQuery('#rawgit-link').css('color', 'rgb(65, 131, 196)');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment