Skip to content

Instantly share code, notes, and snippets.

@Aymkdn
Last active December 10, 2015 18:48
Show Gist options
  • Save Aymkdn/4476768 to your computer and use it in GitHub Desktop.
Save Aymkdn/4476768 to your computer and use it in GitHub Desktop.
Run a web page from a Github project in one click. See README below.
// ==UserScript==
// @name [GitHub] Host files
// @namespace http://blog.kodono.info
// @description Permits to visualize a file hosted by GitHub like if it was on a server
// @include https://github.com/*
// @include https://gist.github.com/*
// @grant none
// @unwrap
// ==/UserScript==
var myServer = "http://github-proxy.kodono.info"; // make sure to define the URL of your server where the index.php file is stored
var $ = unsafeWindow.$;
function setHosted() {
// if we already have the button
if (document.getElementById('my-run-button') !== null) return;
// for ://github.com
var git = $(".actions .button-group:eq(0)");
if (git.length == 1)
git.append('<a class="button minibutton" href="'+myServer+'/?q=https://raw.github.com'+$('#raw-url').attr("href").replace("raw/","")+'"><span><span id="my-run-button" class="icon"></span>&#9654; Run</span></a>');
else if ($('.actions > a').text() == "raw")
$('.actions').append('<a href="'+myServer+'/?q=https://gist.github.com'+$('.actions > a').attr("href")+'">&#9654; Run</a>') // for ://gist.github.com
}
// when the page is loaded in AJAX we could not see it
var storedUrl = window.location.href;
window.setInterval(function () {
if (window.location.href !== storedUrl) {
storedUrl = window.location.url;
setHosted();
}
}, 100);
// add the button
setHosted();
<?php
$q = $_GET["q"];
$u = parse_url($q);
if ($u["host"] == "raw.github.com" || $u["host"] == "gist.github.com") {
// send the correct mime type
$e = explode(".",$u["path"]);
$e = strtolower($e[count($e)-1]);
switch($e) {
case 'css':
$mime = 'text/css';
break;
case 'js':
$mime = 'application/javascript';
break;
case 'gif':
$mime = 'image/gif';
break;
case 'png':
$mime = 'image/png';
break;
case 'jpg':
$mime = 'image/jpeg';
break;
case 'html':
case 'htm':
$mime = 'text/html';
break;
case 'swf':
$mime = 'application/x-shockwave-flash';
break;
default:
$mime = 'text/plain';
break;
}
$s = file_get_contents($q);
$p = explode("/",$u["path"]);
$p = array_slice($p,0,count($p)-1);
$p = implode("/",$p);
$s = preg_replace('/href="([^http])(.*)"/','href="index.php?q='.$u["scheme"].'://'.$u["host"].$p.'/$1$2"',$s);
$s = preg_replace('/src="([^http])(.*)"/','src="index.php?q='.$u["scheme"].'://'.$u["host"].$p.'/$1$2"',$s);
header('Content-type: '.$mime);
echo utf8_decode($s);
}
?>

When you visit a Github project with, for example, a demo.html page you can simply click on the "Run" button and the page will be loaded directly into your browser without the need to download the full git project in the correct directory and play with your browser to find where you downloaded it.

Instructions :

  1. Store the below PHP file to your web server;
  2. Edit the below Greasemonkey file with the URL of your webserver;
  3. Load the Greasemonkey script into your web browser;
  4. Go to any file stored on Github and you'll see a "Run" button just after the "History" button.

Once you did all the steps then you can try it. For example go to https://github.com/prezjordan/dynamo.js/blob/master/test/test.html and you should see the Run button:

Screenshot of the rendering

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment